import { ZigBeeClient } from '../zigbee/zig-bee-client';
import { CharacteristicEventTypes, CharacteristicGetCallback, PlatformAccessory } from 'homebridge';
import { ZigbeeNTHomebridgePlatform } from '../platform';
import { DeviceState } from '../zigbee/types';
import { SensorServiceBuilder } from './sensor-service-builder';

export class MotionSensorServiceBuilder extends SensorServiceBuilder {
  constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    state: DeviceState
  ) {
    super(platform.Service.MotionSensor, platform, accessory, client, state);
  }

  public withOccupancy(): MotionSensorServiceBuilder {
    const Characteristic = this.platform.Characteristic;

    this.service
      .getCharacteristic(Characteristic.MotionDetected)
      .on(CharacteristicEventTypes.GET, async (callback: CharacteristicGetCallback) => {
        this.log.debug(
          `Getting state for motion sensor ${this.friendlyName}: ${this.state.occupancy === true}`
        );
        callback(null, this.state.occupancy === true);
      });

    return this;
  }
}