import * as echarts from "echarts";
import moment from "moment/moment";
import _ from "lodash";

export const conditionListBackup = [
  { "name": "钻进-坐卡", "EnName": null, "color": "#CCCCCC" },
  { "name": "钻进-待命", "EnName": null, "color": "#3cb44b" },
  { "name": "钻进-干钻", "EnName": null, "color": "#ffe119" },
  { "name": "钻进-滑动钻进", "EnName": null, "color": "#4363d8" },
  { "name": "钻进-旋转钻进", "EnName": null, "color": "#f58231" },
  { "name": "钻进-停待", "EnName": null, "color": "#911eb4" },
  { "name": "钻进-停泵空转", "EnName": null, "color": "#46f0f0" },
  { "name": "钻进-静止循环", "EnName": null, "color": "#f032e6" },
  { "name": "钻进-循环空转", "EnName": null, "color": "#bcf60c" },
  { "name": "钻进-上提钻具", "EnName": null, "color": "#fabebe" },
  { "name": "钻进-干倒划", "EnName": null, "color": "#008080" },
  { "name": "钻进-边提边循环", "EnName": null, "color": "#e6beff" },
  { "name": "钻进-倒划眼", "EnName": null, "color": "#9a6324" },
  { "name": "钻进-下放钻具", "EnName": null, "color": "#fffac8" },
  { "name": "钻进-干划眼", "EnName": null, "color": "#800000" },
  { "name": "钻进-边放边循环", "EnName": null, "color": "#aaffc3" },
  { "name": "钻进-划眼", "EnName": null, "color": "#e6194B" },
  { "name": "起下钻-在地面", "EnName": null, "color": "#ffd8b1" },
  { "name": "起下钻-坐卡", "EnName": null, "color": "#000075" },
  { "name": "起下钻-停待", "EnName": null, "color": "#808080" },
  { "name": "起下钻-空转", "EnName": null, "color": "#3cb44b" },
  { "name": "起下钻-中途循环", "EnName": null, "color": "#000000" },
  { "name": "起下钻-定点转转盘循环", "EnName": null, "color": "#a9a9a9" },
  { "name": "起下钻-上提钻具", "EnName": null, "color": "#fabed4" },
  { "name": "起下钻-干倒划", "EnName": null, "color": "#ffd7b4" },
  { "name": "起下钻-边提边循环", "EnName": null, "color": "#dcbeff" },
  { "name": "起下钻-倒划", "EnName": null, "color": "#000000" },
  { "name": "起下钻-下放钻具", "EnName": null, "color": "#808080" },
  { "name": "起下钻-干划", "EnName": null, "color": "#e6194B" },
  { "name": "起下钻-边放边循环", "EnName": null, "color": "#a9a9a9" },
  { "name": "起下钻-划眼", "EnName": null, "color": "#fabed4" },
  { "name": "未识别工况-未识别动作", "EnName": null, "color": "#ffd7b4" }
];
export const conditionList = [
  { "name": "钻进-坐卡", "EnName": "坐卡（钻进）", "color": "#CCCCCC" },
  { "name": "钻进-待命", "EnName": "???", "color": "#E2EFDA" },
  { "name": "钻进-干钻", "EnName": "???", "color": "#ffe119" },
  { "name": "钻进-滑动钻进", "EnName": "滑动钻进", "color": "#008000" },
  { "name": "钻进-旋转钻进", "EnName": "钻进", "color": "#00FF00" },
  { "name": "钻进-停待", "EnName": "静止(钻进)", "color": "#E2EFDA" },
  { "name": "钻进-停泵空转", "EnName": "空转", "color": "#996633" },
  { "name": "钻进-静止循环", "EnName": "原地循环(钻进)", "color": "#BF9000" },
  { "name": "钻进-循环空转", "EnName": "原地循环+旋转(钻进)", "color": "#00CC99" },
  { "name": "钻进-上提钻具", "EnName": "上提钻具(钻进)", "color": "#FBFB65" },
  { "name": "钻进-干倒划", "EnName": "上提划眼+开泵(钻进)", "color": "#66CCFF" },
  { "name": "钻进-边提边循环", "EnName": "上提钻具+开泵(钻进)", "color": "#FFCC66" },
  { "name": "钻进-倒划眼", "EnName": "上提划眼(钻进)", "color": "#00FFFF" },
  { "name": "钻进-下放钻具", "EnName": "下放钻具（钻进）", "color": "#FFF2CC" },
  { "name": "钻进-干划眼", "EnName": "下放划眼+开泵(钻进)", "color": "#6699FF" },
  { "name": "钻进-边放边循环", "EnName": "下放钻具+开泵(钻进)", "color": "#FFCC99" },
  { "name": "钻进-划眼", "EnName": "下放划眼(钻进)", "color": "#0066FF" },
  { "name": "起下钻-在地面", "EnName": "???", "color": "#ffd8b1" },
  { "name": "起下钻-坐卡", "EnName": "坐卡（起下钻）", "color": "#969696" },
  { "name": "起下钻-停待", "EnName": "静止(起下钻)", "color": "#CCFF33" },
  { "name": "起下钻-空转", "EnName": "空转(起下钻)", "color": "#E4B700" },
  { "name": "起下钻-中途循环", "EnName": "原地循环(起下钻)", "color": "#FF9900" },
  { "name": "起下钻-定点转转盘循环", "EnName": "原地循环+旋转(起下钻)", "color": "#C2DCB1" },
  { "name": "起下钻-上提钻具", "EnName": "起钻(起下钻)", "color": "#000099" },
  { "name": "起下钻-干倒划", "EnName": "干倒划眼起钻(起下钻)", "color": "#990000" },
  { "name": "起下钻-边提边循环", "EnName": "开泵起钻(起下钻)", "color": "#677872" },
  { "name": "起下钻-倒划", "EnName": "倒划眼起钻(起下钻)", "color": "#FF0000" },
  { "name": "起下钻-下放钻具", "EnName": "下钻(起下钻)", "color": "#3333CC" },
  { "name": "起下钻-干划", "EnName": "干划眼下钻(起下钻)", "color": "#CC3300" },
  { "name": "起下钻-边放边循环", "EnName": "开泵下钻(起下钻)", "color": "#6666FF" },
  { "name": "起下钻-划眼", "EnName": "划眼下钻(起下钻)", "color": "#0000CC" },
  { "name": "未识别工况-未识别动作", "EnName": "未知", "color": "#FFFFFF" }
];

