
import store from "@/store";
/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
    decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"')
      .replace(/\+/g, ' ') +
    '"}'
  )
}

export function deepClone(source, map = new WeakMap()) {
  // 处理基本类型和函数（直接返回）
  if (typeof source !== 'object' || source === null) {
    return source;
  }

  // 处理循环引用
  if (map.has(source)) {
    return map.get(source);
  }

  // 创建新容器
  const target = Array.isArray(source) ? [] : {};
  map.set(source, target);  // 记录克隆关系

  // 克隆普通键值
  for (const key in source) {
    if (Object.prototype.hasOwnProperty.call(source, key)) {
      target[key] = deepClone(source[key], map);
    }
  }

  // 克隆Symbol键值（ES6+）
  const symbolKeys = Object.getOwnPropertySymbols(source);
  for (const symKey of symbolKeys) {
    if (source.propertyIsEnumerable(symKey)) {
      target[symKey] = deepClone(source[symKey], map);
    }
  }

  return target;
}
export function formatSize(size, fixed = 2) {
  if (isNaN(parseFloat(size))) return ''
  let kbSize = size / 1024
  if (kbSize <= 1024) {
    return `${kbSize.toFixed(fixed)}KB`
  }
  let mbSize = kbSize / 1024
  return `${mbSize.toFixed(fixed)}MB`
}

let timer = null,  // 网速定时器
  lastPercentage = 0,
  percentageById = {},
  imageId = null,
  bytesReceivedPerSecond = {}; // 时间节点上传文件总量
// 获取网速
export function getNetWorkSpeed() {
  if (timer) return false;
  // if (lastPercentage < 100) return false;
  if (imageId && imageId !== Id) return false
  imageId = null
  timer = setInterval(() => {
    let timeList = Object.keys(bytesReceivedPerSecond).sort((a, b) => a - b);
    if (timeList.length > 0) {
      let totalBytes = timeList.reduce((sum, bytes) => sum + bytesReceivedPerSecond[bytes], 0) / (5 * 1024);
      let unit = 'KB/s';
      if (totalBytes > 1024) {
        totalBytes = totalBytes / 1024;
        unit = "MB/s";
      }
      store.state.trials.downloadTip = totalBytes.toFixed(3) + unit;
    }
    if (timeList.length >= 5) {
      delete bytesReceivedPerSecond[timeList[0]]
    }
    let time = new Date().getTime();
    bytesReceivedPerSecond[time] = 0;
  }, 1000)

}
export function setNetWorkSpeedSize(totalPercentage, total, Id) {
  if (imageId && imageId !== Id) return false
  imageId = Id
  let percentage = totalPercentage - lastPercentage
  percentage = percentage / 100
  lastPercentage = totalPercentage
  // console.log(percentage, totalPercentage, total)
  let time = new Date().getTime();
  let timeList = Object.keys(bytesReceivedPerSecond).sort((a, b) => a - b);
  let bytesTime = timeList.find(item => time - item < 1000);
  if (bytesTime) {
    bytesReceivedPerSecond[bytesTime] += total * percentage;
  } else {
    // console.log("未查询到时间")
    if (timeList.length > 0) {
      bytesReceivedPerSecond[timeList[timeList.length - 1]] += total * percentage;
    } else {
      bytesReceivedPerSecond[time] = total * percentage;
    }
  }
  store.state.trials.uploadSize = `${formatSize(totalPercentage / 100 * total)}/${formatSize(total)}`
}
export function setNetWorkSpeedSizeAll(totalPercentage, total, Id) {
  if (!percentageById[Id]) {
    percentageById[Id] = 0
  }
  let percentage = totalPercentage - percentageById[Id]
  percentage = percentage / 100
  percentageById[Id] = totalPercentage
  // console.log(percentage, totalPercentage, total)
  let time = new Date().getTime();
  let timeList = Object.keys(bytesReceivedPerSecond).sort((a, b) => a - b);
  let bytesTime = timeList.find(item => time - item < 1000);
  if (bytesTime) {
    bytesReceivedPerSecond[bytesTime] += total * percentage;
  } else {
    // console.log("未查询到时间")
    if (timeList.length > 0) {
      bytesReceivedPerSecond[timeList[timeList.length - 1]] += total * percentage;
    } else {
      bytesReceivedPerSecond[time] = total * percentage;
    }
  }
  let isComplete = Object.keys(percentageById).every(key => percentageById[key >= 100])
  if (isComplete) {
    workSpeedclose(true)
  }
}
export function workSpeedclose(isForce = false) {
  if (!isForce && lastPercentage < 100) {
    return false
  }
  console.log('workSpeedclose')
  if (timer) {
    clearInterval(timer);
    timer = null;
    store.state.trials.downloadTip = '0KB/s'
    store.state.trials.downloadSize = ''
  }
  bytesReceivedPerSecond = {};
  lastPercentage = 0;
  imageId = null;
  percentageById = {};
}
function readDirectoryEntries(directoryReader) {
  return new Promise((resolve, reject) => {
    let entries = [];
    function readBatch() {
      directoryReader.readEntries(
        (results) => {
          if (results.length) {
            entries = entries.concat(results);
            readBatch();
          } else {
            resolve(entries);
          }
        },
        (error) => reject(error)
      );
    }
    readBatch();
  });
}
export async function readEntry(entry) {
  const files = [];

  // 如果是文件夹，创建读取器并递归读取其内容
  if (entry.isDirectory) {
    const directoryReader = entry.createReader();
    const entries = await readDirectoryEntries(directoryReader)
    // 递归读取文件夹内的每一项
    for (const subEntry of entries) {
      const subFiles = await readEntry(subEntry);
      files.push(...subFiles);
    }
  }
  // 如果是文件，则将其转换为File对象
  else if (entry.isFile) {
    const file = await new Promise((resolve, reject) => {
      entry.file(resolve, reject); // entry.file()是异步的
    });
    files.push(file);
  }

  return files;
}
// 使用FNV-1a哈希算法确保相同GUID产生相同结果
function fnv1aHash(str) {
  const FNV_OFFSET_BASIS = 2166136261;
  const FNV_PRIME = 16777619;

  let hash = FNV_OFFSET_BASIS;
  for (let i = 0; i < str.length; i++) {
    hash ^= str.charCodeAt(i);
    hash = (hash * FNV_PRIME) >>> 0; // 使用无符号右移确保结果为无符号32位整数
  }
  return hash;
}
// RGB转十六进制
function rgbToHex(r, g, b) {
  return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}
