/**
 * 通用工具类
 */
export default class func {
  /**
   * 不为空
   * @param val
   * @returns {boolean}
   */
  static notEmpty(val) {
    return !this.isEmpty(val);
  }

  /**
   * 是否为定义
   * @param val
   * @returns {boolean}
   */
  static isUndefined(val) {
    return val === null || typeof val === 'undefined';
  }

  /**
   * 为空
   * @param val
   * @returns {boolean}
   */
  static isEmpty(val) {
    if (
      val === null ||
      typeof val === 'undefined' ||
      (typeof val === 'string' && val === '' && val !== 'undefined')
    ) {
      return true;
    }
    return false;
  }

  /**
   * 强转int型
   * @param val
   * @param defaultValue
   * @returns {number}
   */
  static toInt(val, defaultValue) {
    if (this.isEmpty(val)) {
      return defaultValue === undefined ? -1 : defaultValue;
    }
    const num = parseInt(val, 0);
    return Number.isNaN(num) ? (defaultValue === undefined ? -1 : defaultValue) : num;
  }

  /**
   * Json强转为Form类型
   * @param obj
   * @returns {FormData}
   */
  static toFormData(obj) {
    const data = new FormData();
    Object.keys(obj).forEach(key => {
      data.append(key, Array.isArray(obj[key]) ? obj[key].join(',') : obj[key]);
    });
    return data;
  }

  /**
   * date类转为字符串格式
   * @param date
   * @param format
   * @returns {null}
   */
  static format(date, format = 'YYYY-MM-DD HH:mm:ss') {
    return date ? date.format(format) : null;
  }

  /**
   * data类格式化
   * @param timestamp
   * @returns {string}
   */
  static formatDateTime(timestamp) {
    return this.formatDate(new Date(timestamp));
  }

  /**
   * data类格式化
   * @param date
   * @returns {string}
   */
  static formatDate(date) {
    const pad = (num) => (num < 10 ? '0' + num : num);

    const year = date.getFullYear();
    const month = pad(date.getMonth() + 1);  // 月份从0开始，所以+1
    const day = pad(date.getDate());
    const hour = pad(date.getHours());
    const minute = pad(date.getMinutes());
    const second = pad(date.getSeconds());

    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  }

  /**
   * 格式化时区解决时间差问题
   * @param datetime
   * @returns {string}
   */
  static toLocalISOString(datetime) {
    let timezoneOffset = datetime.getTimezoneOffset() * 60000; // 获取当前时区与UTC的时间差（以毫秒为单位）
    let localDatetime = new Date(datetime - timezoneOffset); // 调整时间，得到当前时区时间
    return localDatetime.toISOString();
  }

  /**
   * 根据逗号联合
   * @param arr
   * @returns {string}
   */
  static join(arr) {
    return Array.isArray(arr) ? arr.join(',') : arr;
  }

  /**
   * 根据逗号分隔
   * @param str
   * @returns {string}
   */
  static split(str) {
    return str ? String(str).split(',') : '';
  }

  /**
   * 转换空字符串
   * @param str
   * @returns {string|*}
   */
  static toStr(str) {
    if (typeof str === 'undefined' || str === null) {
      return '';
    }
    return str;
  }

  /**
   * 判断是否为数组
   * @param param
   * @returns {boolean}
   */
  static isArrayAndNotEmpty(param) {
    return Array.isArray(param) && param.length > 0;
  }

  /**
   * 格式化URL
   * @param url
   * @returns {*|string}
   */
  static formatUrl(url) {
    if (!url) return url;
    if (url.startsWith('http://') || url.startsWith('https://')) {
      return url;
    } else {
      return `http://${url}`;
    }
  }

  /**
   * bytes转换为kb单位
   * @param bytes
   * @returns {string}
   */
  static bytesToKB(bytes) {
    const kb = bytes / 1024;
    return kb.toFixed(2);
  }
  
  /**
   * json数组转换成key value字符串
   * @param jsonArray "[{enumKey: 'key', enumValue: 'value'}]"
   * @returns {*}
   */
  static jsonArrayToKeyValue(jsonArray) {
    if (this.isEmpty(jsonArray)) {
      return '';
    }
    return jsonArray.map(item => `${item.enumKey}:${item.enumValue}`).join(';');
  }

  /**
   * key value字符串转换成json数组
   * @param keyValue key:value;key:value
   * @returns {*[]}
   */
  static keyValueToJsonArray(keyValue) {
    if (this.isEmpty(keyValue)) {
      return [];
    }
    return keyValue.split(';').map((kv, index) => {
      const [key, value] = kv.split(':');
      return {
        id: index,
        enumKey: key,
        enumValue: value
      };
    });
  }

  /**
   * 生成随机字符串
   * @param length 长度
   * @returns {string}
   */
  static strGenerate(length) {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const maxLength = 256;
    if (length > maxLength) {
      throw new Error(`长度最大值不能超过 ${maxLength}`);
    }

    return Array.from({length}, () => characters.charAt(Math.floor(Math.random() * characters.length))).join('');
  }

