text: $("#SEF-cancel-btn").on("click", function() {
      window.location.replace("./index.html");
  });
  
  $("#btn-import-key").on("click", function (e) {
      e.preventDefault();
    
      const pvtFile = document.getElementById("file-pvt-key").files[0];
      console.log(pvtFile);
      const pubFile = document.getElementById("file-pub-key").files[0];
      console.log(pubFile);
    
      const _files = [pvtFile, pubFile].map((file) => {
        return {
          name: file.name,
          path: file.path,
        };
      });
    
      window.api.copyFiles(_files).then(window.api.reloadKeys).catch(alert);
  });
  
  $("#sel-keys")
    .on("change", () => {
      const keyFile =
        savedKeys.find((key) => key.name == $("#sel-keys").val());
      if (!keyFile) {
        $("#txt-key-id").text("Key information");
        $("#txt-key-email").text(null);
        $("#txt-key-fp").text(null);
        return;
      }
      console.log(keyFile);
  
      const keyType = getKeyType(keyFile);
      if (keyType < 0) {
        alert(
          "Unknown key file format. Please use *.key for binary or *.asc for armored ASCII"
        );
        return;
      }
  
      window.api.key
        .readKeyFile(keyFile.path, keyType == 0)
        .then((privateKey) => {
          console.log(privateKey);
          const userInfo = privateKey.users[0].userId;
          $("#txt-key-id").text(userInfo.name);
          $("#txt-key-email").text(userInfo.email);
          const fingerprint = privateKey.keyPacket.fingerprint;
          const fpr = toHex(fingerprint).toUpperCase();
          $("#txt-key-fp").text(fpr.slice(0, 4) + ' ' + fpr.slice(4, 8) + ' ' + fpr.slice(8, 12) + ' ' + fpr.slice(12, 16) + ' ' + fpr.slice(16, 20) + ' ' + fpr.slice(20, 24) + ' ' + fpr.slice(24, 28) + ' ' + fpr.slice(28, 32) + ' ' + fpr.slice(32, 36) + ' ' + fpr.slice(36));
        })
        .catch(alert);
    });
  
  $("#btn-export-key").on("click", (e) => {
    e.preventDefault();
  
    const keyFile = savedKeys.find((key) => key.name == $("#sel-keys").val());
    if (!keyFile) {
      alert("Please select a key pair from list");
      return;
    }
  
    window.api.downloadPublicKey(keyFile.name);
  });
  
  function getKeyType(keyFile) {
    let extension = keyFile.name.split(".").reverse()[0];
    console.log(extension);
    if (extension == "key") {
      console.log("binary");
      return 0;
    } else if (extension == "asc") {
      console.log("ascii");
      return 1;
    }
    return -1;
  }
  
  function bindKeys(files = []) {
    $("#sel-keys").empty();
    $("#sel-keys").append(new Option("Select a key.."));
    savedKeys = files
      .filter((file) => new RegExp("private|pvt").test(file.name))
      .map(fileToKey);
    savedKeys.forEach((key) => {
      $("#sel-keys").append(new Option(key.title, key.name));
    });
  }
  
  function fileToKey(file) {
    let fileElements = file.name.split("-");
    fileElements.pop();
    return {
      title: fileElements.join(" "),
      name: file.name,
      path: file.path,
    };
  }
  
  let savedKeys = [];
  let lastPlainMessage;
  let lastEncryptedMessage;
  
  window.api.onReloadKeys((args) => {
    console.log("on Reload Keys");
    window.api.listKeys().then(bindKeys).catch(alert);
  });
  
  (() => {
    window.api.listKeys().then(bindKeys).catch(alert);
  })();
  
  function toHex(buffer) {
    return Array.prototype.map
      .call(buffer, (x) => ("00" + x.toString(16)).slice(-2))
      .join("");
  }
  