// HSL转RGB函数
function hslToRgb(h, s, l) {
  let r, g, b;

  if (s === 0) {
    r = g = b = l; // 灰色
  } else {
    const hue2rgb = (p, q, t) => {
      if (t < 0) t += 1;
      if (t > 1) t -= 1;
      if (t < 1 / 6) return p + (q - p) * 6 * t;
      if (t < 1 / 2) return q;
      if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
      return p;
    };

    const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
    const p = 2 * l - q;

    r = hue2rgb(p, q, h + 1 / 3);
    g = hue2rgb(p, q, h);
    b = hue2rgb(p, q, h - 1 / 3);
  }

  return { r, g, b };
}
export function guidToColor(guid) {
  // 移除GUID中的连字符和花括号（如果有）
  let cleanGuid = guid.replace(/[{}()-]/g, '');
  // 计算GUID的哈希值
  const hash = fnv1aHash(cleanGuid);

  // 使用哈希值生成HLS颜色（确保高区分度）
  // 将哈希值映射到0-1之间
  const h = (hash & 0xFFFF) / 0xFFFF; // 使用前16位作为色相
  const s = ((hash >> 16) & 0xFF) / 0xFF * 0.6 + 0.4; // 饱和度在0.4-1.0之间
  const l = ((hash >> 24) & 0xFF) / 0xFF * 0.4 + 0.4; // 亮度在0.3-0.7之间，避免太暗或太亮

  // 返回RGB对象
  let rgb = hslToRgb(h, s, l);
  let obj = {
    r: Math.round(rgb.r * 255),
    g: Math.round(rgb.g * 255),
    b: Math.round(rgb.b * 255)
  }
  let str = rgbToHex(obj.r, obj.g, obj.b)
  return str;
}