  /**
   * 生成UUID
   * @returns {string}
   */
  static generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    });
  }

   /**
   * 将列表数据放大多少倍
   * @returns {string}
   */
   static zoomListNum(list,scale) {
    let result = [];
    list.forEach(item=>{
      result.push(parseFloat(item*scale).toFixed(3));
    })
    return result;
  }

    /**
   * 将列表数据放大多少倍
   * @returns {string}
   */
    static zoomNum(num,scale,fix) {
      if(num==-1){
        return "-";
      }
      if(!num){
        return 0;
      }
      if(!fix){
        fix = 3;
      }
      return (parseFloat(num*scale).toFixed(fix));
    }
  
  static getAdCode(proviceName){
    var adcodes = [{"adcode":100000,"name":"全国"},{"adcode":110000,"name":"北京市"},{"adcode":120000,"name":"天津市"},{"adcode":130000,"name":"河北省"},{"adcode":140000,"name":"山西省"},{"adcode":150000,"name":"内蒙古自治区"},{"adcode":210000,"name":"辽宁省"},{"adcode":220000,"name":"吉林省"},{"adcode":230000,"name":"黑龙江省"},{"adcode":310000,"name":"上海市"},{"adcode":320000,"name":"江苏省"},{"adcode":330000,"name":"浙江省"},{"adcode":340000,"name":"安徽省"},{"adcode":350000,"name":"福建省"},{"adcode":360000,"name":"江西省"},{"adcode":370000,"name":"山东省"},{"adcode":410000,"name":"河南省"},{"adcode":420000,"name":"湖北省"},{"adcode":430000,"name":"湖南省"},{"adcode":440000,"name":"广东省"},{"adcode":450000,"name":"广西壮族自治区"},{"adcode":460000,"name":"海南省"},{"adcode":500000,"name":"重庆市"},{"adcode":510000,"name":"四川省"},{"adcode":520000,"name":"贵州省"},{"adcode":530000,"name":"云南省"},{"adcode":540000,"name":"西藏自治区"},{"adcode":610000,"name":"陕西省"},{"adcode":620000,"name":"甘肃省"},{"adcode":630000,"name":"青海省"},{"adcode":640000,"name":"宁夏回族自治区"},{"adcode":650000,"name":"新疆维吾尔自治区"},{"adcode":710000, "name": "台湾省"},{"adcode":810000,"name":"香港特别行政区"},{"adcode":820000,"name":"澳门特别行政区"}]
    let result = "未查询到";
    console.log(proviceName)
    adcodes.forEach(item=>{

      if(proviceName == item.name){
        console.log(item)
        result =  item.adcode;
      }
    })
    return result;
  }

  /**
   * 通过省获取省会的坐标地址
   * @param proviceName
   * @returns {坐标}
   */
  static getProvicePosition(proviceName) {

    if(proviceName.indexOf("省") !== -1){
      proviceName = proviceName.replace("省","");
    }

    var capitalCitiesCoordinates = {
      "北京": { "latitude": 39.904987, "longitude": 116.405289 },
      "安徽": { "latitude": 31.861191, "longitude": 117.283043 },
      "重庆": { "latitude": 29.533155, "longitude": 106.504959 },
      "福建": { "latitude": 26.075302, "longitude": 119.306236 },
      "甘肃": { "latitude": 36.061380, "longitude": 103.834170 },
      "广东": { "latitude": 23.125177, "longitude": 113.28064 },
      "广西": { "latitude": 22.82402, "longitude": 108.320007 },
      "贵州": { "latitude": 26.578342, "longitude": 106.713478 },
      "海南": { "latitude": 20.044220, "longitude": 110.199890 },
      "河北": { "latitude": 38.045475, "longitude": 114.502464 },
      "河南": { "latitude": 34.757977, "longitude": 113.665413 },
      "黑龙江": { "latitude": 45.756966, "longitude": 126.642464 },
      "湖北": { "latitude": 30.584354, "longitude": 114.298569 },
      "湖南": { "latitude": 28.19409, "longitude": 112.982277 },
      "江苏": { "latitude": 32.041546, "longitude": 118.76741 },
      "江西": { "latitude": 28.676493, "longitude": 115.892151 },
      "辽宁": { "latitude": 41.796768, "longitude": 123.429092 },
      "内蒙古": { "latitude": 40.841490, "longitude": 111.751990 },
      "宁夏": { "latitude": 38.486440, "longitude": 106.232480 },
      "青海": { "latitude": 36.617290, "longitude": 101.777820 },
      "山东": { "latitude": 36.675808, "longitude": 117.000923 },
      "山西": { "latitude": 37.857014, "longitude": 112.549248 },
      "陕西": { "latitude": 34.263161, "longitude": 108.948021 },
      "上海": { "latitude": 31.231707, "longitude": 121.472641 },
      "四川": { "latitude": 30.659462, "longitude": 104.065735 },
      "天津": { "latitude": 39.125595, "longitude": 117.190186 },
      "西藏": { "latitude": 29.644150, "longitude": 91.11450 },
      "新疆": { "latitude": 43.826630, "longitude": 87.616880 },
      "云南": { "latitude": 25.040609, "longitude": 102.71225 },
      "浙江": { "latitude": 30.287458, "longitude": 120.15358 },
      // 港澳台特别行政区
      "香港": { "latitude": 22.275340, "longitude": 114.165460 },
      "澳门": { "latitude": 22.198750, "longitude": 113.549130 },
      "台北": { "latitude": 25.030724, "longitude": 121.520076 },
      "广西壮族自治区": {"longitude": "108.320007","latitude": "22.82402"},
      "内蒙古自治区": {
        "longitude": "111.75199",
        "latitude": "40.84149"
      },
      "西藏自治区": {
        "longitude": "91.132212",
        "latitude": "29.660361"
      },
      "新疆维吾尔自治区": {
        "longitude": "87.617729",
        "latitude": "43.826839"
      },
      "宁夏回族自治区": {
        "longitude": "106.278361",
        "latitude": "38.46747"
      }
    };
  
    let result = capitalCitiesCoordinates[proviceName];
    if(!result){
      result = {"longitude": "0","latitude": "0"};
    }

    return result;
  }
  
}
