import { Main, UnityWebSocketStatus } from './main';

export class Lighting {
	private _Main: Main;

	autoLightingInput: HTMLInputElement = document.querySelector(
		'.ntsh-autolighting input',
	);
	gainInput: HTMLInputElement = document.querySelector(
		'.ntsh_lightingsettings-gain',
	);
	exposureInput: HTMLInputElement = document.querySelector(
		'.ntsh_lightingsettings-exposure',
	);
	whiteBalanceInput: HTMLInputElement = document.querySelector(
		'.ntsh_lightingsettings-whitebalance',
	);

	constructor(Main: Main) {
		this._Main = Main;

		this.registerListeners();
	}

	update(state: UnityWebSocketStatus) {
		if (state?.parameters?.advancedSliders == null) return;

		const autoExposureIndex = state.parameters?.advancedSliders.findIndex(
			(slider) => slider.sliderName === 'AutoExposure',
		);
		const autoExposureSlider =
			state.parameters?.advancedSliders[autoExposureIndex ?? -1];

		const gainSliderIndex = state.parameters?.advancedSliders.findIndex(
			(slider) => slider.sliderName === 'Gain',
		);
		const gainSlider =
			state.parameters?.advancedSliders[gainSliderIndex ?? -1];

		const exposureSliderIndex = state.parameters?.advancedSliders.findIndex(
			(slider) => slider.sliderName === 'Exposure',
		);
		const exposureSlider =
			state.parameters?.advancedSliders[exposureSliderIndex ?? -1];

		const whiteBalanceSliderIndex =
			state.parameters?.advancedSliders.findIndex(
				(slider) => slider.sliderName === 'Whitebalance',
			);
		const whiteBalanceSlider =
			state.parameters?.advancedSliders[whiteBalanceSliderIndex ?? -1];

		this.autoLightingInput.checked = autoExposureSlider?.outputValue === 1;
		this.gainInput.valueAsNumber =
			gainSlider?.outputValue ?? this.gainInput.valueAsNumber;
		this.exposureInput.valueAsNumber =
			exposureSlider?.outputValue ?? this.exposureInput.valueAsNumber;
		this.whiteBalanceInput.valueAsNumber =
			whiteBalanceSlider?.outputValue ??
			this.whiteBalanceInput.valueAsNumber;

		this.autoLightingInput.setAttribute(
			'index',
			autoExposureIndex?.toString() ?? '-1',
		);
		this.gainInput.setAttribute(
			'index',
			gainSliderIndex?.toString() ?? '-1',
		);
		this.exposureInput.setAttribute(
			'index',
			exposureSliderIndex?.toString() ?? '-1',
		);
		this.whiteBalanceInput.setAttribute(
			'index',
			whiteBalanceSliderIndex?.toString() ?? '-1',
		);
	}

	private registerListeners() {
		this.autoLightingInput.onchange = () => {
			this._Main.socket.emit(
				'unityWebSocket',
				'advancedParameterValue',
				parseInt(this.autoLightingInput.getAttribute('index') ?? '-1'),
				this.autoLightingInput.checked ? 1 : 0,
			);
		};

		this.gainInput.onchange = () => {
			this._Main.socket.emit(
				'unityWebSocket',
				'advancedParameterValue',
				parseInt(this.gainInput.getAttribute('index') ?? '-1'),
				this.gainInput.valueAsNumber,
			);
		};

		this.exposureInput.onchange = () => {
			this._Main.socket.emit(
				'unityWebSocket',
				'advancedParameterValue',
				parseInt(this.exposureInput.getAttribute('index') ?? '-1'),
				this.exposureInput.valueAsNumber,
			);
		};

		this.whiteBalanceInput.onchange = () => {
			this._Main.socket.emit(
				'unityWebSocket',
				'advancedParameterValue',
				parseInt(this.whiteBalanceInput.getAttribute('index') ?? '-1'),
				this.whiteBalanceInput.valueAsNumber,
			);
		};
	}
}
