/**
 * 默认使用document.cookie存储
 * document.cookie不支持时用localstorage储存
 *
 */
"use strict";
function _instanceof(left, right) {
  if (
    right != null &&
    typeof Symbol !== "undefined" &&
    right[Symbol.hasInstance]
  ) {
    return !!right[Symbol.hasInstance](left);
  } else {
    return left instanceof right;
  }
}
var CookieUtil = {
  get: function get(name) {
    var cookieName = encodeURIComponent(name) + "=",
      cookieStart = document.cookie.indexOf(cookieName),
      cookieValue = null;
    if (cookieStart > -1) {
      var cookieEnd = document.cookie.indexOf(";", cookieStart);
      if (cookieEnd == -1) {
        cookieEnd = document.cookie.length;
      }
      cookieValue = decodeURIComponent(
        document.cookie.substring(cookieStart + cookieName.length, cookieEnd)
      );
    }
    return cookieValue;
  },
  set: function set(name, value) {
    var expires =
      arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
    var path =
      arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
    var domain =
      arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null;
    var secure =
      arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : true;
    var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
    if (_instanceof(expires, Date)) {
      cookieText += "; expires=" + expires.toGMTString();
    }
    if (path) cookieText += ";path=" + path;
    if (domain) cookieText += ";domain=" + domain;
    // if (secure) cookieText += ";secure";
    document.cookie = cookieText + ";max-age=" + 60 * 60 * 24 * 180;
  },
  unset: function unset(name, path, domain, secure) {
    this.set(name, "", new Date(0), path, domain, secure);
  }
};

