var buyLink = {
  preJp: "https://partition.aomei.jp",
  pre: "https://www.diskpart.com", // 中转平台地址
  current: "upgrade-now.html", // 当前弹窗地址，用以前得购买地址
  current2: "upgrade-advanced-features.html",
  ruCurrent: "https://www.aomeitech.com/ru/partners/pa-upgrade-for-syssoft.html"
};

var fn = {
  fnIsOurCustomBrowser: function () {
    if (window.external.CB_IsOurCustomBrowser != null) {
      return true;
    } else {
      return false;
    }
  },

  //
  theParam: function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
  },
  // 获取点击标签中传递过来的参数

  // 获取语言参数
  getLang: function () {
    var langArry = ["en", "de", "fr", "it", "es", "tw", "jp", "ja", "ru"];

    var lang = this.theParam("lang");
    var found = false;

    for (var i = 0; i < langArry.length; i++) {
      if (langArry[i] === lang) {
        found = true;
        break;
      }
    }

    if (found) {
      lang === "ja" && (lang = "jp");
    } else {
      lang = "en";
    }

    return lang;
  },

  accordion: function () {
    // 普通
    $(".accordion li h6").click(function () {
      $(this).next().show().parents("li").addClass("active");
      $(this)
        .parent()
        .siblings()
        .find("p")
        .hide()
        .parents("li")
        .removeClass("active");
    });

    // 表格
  },

  setPopurl: function () {
    var popurlLang = fn.getLang().match(/jp|en/)
      ? "/"
      : "/" + fn.getLang() + "/";
    var trackPage =
      this.theParam("source") === "create-portable-version"
        ? buyLink.current2
        : buyLink.current;
    var pre = fn.getLang() === "jp" ? buyLink.preJp : buyLink.pre;
    var popurl = pre + popurlLang + trackPage + window.location.search;
    if (fn.getLang() === "ru") {
      popurl = buyLink.ruCurrent + window.location.search;
    }
    return "&popurl=" + encodeURIComponent(popurl);
  },

  tableEvent: function () {
    // 展开收起
    $(".target").click(function () {
      $(this)
        .parents("tr")
        .siblings()
        .find(".target")
        .removeClass("active")
        .find("p")
        .hide();
      // .stop()
      // .slideUp(100);
      $(this).addClass("active").find("p").show();
      //   .stop().slideDown();
    });
    // hover高亮
    $("tbody tr").hover(
      function () {
        $(this).addClass("hover");
      },
      function () {
        $(this).removeClass("hover");
      }
    );
  },
  // 操作
  elActions: function () {
    $("[el-action]").click(function () {
      var actionInitial = $(this).attr("el-action").split("-");
      var actionType = actionInitial[0];
      var action = actionInitial[1];
      var $target = $('[el-name="' + action + '"]');
      if (actionType === "show") {
        switch (action) {
          case "bsns":
            // $target.animate({ right: 0 }, 300);
            $target.animate(
              {
                top: 0
              },
              1000
            );
            $('[el-name="pro"]').animate(
              {
                opacity: 0
              },
              300,
              function () {
                $(this).hide(); // 动画完成后完全隐藏
              }
            );
            break;
          case "fetrs":
            $target.animate(
              {
                top: 0
              },
              300
            );
            $(".close").hide();
            break;
        }
      } else {
        switch (action) {
          case "bsns":
            // $target.animate({ right: "-1000px" }, 300);
            $target.animate(
              {
                top: "-800px"
              },
              300
            );
            $('[el-name="pro"]').animate(
              {
                opacity: 1
              },
              500,
              function () {
                $(this).show(); // 动画完成后完全隐藏
              }
            );
            break;
          case "fetrs":
            $target.animate(
              {
                top: "-800px"
              },
              300
            );
            $(".close").show();
            break;
        }
      }
    });
  },
  // 构造购买链接
  buildupBuyLink: function (original) {
    var searchPopurl = fn.setPopurl();
    var pre = this.getLang() === "jp" ? buyLink.preJp : buyLink.pre;
    var link =
      this.getLang() === "ru"
        ? buyLink.ruCurrent + "?" + searchPopurl.slice(1)
        : pre + "/cart?" + original + searchPopurl;
    return link;
  },
  // 渲染模版
  renderHtml: function () {
    var theTag = this.theParam("source");
    var theLang = this.getLang();
    if (theLang === "ja") {
      theLang = "jp";
    }

    var renderData = obj;
    renderData.lang = theLang;
    $("body").addClass(theLang);
    $("body").addClass(theTag);

    renderData.lang = theLang; // 语言
    renderData.buyLinkPre =
      fn.getLang() === "jp" ? buyLink.preJp + "/cart?" : buyLink.pre + "/cart?";
    renderData.popurl = fn.setPopurl();

    $("#myTmpl").tmpl(renderData).appendTo(".main-wrap");
    // this.countdown(renderData);
  },
  clickProduct: function () {
    $(".prd-it").click(function () {
      var $this = $(this);
      $this.parents(".prd-ls").find(".prd-it").removeClass("active");
      $this.addClass("active");
    });
  },
  handleClick2: function () {
    $(".prd-ls").each(function (i, item) {
      $(item).find(".prd-it").eq(1).trigger("click");
    });
  },
  goLicenceCode: function () {
    $('[data-go="licenceCode"]').click(function () {
      if (fn.fnIsOurCustomBrowser()) {
        window.external.CB_OpenURL("enter licence code");
      } else {
        chrome.webview.postMessage("enter licence code");
      }
    });
  },

  // 获取链接里的产品参数
  extractPkNumbers: function (url) {
    var pkNumbers = "";
    var queryString = url.split("?")[1]; // 获取查询字符串部分

    if (queryString) {
      var params = queryString.split("&"); // 分割查询字符串成键值对数组

      for (var i = 0; i < params.length; i++) {
        var param = params[i].split("="); // 分割每个键值对
        var key = param[0];
        var match = key.match(/pk\[(\d+)\]/);

        if (match) {
          pkNumbers = match[1];
        }
      }
    }

    return pkNumbers;
  },
  // 是否参加更换购买平台测试
  isReplacePlatform: function (url) {
    var productId = this.extractPkNumbers(url);
    var isProductId = /230|397|398|232|233/.test(productId);
    var isLang = fn.getLang() === "de";
    // var isSource = /register|install-complete|install/.test(this.theParam("source")); // install这个参数不知道有没有，需求给的就添加上了
    if (isProductId || isLang) {
      // 不参加更换购买平台因素
      return false;
    } else {
      return true;
    }
  },
  // 更换购买平台
  replaceBuyPlatform: function (url) {
    if (this.isReplacePlatform(url)) {
      url = url.replace("ep=cb", "ep=vf").replace("ep=si&an=1", "ep=vf");
    }
    return url;
  },
  // 重新设置窗口大小
  setWindowSize: function () {
    setTimeout(function () {
      var mainEle = $(".main-wrap");
      var width = mainEle.outerWidth();
      var height = mainEle.outerHeight();
      // console.log(width, height);
      if (fn.fnIsOurCustomBrowser()) {
        window.external.CB_OpenURL("resize:" + width + "," + height);
      } else {
        chrome.webview.postMessage("resize:" + width + "," + height);
      }
    }, 100);
  }
};
$(function () {
  // 引入对应js
  var scriptElement = document.createElement("script");
  scriptElement.type = "text/javascript";
  scriptElement.src = "scripts/std-special" + "/" + fn.getLang() + ".js";

  function init() {
    fn.renderHtml();
    fn.accordion();
    fn.clickProduct();
    fn.tableEvent();
    fn.elActions();
    fn.handleClick2();
    fn.goLicenceCode();
    fn.setWindowSize();

    $(".btn-org").click(function () {
      var btnLink = fn.replaceBuyPlatform($(this).attr("data-href"));
      var hash = "#" + $(this).attr("data-num");
      if (fn.fnIsOurCustomBrowser()) {
        window.external.CB_OpenURL(btnLink + hash);
      } else {
        chrome.webview.postMessage("openurl:" + btnLink + hash);
        chrome.webview.postMessage("resize:1020,702");
      }
    });
    $(".close").click(function () {
      if (fn.fnIsOurCustomBrowser()) {
        window.external.CB_OpenURL("close");
      } else {
        chrome.webview.postMessage("close");
      }
    });
  }
  // 兼容低版本浏览器
  if (scriptElement.readyState) {
    // 仅适用于IE
    scriptElement.onreadystatechange = function () {
      if (
        scriptElement.readyState === "loaded" ||
        scriptElement.readyState === "complete"
      ) {
        scriptElement.onreadystatechange = null;
        // 脚本加载完成后的操作
        init();
      }
    };
  } else {
    // 其他浏览器
    scriptElement.onload = function () {
      // 脚本加载完成后的操作
      init();
    };
  }
  // window.localStorage.clear();
  document.getElementsByTagName("head")[0].appendChild(scriptElement);
});
