import { Controller } from "./main";

export class NightLampView {
    private readonly night_lamp_only_when_dark: HTMLInputElement;
    private readonly night_lamp_time_start: HTMLSelectElement;
    private readonly night_lamp_time_end: HTMLSelectElement;
    private readonly night_lamp_test: HTMLInputElement;
    private readonly night_lamp_enabled: HTMLInputElement;
    private readonly night_lamp_soc_low: HTMLInputElement;
    private readonly night_lamp_soc_restore: HTMLInputElement;

    constructor(controller:Controller){
      (document.getElementById("lightview") as HTMLElement).innerHTML = require('./nightlightview.html') as string;

      this.night_lamp_only_when_dark = document.getElementById("night_lamp_only_when_dark") as HTMLInputElement;
      this.night_lamp_only_when_dark.onchange = controller.configChanged

      this.night_lamp_enabled = document.getElementById("night_lamp_enabled") as HTMLInputElement;
      this.night_lamp_enabled.onchange = controller.configChanged

      this.night_lamp_soc_low = document.getElementById("night_lamp_soc_low") as HTMLInputElement;
      this.night_lamp_soc_low.onchange = controller.configChanged

      this.night_lamp_soc_restore = document.getElementById("night_lamp_soc_restore") as HTMLInputElement;
      this.night_lamp_soc_restore.onchange = controller.configChanged

      this.night_lamp_time_start = document.getElementById("night_lamp_time_start") as HTMLSelectElement;
      this.night_lamp_time_start.onchange = controller.configChanged
      for (let i = 0; i < 24; i++) {
        let option = document.createElement("option");
        if (i == 20){
            option.selected = true
        }
        option.innerText = i.toString();
        this.night_lamp_time_start.appendChild(option);
      }
      this.night_lamp_time_end = document.getElementById("night_lamp_time_end") as HTMLSelectElement;
      this.night_lamp_time_end.onchange = controller.configChanged
  
      for (let i = 0; i < 24; i++) {
        let option = document.createElement("option");
        if (i == 1){
            option.selected = true
        }
        option.innerText = i.toString();
        this.night_lamp_time_end.appendChild(option);
      }

      let night_lamp_test = document.getElementById("night_lamp_test") as HTMLInputElement;
      this.night_lamp_test = night_lamp_test
      this.night_lamp_test.onchange = () => {
        controller.testNightLamp(night_lamp_test.checked)
      }
    }
  
    setConfig(nightLamp: NightLampConfig) {
      this.night_lamp_only_when_dark.checked = nightLamp.night_lamp_only_when_dark
      this.night_lamp_time_start.value = nightLamp.night_lamp_hour_start.toString();
      this.night_lamp_time_end.value = nightLamp.night_lamp_hour_end.toString();
      this.night_lamp_enabled.checked = nightLamp.enabled;
      this.night_lamp_soc_low.value = nightLamp.low_soc_cutoff.toString();
      this.night_lamp_soc_restore.value = nightLamp.low_soc_restore.toString();
    }
  
    getConfig(): NightLampConfig {
      return {
        night_lamp_hour_start: +this.night_lamp_time_start.value,
        night_lamp_hour_end: +this.night_lamp_time_end.value,
        night_lamp_only_when_dark: this.night_lamp_only_when_dark.checked,
        enabled: this.night_lamp_enabled.checked,
        low_soc_cutoff: +this.night_lamp_soc_low.value,
        low_soc_restore: +this.night_lamp_soc_restore.value
      }
    }
  }