var Admin = {
  beginning: true,
  logged_in: false,
  update_strict_skip: function(value) {
    var form = document.getElementById("adminSettingsForm");
    form.strictSkipNumber = value;
    Admin.submitAdmin(form, false);
  },
  pw: function(msg) {
    Admin.logged_in = msg;
    if (!msg) return;
    w_p = false;
    M.Modal.init(document.getElementById("channel_info"));
    if (Admin.logged_in) {
      Helper.removeClass(".info_change_li", "hide");
      Helper.removeClass("#user_suggests", "hide");
      Helper.removeClass("#user-suggest-html", "hide");
      if (
        Helper.html(".suggested-badge") != "0" &&
        Helper.html(".suggested-badge") != ""
      ) {
        Helper.removeClass(".suggested-badge", "hide");
      }
      if (conf != undefined && conf.strictSkip) {
        Helper.removeClass(".strict-skip-input", "hide");
      }
    } else {
      Admin.hideUserSuggested();
      Helper.addClass(".strict-skip-input", "hide");
    }
    Helper.removeClass(".delete-context-menu", "context-menu-disabled");
    names = [
      "vote",
      "addsongs",
      "longsongs",
      "frontpage",
      "allvideos",
      "removeplay",
      "skip",
      "shuffle",
      "userpass",
      "toggleChat",
      "strictSkip"
    ];
    //Crypt.set_pass(chan.toLowerCase(), Crypt.tmp_pass);
    for (var i = 0; i < names.length; i++) {
      document.getElementsByName(names[i])[0].removeAttribute("disabled");
    }
    Helper.removeClass(".card-action", "hide");
    Helper.addClass("#admin-lock", "clickable");
    document.getElementById("admin-lock").innerHTML = "lock_open";
    if (!Helper.mobilecheck()) {
      Helper.tooltip("#admin-lock", {
        delay: 5,
        position: "left",
        html: "Logout"
      });
    }
    document.getElementById("password").value = "";
    document
      .getElementById("password")
      .setAttribute("placeholder", "Change admin password");
    Helper.removeClass(".user-password-li", "hide");
    Helper.removeClass(".chat-toggle-li", "hide");
    Helper.removeClass(".delete-all", "hide");
    if (document.getElementsByClassName("password_protected")[0].checked) {
      Helper.removeClass(".change_user_pass", "hide");
    }
    if (Helper.html("#admin-lock") != "lock_open") {
      Helper.addClass("#admin-lock", "clickable");
      document.getElementById("admin-lock").innerHTML = "lock_open";
      if (!Helper.mobilecheck()) {
        Helper.tooltip("#admin-lock", {
          delay: 5,
          position: "left",
          html: "Logout"
        });
      }
    }
  },
  hideUserSuggested: function() {
    Helper.addClass("#user_suggests", "hide");
    Helper.addClass("#user-suggest-html", "hide");
    Helper.addClass(".suggested-badge", "hide");
  },
  conf: function(msg) {
    if (msg[0].adminpass == "") {
      ////Crypt.remove_pass(chan.toLowerCase());
    }
    Admin.set_conf(msg[0]);
    if (msg[0].adminpass !== "" && Admin.beginning) {
      //emit("password", {password: Crypt.crypt_pass(Crypt.get_pass(chan.toLowerCase())), channel: chan.toLowerCase()});
      Admin.beginning = false;
    }
  },
  pass_save: function() {
    if (!w_p) {
      //emit('password', {password: Crypt.crypt_pass(CryptoJS.SHA256(document.getElementById("password").value).toString()), channel: chan.toLowerCase(), oldpass: Crypt.crypt_pass(Crypt.get_pass(chan.toLowerCase()))});
      emit("password", {
        password: Crypt.crypt_pass(document.getElementById("password").value),
        channel: chan.toLowerCase()
      });
    } else {
      //emit('password', {password: Crypt.crypt_pass(CryptoJS.SHA256(document.getElementById("password").value).toString()), channel: chan.toLowerCase()});
      emit("password", {
        password: Crypt.crypt_pass(document.getElementById("password").value),
        channel: chan.toLowerCase()
      });
    }
  },
  log_out: function() {
    before_toast();
    /*if(Crypt.get_pass(chan.toLowerCase())) {*/
    //Crypt.remove_pass(chan.toLowerCase());
    if (Admin.logged_in) {
      socket.emit("logout");
      M.toast({ html: "Logged out", displayLength: 4000 });
      Admin.display_logged_out();
    } else {
      M.toast({ html: "Not logged in", displayLength: 4000 });
    }
  },
  display_logged_out: function() {
    Admin.logged_in = false;
    w_p = true;
    adminpass = "";
    names = [
      "vote",
      "addsongs",
      "longsongs",
      "frontpage",
      "allvideos",
      "removeplay",
      "skip",
      "shuffle",
      "toggleChat",
      "strictSkip"
    ];
    document.getElementById("password").value = "";
    Helper.addClass(".info_change_li", "hide");
    for (i = 0; i < names.length; i++) {
      document.getElementsByName(names[i])[0].setAttribute("disabled", true);
    }
    if (Helper.html("#admin-lock") != "lock") {
      if (!Helper.mobilecheck()) {
        Helper.tooltip("#admin-lock", "destroy");
      }
      Helper.removeClass("#admin-lock", "clickable");
      document.getElementById("admin-lock").innerHTML = "lock";
    }
    Helper.addClass(".strict-skip-input", "hide");
    Helper.addClass(".user-password-li", "hide");
    Helper.addClass(".chat-toggle-li", "hide");
    Helper.addClass(".delete-all", "hide");
    if (document.getElementsByClassName("password_protected")[0].checked) {
      Helper.removeClass(".change_user_pass", "hide");
    }
    Helper.addClass(".change_user_pass", "hide");
    Admin.hideUserSuggested();
    Helper.removeClass("#admin-lock", "clickable");
    document
      .getElementById("password")
      .setAttribute("placeholder", "Enter admin password");
    Helper.addClass(".delete-context-menu", "context-menu-disabled");
  },
  save: function(userpass) {
    Admin.submitAdmin(
      document.getElementById("adminSettingsForm").elements,
      userpass
    );
  },
  set_conf: function(conf_array) {
    conf = conf_array;
    music = conf_array.allvideos;
    longsongs = conf_array.longsongs;
    names = [
      "vote",
      "addsongs",
      "longsongs",
      "frontpage",
      "allvideos",
      "removeplay",
      "skip",
      "shuffle",
      "userpass",
      "toggleChat",
      "strictSkip"
    ];
    if (!conf.hasOwnProperty("toggleChat")) conf.toggleChat = true;
    toggleChat = conf.toggleChat;
    hasadmin = conf_array.adminpass != "";
    var show_disabled = true;
    if ((hasadmin && Admin.logged_in) || !hasadmin) {
      show_disabled = false;
    }
    for (var i = 0; i < names.length; i++) {
      document.getElementsByName(names[i])[0].checked =
        conf_array[names[i]] === true;
      if (show_disabled) {
        document
          .getElementsByName(names[i])[0]
          .setAttribute("disabled", show_disabled);
      } else {
        document.getElementsByName(names[i])[0].removeAttribute("disabled");
      }
    }
    document.getElementById("strict-input-number").value =
      conf.strictSkipNumber;
    if (hasadmin && !Admin.logged_in) {
      if (Helper.html("#admin-lock") != "lock") Admin.display_logged_out();
    } else if (!hasadmin) {
      document
        .getElementById("password")
        .setAttribute("placeholder", "Create admin password");
    } else {
      if (document.getElementsByClassName("password_protected")[0].checked) {
        Helper.removeClass(".change_user_pass", "hide");
      }
    }
    if (Admin.logged_in) {
      if (conf != undefined && conf.strictSkip) {
        Helper.removeClass(".strict-skip-input", "hide");
      }
    }
    if (conf != undefined && !conf.strictSkip) {
      Helper.addClass(".strict-skip-input", "hide");
    }
    if (!document.getElementsByClassName("password_protected")[0].checked) {
      Helper.addClass(".change_user_pass", "hide");
      //Crypt.remove_userpass(chan.toLowerCase());
    }
    var updated = false;
    if (conf_array.thumbnail != undefined && conf_array.thumbnail != "") {
      document.getElementById("thumbnail_image").innerHTML =
        "";
      document.getElementById("thumbnail_input").value = conf_array.thumbnail;
      updated = true;
    }
    if (conf_array.description != undefined && conf_array.description != "") {
      document.getElementById("description_area").innerHTML =
        conf_array.description;
      document.getElementById("description_input").value =
        conf_array.description;
      updated = true;
    }
    if (conf_array.rules != undefined && conf_array.rules != "") {
      var existingRules = document.querySelector(".rules-container");
      if (existingRules) existingRules.remove();
      var rules = conf_array.rules.split("\n");
      var elementToAdd =
        "
" + rules[i] + "
"; } elementToAdd += "