

import ImageCompressor from "image-compressor.js";

// 生成10位数的随机数
export function generateRandom10Digits() {
  // 生成范围在1000000000到9999999999之间的随机整数
  const min = 1000000000;
  const max = 9999999999;
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 随机颜色 - 把16进制的颜色换成rgba格式
export function choseRbgb(color, opcity) {
  if (color) {
    return 'rgba(' + parseInt('0x' + color.slice(1, 3)) + ',' + parseInt('0x' + color.slice(3, 5)) + ',' + parseInt('0x' + color.slice(5, 7)) + ',' + opcity + ')'
  } else {
    let r = Math.floor(Math.random() * 256)
    let g = Math.floor(Math.random() * 256)
    let b = Math.floor(Math.random() * 256)
    let a = opcity ? opcity : 1
    return `rgba(${r},${g},${b},${a})`
  }
}

// 随机十六进制颜色
export function randomHexColor() { // 随机生成十六进制颜色
  var hex = Math.floor(Math.random() * 16777216).toString(16);
  while (hex.length < 6) {
    hex = '0' + hex;
  }
  return '#' + hex;
}

/**
 * 压缩图片
 * @param {*} file
 * @param {*} quality 压缩比
 */
export function compressImage(file, quality = 0.6) {
  return new Promise((resolve, reject) => {
    new ImageCompressor(file, {
      quality, //压缩质量
      success(res) {
        let fileData = new File([res], res.name, { type: res.type });
        resolve(fileData);
      },
      error(e) {
        reject("图片压缩失败，请稍后再试");
      },
    });
  });
};

// 今天周几
export function weekValidate() {
  let week = new Date().getDay()
  let weekenday = ''
  switch (week) {
    case 0: return weekenday = '星期日'
    case 1: return weekenday = '星期一'
    case 2: return weekenday = '星期二'
    case 3: return weekenday = '星期三'
    case 4: return weekenday = '星期四'
    case 5: return weekenday = '星期五'
    case 6: return weekenday = '星期六'
  }
}

// 毫秒转时长
export function formatDuring(mss) {
  var days = parseInt(mss / (1000 * 60 * 60 * 24));
  var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = (mss % (1000 * 60)) / 1000;
  if (days) {
    return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
  } else if (hours) {
    return hours + "小时" + minutes + "分钟" + seconds + "秒";
  } else if (minutes) {
    return minutes + "分钟" + seconds + "秒";
  } else {
    return seconds + "秒";
  }
}

// 转换时间格式
export function timeValidate(date, type) {
  const time = date ? new Date(date) : new Date()
  const yyyy = time.getFullYear()
  const MM = (time.getMonth() + 1).toString().padStart(2, 0)
  const dd = time.getDate().toString().padStart(2, '0')
  const hh = time.getHours().toString().padStart(2, '0')
  const mm = time.getMinutes().toString().padStart(2, '0')
  const ss = time.getSeconds().toString().padStart(2, '0')
  if (type == 'ymd') {
    return `${yyyy}-${MM}-${dd}`;
  }
  if (type == 'md') {
    return `${MM}.${dd}`
  }
  if (type == 'td') {
    return `${yyyy}年${MM}月${dd}日`
  }
  if (type == 'yd') {
    return `${yyyy}`
  }
  if (type == 'mm') {
    return `${yyyy}${MM}${dd}${hh}${mm}${ss}`
  }
  if (type == 'ydm') {
    return `${yyyy}年${MM}月${dd}日${hh}时${mm}分${ss}秒`
  }
  return `${yyyy}-${MM}-${dd} ${hh}:${mm}:${ss}`
}


export function timeSlotChange(val) {
  let startTime, endTime;
  let now = new Date(); //当前日期
  let nowDayOfWeek = now.getDay(); //今天本周的第几天
  let nowDay = now.getDate(); //当前日
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getFullYear(); //当前年
  let jd = Math.ceil((nowMonth + 1) / 3)
  switch (val) {
    case '天':
    case '日':
      startTime = timeValidate(new Date(), 'ymd')
      endTime = timeValidate(new Date(), 'ymd')
      break;
    case "本周":
    case "周":
      startTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
      endTime = new Date(nowYear, nowMonth, nowDay + 6 - nowDayOfWeek)
      break;
    case "本月":
    case "月":
      startTime = new Date(nowYear, nowMonth, 1)
      endTime = new Date(nowYear, nowMonth + 1, 0)
      break;
    case "本季度":
    case "季度":
      startTime = new Date(nowYear, (jd - 1) * 3, 1)
      endTime = new Date(nowYear, jd * 3, 0)
      break
    case "本年":
    case "年":
      startTime = new Date(nowYear, 0, 1)
      endTime = new Date(nowYear, 11, 31)
      break
  }
  return [timeValidate(startTime, 'ymd'), timeValidate(endTime, 'ymd')]
}


// 获取当前近多少天  7后7天  -7 前五天
export function getRecentDay(n) {
  var currentDate = new Date();
  var preDate = new Date(currentDate.getTime() + n * 24 * 3600 * 1000)
  let year = preDate.getFullYear()
  let mon = preDate.getMonth() + 1
  let day = preDate.getDate()
  let s = year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day)
  return s
}