const seriesItemTemp = {
  name: "???",
  type: "custom",
  itemStyle: {
    color: "???"
  },
  renderItem: function(params, api) {
    let categoryIndex = api.value(0);
    let start = api.coord([api.value(1), categoryIndex]);
    let end = api.coord([api.value(2), categoryIndex]);
    let height = api.size([0, 1])[1] * 0.6;

    return {
      type: "rect",
      shape: echarts.graphic.clipRectByRect(
        {
          x: start[0],
          y: start[1] - height / 2,
          width: end[0] - start[0],
          height: height
        },
        {
          x: params.coordSys.x,
          y: params.coordSys.y,
          width: params.coordSys.width,
          height: params.coordSys.height
        }
      ),
      style: api.style() // 使用series.itemStyle内置样式
    };
  },
  encode: {
    x: [1, 2], // 表示维度 3、1、5 映射到 x 轴。
    y: 0 // 表示维度 2 映射到 y 轴。
  },
  data: [
    // [1, `${today} 08:09:10`, `${today} 09:09:10`],
  ]
};

const seriesItemTempByHour = {
  name: "???",
  type: "custom",
  itemStyle: {
    color: "???"
  },
  renderItem: function(params, api) {
    let categoryIndex = api.value(0)
    // 注意这里x, y的顺序相对之前代码进行了调整
    let start = api.coord([categoryIndex, api.value(1)])
    let end = api.coord([categoryIndex, api.value(2)])
    // 计算宽度时，根据类别的数量调整矩形的宽度
    let width = api.size([1, 0])[0] * 0.6

    return {
      type: 'rect',
      shape: echarts.graphic.clipRectByRect({
        x: start[0] - width / 2, // 确保矩形在类别坐标中居中
        y: end[1], // 因为图表是自下而上绘制，y轴的结束点作为矩形的起点
        width: width, // 根据类别调整的矩形宽度
        height: start[1] - end[1] // 正确计算矩形的高度
      }, {
        x: params.coordSys.x,
        y: params.coordSys.y,
        width: params.coordSys.width,
        height: params.coordSys.height
      }),
      style: api.style()
    }

  },
  encode: {
    x: 0, // 类别映射到 x 轴
    y: [1, 2] // 时间范围映射到 y 轴度 2 映射到 y 轴。
  },
  data: [
    // [1, `${today} 08:09:10`, `${today} 09:09:10`],
  ]
};

export function getNewSeriesItemByHour(name, color) {
  let temp = {};
  // Object.assign 为浅拷贝
  temp = _.cloneDeep(seriesItemTempByHour);
  // Object.assign(temp, seriesItemTemp);
  temp.name = name;
  temp.itemStyle.color = color;
  // console.log(temp.itemStyle.color)
  return temp;
}
export function getNewSeriesItem(name, color) {
  let temp = {};
  // Object.assign 为浅拷贝
  temp = _.cloneDeep(seriesItemTemp);
  // Object.assign(temp, seriesItemTemp);
  temp.name = name;
  temp.itemStyle.color = color;
  // console.log(temp.itemStyle.color)
  return temp;
}

export function fakeData(count) {
  let timeArray = [];
  let todayStart = moment()
    .startOf("day")
    .valueOf(); // 使用当前日期0点时间戳
  let todayEnd = moment()
    .endOf("day")
    .valueOf();

  for (let i = 0; i < count; i++) {


    let startTime = moment(todayStart + 1000*60 * getRandomInt(1, 10)); // 加上随机分钟
    let endTime = moment(startTime.valueOf() + 1000*60 * getRandomInt(1, 15)); // 加上随机分钟

    if (endTime.isAfter(todayEnd) || startTime.isAfter(todayEnd)) {
      console.log("out off time range");
      return timeArray;
    }

    timeArray.push([startTime.format("YYYY-MM-DD HH:mm:ss"), endTime.format("YYYY-MM-DD HH:mm:ss")]);
    // 设置下一个时间段的起始时间
    todayStart = endTime.valueOf();
  }
  // console.log(timeArray);
  return timeArray;
}

export function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

