import dayjs from 'dayjs';
import dataDictionary from './dataDictionary';

let utils = {
  /**
   * 参数处理
   */
  tansParams: (params: any) => {
    let result = '';
    for (const propName of Object.keys(params)) {
      const value = params[propName];
      const part = encodeURIComponent(propName) + '=';
      if (value !== null && value !== '' && typeof value !== 'undefined') {
        if (typeof value === 'object') {
          for (const key of Object.keys(value)) {
            if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
              const params = propName + '[' + key + ']';
              const subPart = encodeURIComponent(params) + '=';
              result += subPart + encodeURIComponent(value[key]) + '&';
            }
          }
        } else {
          result += part + encodeURIComponent(value) + '&';
        }
      }
    }
    return result;
  },

  /**
   * 参数处理
   */
  getProject: (data: any, type: number) => {
    //格式化数据
    let list = [];
    for (let key in data) {
      // let arrList = key.split('@@');
      let newArr = key.split('_');
      newArr.push(data[key]);
      list.push(newArr);
    }
    // console.log("list",list)
    //重组数组
    let newList = [];
    let project: any = dataDictionary.project;
    for (let key in project) {
      let area: any = [];
      list.forEach((item) => {
        if (key == item[0]) {
          let arrList = item[1].split('@@');
          area.push({
            key: item[1],
            name: arrList[0],
            value: item[2],
          });
        }
      });
      let obj: any = {
        key: key,
        name: project[key],
        area: area
      };
      newList.push(obj);
    }
    return newList;
  },

  /**
   * 秒转为时间
   */
  timeFormat: (val: number) => {
    let h = parseInt(((val / 60 / 60) % 24).toString());
    let m = parseInt(((val / 60) % 60).toString());
    let s = parseInt((val % 60).toString());
    if (h > 0) {
      m = h * 60 + m;
    }
    return `${m}:${s}`;
  },

  // 时间戳转为日期
  timestampFormat: (data: any, format: any) => {
    let formatData =  format || 'YYYY-MM-DD HH:mm:ss';
    if(data){
      if(data.toString().length == 10){
        return dayjs(data * 1000).format(formatData)
      }else{
        return dayjs(data).format(formatData)
      }
    }else{
      return '';
    }
  },

  /**
   * 跑步成绩格式化
   */
  runTime: (times: any, noMin: boolean = false, type: boolean, lang: any = 0) => {
    if (times != undefined && times.toString().indexOf('.') != -1) {
      times = Math.round((Number(times) + Number.EPSILON) * 100) / 100;
    }
    if (type) {
      if (times <= 0) {
        return '0分0秒';
      }
      // let isMinus = false
      if (isNaN && isNaN(times)) {
        times = 0;
      }
      if (times === -1) {
        return '-';
      }
      if (times < 0) {
        times = Math.abs(times);
        // isMinus = true
      }
      if (noMin) {
        // dayjs SS格式化后SS保持原样,  所以直接用SSS格式化后.slice(0, -1)去掉最后一位
        return dayjs(+times * 1000)
          .format('ss″SSS')
          .slice(0, -1);
      }
      //console.log(times, "runTimerunTime");
      if (times >= 3600) {
        return (
          Math.floor(times / 3600) +
          '' +
          dayjs(+times * 1000)
            .format('mm:ss.SSS')
            .slice(0, -1)
        );
      } else if (times >= 60) {
        return dayjs(+times * 1000)
          .format('m分s秒 ')
          .slice(0, -1);
      } else {
        return dayjs(+times * 1000)
          .format('m分s秒 ')
          .slice(0, -1);
      }
    } else {
      if (times <= 0) {
        if (noMin) {
          return '00″00';
        } else {
          return '00′00″00';
        }
      }
      // let isMinus = false
      if (isNaN && isNaN(times)) {
        times = 0;
      }
      if (times === -1) {
        return '-';
      }
      if (times < 0) {
        times = Math.abs(times);
        // isMinus = true
      }
      if (noMin) {
        // dayjs SS格式化后SS保持原样,  所以直接用SSS格式化后.slice(0, -1)去掉最后一位
        return dayjs(+times * 1000)
          .format('ss″SSS')
          .slice(0, -1);
      }
      //console.log(times, "runTimerunTime");
      if (times >= 3600) {
        return (
          Math.floor(times / 3600) +
          '' +
          dayjs(+times * 1000)
            .format('mm:ss.SSS')
            .slice(0, -1)
        );
      } else if (times >= 60) {
        return dayjs(+times * 1000)
          .format('mm′ss″SSS')
          .slice(0, -1);
      } else {
        if (lang == 1) {
          return dayjs(+times * 1000)
            .format('ss秒SSS')
            .slice(0, -1);
        } else {
          return dayjs(+times * 1000)
            .format('ss″SSS')
            .slice(0, -1);
        }
      }
    }
  },

  // 当前时间
  getDate: () => {
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth();
    let day = now.getDate();
    let hours = now.getHours();
    let minutes = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
    let seconds = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();
    month = month + 1;
    //let week;
    //let arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    //week = arr_week[now.getDay()];
    let time = year + '-' + month + '-' + day + ' ' + ' ' + hours + ':' + minutes + ':' + seconds;
    return time;
  },

  // 全屏
  fullScreen: () => {
    if (document.fullscreenElement) {
      document.exitFullscreen();
    }
    document.body.requestFullscreen();
  },

  // 设置收藏夹图标
  setFavicon: (url?:any) => {
    let dom = document.querySelectorAll('link');
    for (let i = 0; i < dom.length; i++) {
      if (dom[i].rel === 'icon') {
        dom[i].href = url || ''
      }
    }
  }
};
export default utils;