// 获取n近7月  7后7 -7 前
export function getnRencebtMonth(n) {
  let date = new Date();
  date.setMonth(date.getMonth() - n)
  date.toLocaleDateString()
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? ('0' + m) : m + ''
  return y + m
}
/**
 * 数据去重 相同数据值累加
 * @param {Object} array 数据
 */
export function setArray(array) {
  let newArr = []
  array.forEach(item => {
    const res = newArr.findIndex(ol => {
      //组织机构代码相同 并且报警类别相同
      return item.ssbmdm == ol.ssbmdm && item.bjlb == ol.bjlb
    })
    if (res !== -1) {
      newArr[res].sl = newArr[res].sl + item.sl
    } else {
      newArr.push(item)
    }
  })

  return newArr
}

/**
 * 合并数据
 * @param {Object} array 数据
 */
export function hbArray(array, item1, item2, item3) {
  let newArr = []
  array.forEach(item => {
    const res = newArr.findIndex(ol => {
      //组织机构代码相同 并且报警类别相同
      return item.product == ol.product
    })
    if (res !== -1) {
      newArr[res][item1] = newArr[res][item1] + item[item1]
      newArr[res][item2] = newArr[res][item2] + item[item2]
      newArr[res][item3] = newArr[res][item3] + item[item3]
    } else {
      newArr.push(item)
    }
  })

  return newArr
}
//时间格式
export function dateFormat(type, time) {
  let date
  if (time) {
    date = new Date(time);
  } else {
    date = new Date();
  }
  let year = date.getFullYear();
  let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  let day

  if (type == 'z') {
    //前一天日期
    day = date.getDate() - 1;
    day = day < 10 ? "0" + day : day;
    return `${year}-${month}-${day}`;
  } else if (type == 'all') {
    //格式化日期时间
    day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  } else {
    //当天日期
    day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return `${year}-${month}-${day}`;
  }
  return day
}

//数字超长处理
export function handleNum(num) {
  var data = 0
  if (num) {
    try {
      if (num * 1 > 100000) {
        data = (num / 10000).toFixed(0) + '万'
      } else {
        data = (num * 1).toFixed(0)
      }
    } catch (error) {
      data = 0
    }
  }

  return data
}
/**
 * 文件是否是图片
 * @param {*} val
 */
export function IS_PNG(val) {
  return ['bmp', 'jpg', 'png', 'tif', 'gif', 'pcx', 'tga', 'exif', 'fpx', 'svg', 'psd', 'cdr', 'pcd', 'dxf', 'ufo',
    'eps', 'ai', 'raw', 'wmf', 'webp', 'avif', 'apng'
  ].indexOf(val.toLowerCase()) !== -1
}
/**
 * 文件是否是音频
 * @param {*} val
 */
export function IS_MP3(val) {
  return ['mp3', 'wav', 'wma', 'mp2', 'flac', 'midi', 'ra', 'ape', 'aac', 'cda', 'mov'].indexOf(val.toLowerCase()) !==
    -1
}

/**
 * 文件是否是视频
 * @param {*} val
 */
export function IS_MP4(val) {
  return ['avi', 'wmv', 'mpeg', 'mp4', 'm4v', 'mov', 'asf', 'fiv', 'f4v', 'mvb', 'rm', '3gp', 'vob'].indexOf(val
    .toLowerCase()) !== -1
}


function handelArr(arr) {
  let brr = []
  if (arr && arr.length > 0) {
    let obj = {}
    let coords = "";
    for (let i = 0; i < arr.length; i++) {
      coords += arr[i] + ","
    }
    obj.coords = coords
    brr.push(obj)
  }
  return brr
}


/**
 * 时间  天数
 * @param {*} val
 */
export function setEchartTime(val) {
  let date = new Date();
  let arrTime = [];
  if (val == 0) {
    for (let i = 0; i < 24; i++) {
      arrTime.push(i)
    }
  } else {
    for (let i = 0; i < val; i++) {
      let date1 = new Date(date.getTime() - i * 24 * 60 * 60 * 1000)
      arrTime.push(_setTime(date1))
    }
    arrTime.reverse()
  }
  return arrTime
}
//设置时间
function _setTime(date) {
  let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return `${month}-${day}`;
}


//拼接地址
export function setAddress(val) {
  const url = '/mosty-api/mosty-base/minio/image/download/'
  return url + val
}
