import { FanDevice } from '@matter/node/devices';

import { FanControl } from '@matter/main/clusters';
import { Converter } from 'isy-nodejs/Converters';
import { Devices } from 'isy-nodejs/ISY';
import { BehaviorRegistry } from '../BehaviorRegistry.js';
import { ISYClusterBehavior } from '../ISYClusterBehavior.js';

export class FanControlBehavior extends ISYClusterBehavior(FanDevice.requirements.FanControlServer, Devices.Insteon.Fan) {
	override async initialize(_options?: {}) {
		await super.initialize(_options);
		this.state.fanModeSequence = FanControl.FanModeSequence.OffLowMedHigh;
		this.state.percentCurrent = this.device?.motor.status ?? 0;
		this.events.fanMode$Changed.on(async (value, oldValue, context) => {
			if (!context.offline) {
				this.logger.debug(`Request received to update fanMode to ${FanControl.FanMode[value]}`);

				return await this.device.motor.on(Converter.Matter.FanLevel.FanMode.from(value));
			}
		});
		this.events.percentSetting$Changed.on(async (value, oldValue, context) => {
			if (!context.offline) {
				this.logger.debug(`Request received to update percentSetting to ${value}`);
				//let newVal = Converter.Standard.LevelFrom0To255.Percent.from(value);
				return await this.device.motor.on(value);
			}
		});
	}

	/*this.state.onLevel = Converter.get('Level255toero.LightingLevel').to(this.device.drivers.OL)*/
	//this.state.currentLevel = this.device.drivers.ST.value;

	//this.state.onLevel = this.device.drivers.OL;
}
BehaviorRegistry.register(FanControlBehavior);
