import { Controller } from "./main";

export class BatteryView{
    voltage_milli_volt: HTMLSpanElement;
    current_milli_ampere: HTMLSpanElement;
    cycle_count: HTMLSpanElement;
    design_milli_ampere: HTMLSpanElement;
    remaining_milli_ampere: HTMLSpanElement;
    state_of_charge: HTMLSpanElement;
    state_of_health: HTMLSpanElement;
    temperature: HTMLSpanElement;
    auto_refresh: HTMLInputElement;
    timer: NodeJS.Timeout | undefined;
    controller: Controller;

    constructor (controller:Controller) {
      (document.getElementById("batteryview") as HTMLElement).innerHTML = require("./batteryview.html")
        this.voltage_milli_volt = document.getElementById("battery_voltage_milli_volt") as HTMLSpanElement;
        this.current_milli_ampere = document.getElementById("battery_current_milli_ampere") as HTMLSpanElement;
        this.cycle_count = document.getElementById("battery_cycle_count") as HTMLSpanElement;
        this.design_milli_ampere = document.getElementById("battery_design_milli_ampere") as HTMLSpanElement;
        this.remaining_milli_ampere = document.getElementById("battery_remaining_milli_ampere") as HTMLSpanElement;
        this.state_of_charge = document.getElementById("battery_state_of_charge") as HTMLSpanElement;
        this.state_of_health = document.getElementById("battery_state_of_health") as HTMLSpanElement;
        this.temperature = document.getElementById("battery_temperature") as HTMLSpanElement;
        this.auto_refresh = document.getElementById("battery_auto_refresh") as HTMLInputElement;

        this.controller = controller
        this.auto_refresh.onchange = () => {
            if(this.timer){
              clearTimeout(this.timer)
            }
            if(this.auto_refresh.checked){
              controller.updateBatteryData()
            }
          }
    }

    update(batterystate: BatteryState|null){
        if (batterystate == null) {
            this.voltage_milli_volt.innerText = "N/A"
            this.current_milli_ampere.innerText = "N/A"
            this.cycle_count.innerText = "N/A"
            this.design_milli_ampere.innerText = "N/A"
            this.remaining_milli_ampere.innerText = "N/A"
            this.state_of_charge.innerText = "N/A"
            this.state_of_health.innerText = "N/A"
            this.temperature.innerText = "N/A"
        } else {
            this.voltage_milli_volt.innerText = String(+batterystate.voltage_milli_volt/1000)
            this.current_milli_ampere.innerText = batterystate.current_milli_ampere
            this.cycle_count.innerText = batterystate.cycle_count
            this.design_milli_ampere.innerText = batterystate.design_milli_ampere
            this.remaining_milli_ampere.innerText = batterystate.remaining_milli_ampere
            this.state_of_charge.innerText = batterystate.state_of_charge
            this.state_of_health.innerText = batterystate.state_of_health
            this.temperature.innerText = String(+batterystate.temperature / 100)
        }
        

        if(this.auto_refresh.checked){
            this.timer = setTimeout(this.controller.updateBatteryData, 1000);
          } else {
            if(this.timer){
              clearTimeout(this.timer)
            }
          }
    }
  }