import { Controller } from "./main";

export class OTAView {
    readonly file1Upload: HTMLInputElement;
    readonly firmware_buildtime: HTMLDivElement;
    readonly firmware_githash: HTMLDivElement;
    readonly firmware_partition: HTMLDivElement;

    constructor(controller: Controller) {
        (document.getElementById("firmwareview") as HTMLElement).innerHTML = require("./ota.html")

        this.firmware_buildtime = document.getElementById("firmware_buildtime") as HTMLDivElement;
        this.firmware_githash = document.getElementById("firmware_githash") as HTMLDivElement;
        this.firmware_partition = document.getElementById("firmware_partition") as HTMLDivElement;

        
        const file = document.getElementById("firmware_file") as HTMLInputElement;
        this.file1Upload = file
        this.file1Upload.onchange = () => {
            const selectedFile = file.files?.[0];
            if (selectedFile == null) {
                //TODO error dialog here
                return
            }
            controller.uploadNewFirmware(selectedFile);
        };
    }

    setVersion(versionInfo: VersionInfo) {
        this.firmware_buildtime.innerText = versionInfo.build_time;
        this.firmware_githash.innerText = versionInfo.git_hash;
        this.firmware_partition.innerText = versionInfo.partition;
    }
}