import { Controller } from "./main";

export class SubmitView {
  json: HTMLDivElement;
  submitFormBtn: HTMLButtonElement;
  submit_status: HTMLElement;
  backupBtn: HTMLButtonElement;
  restoreBackupBtn: HTMLButtonElement;
  backuptimestamp: HTMLElement;
  backupsize: HTMLElement;
  backupjson: HTMLElement;

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

    let showJson = document.getElementById('showJson') as HTMLButtonElement
    let rawdata = document.getElementById('rawdata') as HTMLElement
    this.json = document.getElementById('json') as HTMLDivElement
    this.backupjson = document.getElementById('backupjson') as HTMLDivElement
    this.submitFormBtn = document.getElementById("submit") as HTMLButtonElement
    this.backupBtn = document.getElementById("backup") as HTMLButtonElement
    this.restoreBackupBtn = document.getElementById("restorebackup") as HTMLButtonElement
    this.backuptimestamp = document.getElementById("backuptimestamp") as HTMLElement
    this.backupsize = document.getElementById("backupsize") as HTMLElement
    this.submit_status = document.getElementById("submit_status") as HTMLElement
    this.submitFormBtn.onclick = () => {
      controller.uploadConfig(this.json.textContent as string, (status: string) => {
        this.submit_status.innerHTML = status;
      });
    }
    this.backupBtn.onclick = () => {
      controller.backupConfig(this.json.textContent as string, (status: string) => {
        this.submit_status.innerHTML = status;
      });
    }
    this.restoreBackupBtn.onclick = () => {
      controller.getBackupConfig();
    }
    showJson.onclick = () => {
      if (rawdata.style.display == "none"){
        rawdata.style.display = "flex";
      } else {
        rawdata.style.display = "none";
      }
    }
  }

  setBackupInfo(header: BackupHeader) {
    this.backuptimestamp.innerText = header.timestamp
    this.backupsize.innerText = header.size.toString()
  }

  setJson(pretty: string) {
    this.json.textContent = pretty
  }

  setBackupJson(pretty: string) {
    this.backupjson.textContent = pretty
  }
}