/*
 * Description  :
 * Date         :2025-03-14 10:41:16
 * LastEditors  :Xuwei
 * LastEditTime :2025-03-14 16:28:36
 * FilePath     :\upgrade-now\scripts\renew.js
 */
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;
  },

  // 获取点击标签中传递过来的参数
  getTag: function () {
    var _this = this;
    return _this.theParam("source") || "toolbar";
  },

  // 获取语言参数
  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;
  },

  // 转化语言键值
  getKey: function (o, lang) {
    return o.hasOwnProperty(lang) ? lang : "en";
  },

  setPopurl: function () {
    var popurlLang = fn.getLang().match(/jp|en/)
      ? "/"
      : "/" + fn.getLang() + "/";
    var trackPage =
      this.getTag() === "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);
  },
  // 辅助函数：将数字转换为两位数的字符串，添加前导零
  formatTwoDigits: function (num) {
    return num < 10 ? "0" + num : num;
  },
  endTime: function (inputDate) {
    if (!inputDate) {
      return 24;
    }
    var timestamp = new Date(parseInt(inputDate)).getTime();
    //  var newTimestamp = timestamp + 24 * 60 * 60 * 1000;
    var newTimestamp = timestamp;
    var newDate = new Date(newTimestamp);

    // 格式化日期和时间部分为两位数
    var formattedDate =
      newDate.getFullYear() +
      "/" +
      this.formatTwoDigits(newDate.getMonth() + 1) +
      "/" +
      this.formatTwoDigits(newDate.getDate()) +
      " " +
      this.formatTwoDigits(newDate.getHours()) +
      ":" +
      this.formatTwoDigits(newDate.getMinutes());

    return formattedDate;
  },

  countdown: function (renderData) {
    var _this = this;
    var cdt = this.theParam("cdt");
    var nowTime = new Date().getTime();
    var isMoreThan24h = cdt - nowTime > 24 * 60 * 60 * 1000;
    // 传入的倒计时小于当前时间，不开启倒计时
    if (!cdt || parseInt(cdt) <= nowTime || isMoreThan24h) {
      timeout();
      return true;
    }

    function timeout() {
      //   console.log("倒计时结束");
      $(".main-wrap").addClass("timeout");
      // 倒计时结束改变产品价格
    }
    var endStampName = "std-cbn-upgrade";
    CookieUtil.unset(endStampName);
    var cd = new yCountdown({
      el: $("#pa-special-countDown"),
      end: _this.endTime(cdt),
      loop: false,
      flag: true,
      hasDay: false,
      hasHour: true,
      hasMs: false,
      endStampName: endStampName,
      flagTxt: {
        d: "",
        h: obj.hour,
        min: obj.min,
        s: obj.sec,
        ms: ""
      },
      template:
        '\n\t\t\t<div class="countdown" data-ycountdown="yCountdown">\n\t\t\t\t<div class="d">\n\t\t\t\t<div>\n\t\t\t\t\t\t<span data-ycountdown="daysTenth">0</span><span data-ycountdown="days">0</span>\n\t\t\t\t\t</div>\n\t\t\t\t\t<p class="txt"></p>\n\t\t\t\t\t</div>\n\t\t\t\t' +
        '<i class="d">:</i>\n\t\t\t\t' +
        '<div class="h">\n\t\t\t\t<div>\n\t\t\t\t\t\t<span data-ycountdown="hrsTenth">0</span><span data-ycountdown="hrs">0</span>\n\t\t\t\t\t</div>\n\t\t\t\t\t<p class="txt"></p>\n\t\t\t\t\t</div>\n\t\t\t\t' +
        '<i class="h-i">:</i>\n\t\t\t\t' +
        '<div class="min">\n\t\t\t\t<div>\n\t\t\t\t\t\t<span data-ycountdown="minsTenth">0</span><span data-ycountdown="mins">0</span>\n\t\t\t\t\t</div>\n\t\t\t\t\t<p class="txt"></p>\n\t\t\t\t</div>\n\t\t\t\t' +
        "<i>:</i>\n\t\t\t\t" +
        '<div class="s">\n\t\t\t\t<div>\t\n\t\t\t\t\t\t<span data-ycountdown="secsTenth">0</span><span data-ycountdown="secs">0</span>\n\t\t\t\t\t</div>\n\t\t\t\t\t<p class="txt"></p>\n\t\t\t\t\t</div>\n\t\t\t' +
        '<i class="ms"> : </i>' +
        '<div class="ms"><div><span data-ycountdown="msTenth">0</span><span data-ycountdown="ms">0</span><span data-ycountdown="ms">0</span></div>\n\t\t\t\t\t<p class="txt"></p>\n\t\t\t\t\t</div>\n\t\t\t' +
        "</div></div>\n\t\t\t",
      callback: timeout
    });
    cd.init();
  },

  // 渲染模版
  renderHtml: function () {
    var theTag = this.getTag();
    var theLang = this.getLang();
    if (theLang === "ja") {
      theLang = "jp";
    }
    var renderData = obj;

    renderData.lang = theLang;

    $("body").addClass(theLang);
    $("body").addClass(theTag);

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

    $("#myTmpl").tmpl(renderData).appendTo(".main-wrap");
    this.countdown(renderData);
  },

  // 深拷贝
  deepClone: function (obj) {
    // 如果是 null 或非对象类型，直接返回
    if (obj === null || typeof obj !== "object") {
      return obj;
    }

    var clone;

    // 检查对象的类型
    if (obj instanceof Array) {
      // 如果是数组，初始化一个新数组
      clone = [];
      for (var i = 0; i < obj.length; i++) {
        clone[i] = this.deepClone(obj[i]); // 递归深拷贝每个元素
      }
    } else {
      // 如果是对象，初始化一个新对象
      clone = {};
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          clone[key] = this.deepClone(obj[key]); // 递归深拷贝每个属性
        }
      }
    }

    return clone;
  },
  // 重新设置窗口大小
  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/special" + "/" + fn.getLang() + ".js";

  function init() {
    fn.renderHtml();
    fn.setWindowSize();

    $(".btn-org").click(function () {
      var btnLink = $(this).attr("data-href");
      if (fn.fnIsOurCustomBrowser()) {
        window.external.CB_OpenURL(btnLink);
      } else {
        chrome.webview.postMessage("openurl:" + btnLink);
      }
    });
    $(".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);
});
