import {hex_sha1} from '../util/des'

var qs = require('qs');

export default {
  /**
   * 设置cookie
   * @param name
   * @param value
   * @param iDay
   */
  setCookie: function (name, value, time, domain) {
    domain = domain ? ";domain=" + domain : "";
    var Days = time;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + encodeURIComponent(value) + ";expires=" + exp.toGMTString() + ";path=/" + domain;
    return true;
  },
  /**
   * 获取cookie
   * @param name
   * @returns {*}
   */
  getCookie: function (name) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for (var i = 0; i < arrCookie.length; i++) {
      var arr = arrCookie[i].split("=");
      if (name == arr[0]) {
        return arr[1];
      }
    }
    return null;
  },
  /**
   * 删除cookie
   * @param name
   */
  delCookie: function (name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = this.getCookie(name);
    if (cval != null)
      document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  },
  /**
   * 加密token
   */
  getParams(params) {
    var Yo4teW_gid = this.getCookie('Yo4teW_gid');
    var cache_time = this.getCookie('cache_time');
    
    if (cache_time != null) {
      var qwertyuiop = Math.floor((new Date().getTime()) / 1000);
    } else {
      var qwertyuiop = Math.floor((new Date().getTime()) / 1000) + (window.fwtimes);
      this.setCookie('cache_time', window.fwtimes, 1, window.cookieHostname);
    }
    
    var abb = [];
    var abbo = {};
    var asdfghjkl = '';
    var appx = '';
    var aggx = '';
    
    var as = {
      pf: 2,
      qwertyuiop: qwertyuiop,
      Yo4teW_gid: Yo4teW_gid
    }
    
    var obj = Object.assign({}, as, params);
    
    Object.keys(obj).forEach(function (value, index, array) {
      abb.push(value);
    });
    
    abb = abb.sort();
    
    abb.forEach(function (value, index, array) {
      abbo[abb[index]] = obj[abb[index]]
    })
    
    Object.keys(abbo).forEach(function (value, index, array) {
      asdfghjkl += value + '=' + abbo[value]
    });
    
    appx = asdfghjkl.replace(/[^0-9a-zA-Z]/g, '');
    appx = appx.toUpperCase();
    
    for (var i = 0; i < appx.length; i++) {
      if (appx[i] != "A" && appx[i] != "B" && appx[i] != "C") {
        aggx += appx[i]
      }
    }
    
    asdfghjkl = hex_sha1(aggx);
    
    var parameter = Object.assign({}, params, {asdfghjkl: asdfghjkl}, {qwertyuiop: qwertyuiop}, {pf: 2});
    
    return qs.stringify(parameter);
    
  },
  /**
   * *
   * 区分强弱登录态
   * *
   * /
   */
  isLogin(){
    var state_=0;
    if(this.getCookie("Yo4teW_uid")){
      state_= 2;//强弱登录态
      if(this.getCookie("Yo4teW_skey")){
        state_=1;//强登录态
      }
    }
    return state_
  },
  /**
   * *
   * 需要登录态的页面强行跳到登录页面然后回跳回来
   * *
   * /
   */
  loginBack:function(){

  },
  /**
   * *
   * 数组排序
   * *
   * /
   */
  compare(property,state){
    return function(a,b){
      var value1 = a[property];
      var value2 = b[property];
      if(state){
        return value1 - value2;
      }else{
        return value2 - value1;
      }
    }
  },
  /**
   * *
   * 数组去重复
   * *
   * /
   */
  distinct(array){
    var arr = array,
        result = [],i,j,len = arr.length;
    for(i = 0; i < len; i++){
      for(j = i + 1; j < len; j++){
        if(arr[i] === arr[j]){
          j = ++i;
        }
      }
      result.push(arr[i]);
    }
    return result;
  },
  /**
   * *
   * 数字解密
   * *
   * /
   */
  aggxde(str){
    if (str && str.indexOf("font") != -1) {
      var sff = (str.split('class="'));
      sff.shift();
      var numb = "";
      var listb = [
        ['asfgdqwer', 'asfgdtyhg', 'asfgdpolk', 'asfgdpoqw'],
        ['asfgdrfdf', 'asfgderfd', 'asfgdwdsa', 'asfgdpoer'],
        ['asfgdasde', 'asfgdqwsz', 'asfgdrtgd', 'asfgdpovv'],
        ['asfgdwsxc', 'asfgdwsxz', 'asfgdrfvb', 'asfgdpoee'],
        ['asfgdqazs', 'asfgdqasd', 'asfgdqwag', 'asfgdpogh'],
        ['asfgdrtyh', 'asfgdyutr', 'asfgdeews', 'asfgdpotg'],
        ['asfgdpluj', 'asfgdikjf', 'asfgdesgj', 'asfgdpfff'],
        ['asfgdtrdb', 'asfgdiksf', 'asfgdsgkp', 'asfgdprty'],
        ['asfgdpehl', 'asfgdstgb', 'asfgderll', 'asfgdpokf'],
        ['asfgdpehg', 'asfgdstgf', 'asfgderlf', 'asfgdpogk']
      ];
      for (var i = 0; i < sff.length; i++) {
        var ghjk = sff[i].split(" ")[0];
        for (var j = 0; j < listb.length; j++) {
          if (listb[j].indexOf(ghjk) != -1) {
            numb += j;
          }
        }
      }
      return Number(numb)
    }
  }

}