import { Main } from './main';

export class Timer {
	private _Main: Main;

	startup: HTMLInputElement = document.querySelector('.ntsh-timer-startup');
	shutdown: HTMLInputElement = document.querySelector('.ntsh-timer-shutdown');

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

		this.registerListeners();
	}

	update(data: {
		start: { hour: number; minute: number };
		end: { hour: number; minute: number };
	}) {
		const start = `${data.start.hour.toString().padStart(2, '0')}:${data.start.minute.toString().padStart(2, '0')}`;
		const end = `${data.end.hour.toString().padStart(2, '0')}:${data.end.minute.toString().padStart(2, '0')}`;

		this.startup.value = start;
		this.shutdown.value = end;
	}

	registerListeners() {
		this.startup.onchange = () => {
			const [hour, minute] = this.startup.value.split(':').map(Number);
			this._Main.socket.emit('setTimerStart', { hour, minute });
		};
		this.shutdown.onchange = () => {
			const [hour, minute] = this.shutdown.value.split(':').map(Number);
			this._Main.socket.emit('setTimerEnd', { hour, minute });
		};
	}
}
