import FipamoAdminAPI from "../libraries/FipamoAdminAPI";
//import FipamoUtilityAPI from "../libraries/FipamoUtilityAPI";
import Maintenance from "./controllers/MaintenanceManager";
import DataUitls from "./utils/DataUtils";
import * as DataEvent from "./events/DataEvent";
import DashManager from "./controllers/DashManager";
import Notfications from "./ui/Notifications";
const data = new DataUitls();
const notify = new Notfications();

export default class Base {
  //--------------------------
  // constructor
  //--------------------------
  constructor() {
    this.processing = false;
    this.start();
  }

  //--------------------------
  // methods
  //--------------------------
  start() {
    if (
      document.getElementById("dash-form") ||
      document.getElementById("dash-init")
    ) {
      var options = document.getElementsByClassName("init-option");
      for (let index = 0; index < options.length; index++) {
        options[index].addEventListener("click", (e) => this.handleOptions(e));
      }
      if (document.getElementById("dash-form")) {
        document
          .getElementById("login-btn")
          .addEventListener("click", (e) => this.handleLogin(e));
      } else {
        document
          .getElementById("init-blog")
          .addEventListener("click", (e) => this.handleSetup(e));
        document
          .getElementById("blog-restore")
          .addEventListener("click", (e) => this.handleRestore(e));
      }
    } else if (document.getElementById("dash-reset")) {
      document
        .getElementById("get-secret-btn")
        .addEventListener("click", (e) => this.handleReset(e));

      document
        .getElementById("reset-btn")
        .addEventListener("click", (e) => this.handleReset(e));
    } else {
      new DashManager();
    }
  }
  //--------------------------
  // event handlers
  //--------------------------
  handleLogin(e) {
    if (this.processing) return;
    let self = this;
    e.stopPropagation();
    e.preventDefault();
    let authForm = data.formDataToJSON(document.getElementById("login"));
    notify.alert("Looking, hold up", null);
    let api = new FipamoAdminAPI();
    this.processing = true;
    api
      .login(authForm)
      .then((response) => {
        self.processing = false;
        if (response.type === DataEvent.REQUEST_LAME) {
          notify.alert(response.message, false);
        } else {
          notify.alert(response.message, true);
          e.target.innerHTML = response.message;
          setTimeout(() => {
            window.location = "/dashboard";
          }, 500);
        }
      })
      .catch((err) => {
        self.processing = false;
        notify.alert(err, false);
      });
  }

  handleSetup(e) {
    if (this.processing) return;
    let self = this;
    e.stopPropagation();
    e.preventDefault();
    let setUpForm = data.formDataToJSON(document.getElementById("init-form"));
    let mm = new Maintenance();
    this.processing = true;
    mm.create(setUpForm)
      .then((response) => {
        if (response.type === DataEvent.API_INIT_LAME) {
          self.processing = false;
          notify.alert(response.message, false);
        } else {
          self.processing = false;
          notify.alert(response.message, true);
          setTimeout(() => {
            window.location = "/dashboard";
          }, 700);
        }
      })
      .catch((err) => {
        self.processing = false;
        notify.alert(err, false);
      });
  }

  handleRestore(e) {
    if (this.processing) return;
    let self = this;
    e.stopPropagation();
    e.preventDefault();
    let mm = new Maintenance();
    var form = document.getElementById("init-restore");
    this.processing = true;
    mm.restore(form)
      .then((response) => {
        if (response.type === DataEvent.REQUEST_LAME) {
          self.processing = false;
          notify.alert(response.message, false);
        } else {
          self.processing = false;
          notify.alert(response.message, true);
          setTimeout(() => {
            window.location = "/dashboard";
          }, 1500);
        }
      })
      .catch((err) => {
        self.processing = false;
        notify.alert(err, false);
      });
  }

  handleReset(e) {
    e.stopPropagation();
    e.preventDefault();
    let self = this;
    let mm = new Maintenance();
    if (e.target.id == "get-secret-btn") {
      let data = {
        email: document.getElementById("email").value,
        task: "retrieveSecret"
      };
      this.processing = true;
      mm.secret(data)
        .then((response) => {
          self.processing = false;
          if (response.secret) {
            document.getElementById("secret").value = response.secret;
            notify.alert(response.message, true);
          } else {
            if (response.type == "mailSent") {
              notify.alert(response.message, true);
            } else {
              notify.alert(response.message, false);
            }
          }
        })
        .catch((err) => {
          self.processing = false;
          notify.alert(err, false);
        });
    } else {
      let data = {
        newPass: document.getElementById("new_password").value,
        newPassConfirm: document.getElementById("new_password2").value,
        secret: document.getElementById("secret").value
      };
      mm.newPass(data)
        .then((response) => {
          self.processing = false;
          if (response.type == "passNotCreated") {
            notify.alert(response.message, false);
          } else {
            notify.alert(response.message, true);
            setTimeout(() => {
              window.location = "/dashboard";
            }, 1000);
          }
        })
        .catch((err) => {
          self.processing = false;
          notify.alert(err, false);
        });
    }
  }
  handleOptions(e) {
    e.stopPropagation();
    e.preventDefault();
    let init = document.getElementById("dash-init");
    let restore = document.getElementById("dash-restore");
    if (e.target.id === "init-switch-restore") {
      init.style.display = "none";
      init.style.visibility = "hidden";

      restore.style.display = "flex";
      restore.style.visibility = "visible";
    } else {
      init.style.display = "flex";
      init.style.visibility = "visible";

      restore.style.display = "none";
      restore.style.visibility = "hidden";
    }
  }
}
