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

export class Sound {
	private _Main: Main;

	volumeInput: HTMLInputElement = document.querySelector(
		'.ntsh_soundsettings-volume',
	);
	eqLowInput: HTMLInputElement = document.querySelector(
		'.ntsh_soundsettings-eqlow',
	);
	eqMidInput: HTMLInputElement = document.querySelector(
		'.ntsh_soundsettings-eqmid',
	);
	eqHighInput: HTMLInputElement = document.querySelector(
		'.ntsh_soundsettings-eqhigh',
	);

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

		this.registerListeners();
	}

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

		const volumeSliderIndex = state.parameters?.sliders.findIndex(
			(slider) => slider.sliderName === 'Volume',
		);
		const volumeSlider = state.parameters?.sliders[volumeSliderIndex ?? -1];

		const eqLowSliderIndex = state.parameters?.sliders.findIndex(
			(slider) => slider.sliderName === 'EQ_Low',
		);
		const eqLowSlider = state.parameters?.sliders[eqLowSliderIndex ?? -1];

		const eqMidSliderIndex = state.parameters?.sliders.findIndex(
			(slider) => slider.sliderName === 'EQ_Mid',
		);
		const eqMidSlider = state.parameters?.sliders[eqMidSliderIndex ?? -1];

		const eqHighSliderIndex = state.parameters?.sliders.findIndex(
			(slider) => slider.sliderName === 'EQ_High',
		);
		const eqHighSlider = state.parameters?.sliders[eqHighSliderIndex ?? -1];

		this.volumeInput.valueAsNumber =
			volumeSlider?.outputValue ?? this.volumeInput.valueAsNumber;
		this.eqLowInput.valueAsNumber =
			eqLowSlider?.outputValue ?? this.eqLowInput.valueAsNumber;
		this.eqMidInput.valueAsNumber =
			eqMidSlider?.outputValue ?? this.eqMidInput.valueAsNumber;
		this.eqHighInput.valueAsNumber =
			eqHighSlider?.outputValue ?? this.eqHighInput.valueAsNumber;

		this.volumeInput.setAttribute(
			'index',
			volumeSliderIndex?.toString() ?? '-1',
		);
		this.eqLowInput.setAttribute(
			'index',
			eqLowSliderIndex?.toString() ?? '-1',
		);
		this.eqMidInput.setAttribute(
			'index',
			eqMidSliderIndex?.toString() ?? '-1',
		);
		this.eqHighInput.setAttribute(
			'index',
			eqHighSliderIndex?.toString() ?? '-1',
		);
	}

	private registerListeners() {
		this.volumeInput.onchange = () => {
			this._Main.socket.emit(
				'unityWebSocket',
				'parameterValue',
				parseInt(this.volumeInput.getAttribute('index') ?? '-1'),
				this.volumeInput.valueAsNumber,
			);
		};

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

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

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