function yCountdown(cfg) {
  (this.config = $.extend(
    {
      el: $("#y-countdown"),
      hasDay: !1,
      hasHour: !1,
      hasMonth: !1,
      hasYear: !1,
      hasMs: !1,
      flag: !0,
      end: 24,
      loop: !0,
      callback: null,
      endStampName: "endStamp",
      hrsTenthEle: '[data-yCountdown="hrsTenth"]',
      hrsEle: '[data-yCountdown="hrs"]',
      minsTenthEle: '[data-yCountdown="minsTenth"]',
      minsEle: '[data-yCountdown="mins"]',
      secsTenthEle: '[data-yCountdown="secsTenth"]',
      secsEle: '[data-yCountdown="secs"]',
      daysTenthEle: '[data-yCountdown="daysTenth"]',
      daysEle: '[data-yCountdown="days"]',
      msTenthEle: '[data-yCountdown="msTenth"]',
      msEle: '[data-yCountdown="ms"]',
      flagTxt: { d: "DAY", h: "HRS", min: "MINS", s: "SECS", ms: "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\t<p class="txt"></p>\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</div>\n\t\t\t\t' +
        '<i class="d">:</i>\n\t\t\t\t' +
        '<div class="h">\n\t\t\t\t\t<p class="txt"></p>\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</div>\n\t\t\t\t' +
        "<i>:</i>\n\t\t\t\t" +
        '<div class="min">\n\t\t\t\t\t<p class="txt"></p>\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</div>\n\t\t\t\t' +
        "<i>:</i>\n\t\t\t\t" +
        '<div class="s">\n\t\t\t\t\t<p class="txt"></p>\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</div> <i class="ms">:</i><div class="ms"><p class="txt">MS</p><div><span data-ycountdown="msTenth">2</span><span data-ycountdown="ms">7</span></div></div> \n\t\t\t</div></div>\n\t\t\t'
    },
    cfg || {}
  )),
    (this.hours = 0),
    (this.now = 0),
    (this.timer = null);
}
yCountdown.prototype.calcEnding = function () {
  var end = this.config.end;
  if (typeof end == "number") {
    this.hours = end;
    this.endStamp =
      new Date().getTime() + new Date(this.hours * 60 * 60 * 1e3).getTime();
  } else if (
    typeof end == "string" &&
    (end.indexOf("/") > -1 || end.indexOf(",") > -1)
  ) {
    this.endStamp = new Date(end).getTime();
    this.hours = (this.endStamp - new Date().getTime()) / (60 * 60 * 1e3);
    this.config.loop = false;
  }
  if (this.hours > 24 && this.config.hasDay) this.config.hasDay = true;
};

yCountdown.prototype.setStorage = function (key, val) {
  CookieUtil.set(key, val);
};
yCountdown.prototype.getStorage = function (key) {
  return CookieUtil.get(key);
};
yCountdown.prototype.removeStorage = function (key) {
  CookieUtil.unset(key);
};

yCountdown.prototype.calcTimes = function (timeStamp) {
  var times = {};
  times.years = parseInt(timeStamp / (1e3 * 60 * 60 * 24 * 30 * 12));
  times.months = parseInt((timeStamp / (1e3 * 60 * 60 * 24 * 30)) % 12);
  times.days = parseInt(((timeStamp / (1e3 * 60 * 60 * 24)) % 365) % 30);
  if (this.config.hasDay) {
    times.hours = parseInt((timeStamp / (1e3 * 60 * 60)) % 24);
  } else {
    times.hours = parseInt(timeStamp / (1e3 * 60 * 60));
  }
  times.mins = parseInt((timeStamp / (1e3 * 60)) % 60);
  times.secs = parseInt(((timeStamp / 1e3) % 60) % 60);
  times.ms = parseInt((timeStamp % 1e3) / 10);
  return times;
};
yCountdown.prototype.countdown = function () {
  this.now = new Date().getTime();
  var isMoreThan24h = (this.endStamp - this.now) / (1000 * 60 * 60) > 24;
  // 超过24小时即为过期
  var _this$calcTimes = this.calcTimes(
      isMoreThan24h ? 1 : this.endStamp - this.now
    ),
    years = _this$calcTimes.years,
    months = _this$calcTimes.months,
    days = _this$calcTimes.days,
    hours = _this$calcTimes.hours,
    mins = _this$calcTimes.mins,
    secs = _this$calcTimes.secs,
    ms = _this$calcTimes.ms;
  var cfg = this.config;
  var el = cfg.el;
  el.find(cfg.hrsTenthEle).text(hours >= 10 ? parseInt(hours / 10) : "0");
  el.find(cfg.hrsEle).text(hours % 10);
  el.find(cfg.minsTenthEle).text(mins >= 10 ? parseInt(mins / 10) : "0");
  el.find(cfg.minsEle).text(mins % 10);
  el.find(cfg.secsTenthEle).text(secs >= 10 ? parseInt(secs / 10) : "0");
  el.find(cfg.secsEle).text(secs % 10);
  el.find(cfg.msTenthEle).text(ms >= 10 ? parseInt(ms / 10) : "0");
  el.find(cfg.msEle).text(ms % 10);
  if (cfg.hasDay) {
    el.find(cfg.daysTenthEle).text(days >= 10 ? parseInt(days / 10) : "0");
    el.find(cfg.daysEle).text(days % 10);
  }
  if (cfg.hasHour) {
    el.find(cfg.hrsTenthEle).text(hours >= 10 ? parseInt(hours / 10) : "0");
    el.find(cfg.hrsEle).text(hours % 10);
  }
  this.updateEndStamp();
};
yCountdown.prototype.updateEndStamp = function () {
  var cfg = this.config;
  var el = cfg.el;
  if (
    (!this.getStorage(cfg.endStampName) && this.timer !== null) ||
    parseInt(this.getStorage(cfg.endStampName)) > this.endStamp
  ) {
    this.setStorage(cfg.endStampName, this.endStamp);
  } else {
    this.endStamp = parseInt(this.getStorage(cfg.endStampName));

    // 超过24小时即为过期
    if (
      this.now >= this.endStamp ||
      this.endStamp - this.now > 24 * 60 * 60 * 1000
    ) {
      if (cfg.loop) {
        var overTime = this.now - this.endStamp;
        var overHrs = overTime / (1e3 * 60 * 60);
        var needToAddTime = 0;
        if (overHrs < this.hours) {
          needToAddTime = this.hours * 60 * 60 * 1e3;
        } else {
          needToAddTime =
            (parseInt(overHrs / this.hours) + 1) * this.hours * 60 * 60 * 1e3;
        }
        this.endStamp += needToAddTime;
        this.setStorage(cfg.endStampName, this.endStamp);
      } else if (!cfg.loop) {
        clearInterval(this.timer);
        this.timer = null;
        el.find(cfg.hrsTenthEle).text("0");
        el.find(cfg.hrsEle).text("0");
        el.find(cfg.minsTenthEle).text("0");
        el.find(cfg.minsEle).text("0");
        el.find(cfg.secsTenthEle).text("0");
        el.find(cfg.secsEle).text("0");
        if (cfg.hasDay) {
          el.find(cfg.daysTenthEle).text("0");
          el.find(cfg.daysEle).text("0");
        }
        if (cfg.hasMs) {
          el.find(cfg.msTenthEle).text("0");
          el.find(cfg.msEle).text("0");
        }
        if (typeof cfg.callback === "function" && cfg.callback !== undefined) {
          cfg.callback();
        }
      }
    }
  }
};
yCountdown.prototype.handleHtml = function () {
  var config = this.config;
  var _config$flagTxt = config.flagTxt,
    d = _config$flagTxt.d,
    h = _config$flagTxt.h,
    min = _config$flagTxt.min,
    s = _config$flagTxt.s,
    ms = _config$flagTxt.ms;
  if (config.el) {
    $(".yconutdownstyle").length === 0 && $("head").append(config.style);
    document.getElementById(config.el.selector.replace("#", "")).innerHTML =
      config.template;
    if (config.flag) {
      config.hasDay && config.el.find(".d .txt").text(d);
      config.hasHour && config.el.find(".h .txt").text(h);
      config.el.find(".min .txt").text(min);
      config.el.find(".s .txt").text(s);
      config.hasMs && config.el.find(".ms .txt").text(ms);
    }
    !config.hasDay && config.el.find(".d").remove();
    !config.hasHour &&
      config.el.find(".h").remove() &&
      config.el.find(".h-i").remove();
    !config.hasMs &&
      config.el.find(".ms").remove() &&
      config.el.find(".ms-i").remove();
  }
};
yCountdown.prototype.init = function () {
  var _ = this;
  var t = _.config.hasMs ? 50 : 1e3;
  _.calcEnding();
  _.handleHtml();
  _.timer = setInterval(function () {
    _.countdown();
  }, t);
};
