import { computed, ref } from "vue";
import { modelGroup } from "../scene/models";
import { lineGroup, labelColor, setLabelText } from "../scene/lines";

export interface RuleForm {
  kll: string;
  jj: string;
  nzd: string;
  wzd: string;
  snwd: string;
  swwd: string;
  cndc: string;
  startTime: string;
  endTime: string;
  gfdl: string;
  fddl: string;
  zydl: string;
  pm2_5: string;
  CO_2: string;
  kqzl: string;
  ydsd: string;
}

export function getFormValue(index: number | undefined) {
  switch (index) {
    case 1:
      return fromState.value.kll;
    case 2:
      return fromState.value.jj;
    case 3:
      return fromState.value.wzd;
    case 4:
      return fromState.value.snwd;
    case 5:
      return fromState.value.cndc;
    case 6:
      return fromState.value.ydsd;
    case 7:
      return fromState.value.gfdl;
    case 8:
      return fromState.value.fddl;
    case 9:
      return fromState.value.kqzl;
    default:
      return "";
  }
}

const state = ref([]);
const fromState = ref<RuleForm>({
  kll: "",
  jj: "",
  nzd: "",
  wzd: "",
  snwd: "",
  swwd: "",
  cndc: "",
  ydsd: "",
  startTime: "",
  endTime: "",
  gfdl: "",
  fddl: "",
  zydl: "",
  pm2_5: "",
  CO_2: "",
  kqzl: "",
});

const conditioner = ref({
  fs: 0,
  wd: 0,
});

export const useStrategy = () => {
  const getState = computed(() => state.value);
  const getFromState = computed(() => fromState.value);
  const getConditioner = computed(() => conditioner.value);

  const setState = (newState: any) => {
    state.value = newState;
  };

  const setFromState = (newFromState: RuleForm) => {
    fromState.value = newFromState;
    answer();
  };

  //循环目标列表
  const loopTargetList = (targetList: any, targetValue: number | string) => {
    targetList.forEach((item: any) => {
      //1.灯光 2.空调 3.动效走势
      if (item.targetType === 1) {
        // console.log("灯光", item.lightingNum);
        const light = modelGroup.getObjectByName("负荷外光");
        const text = document.getElementById("load");
        //@ts-ignore
        for (let e of text.lastElementChild.children) {
          let first = e.firstElementChild as any;
          let last = e.lastElementChild as any;
          if (first.innerText.indexOf("灯光亮度") !== -1) {
            last.innerText = item.lightingNum * 100 + "%";
          }
        }
        //@ts-ignore
        light.material.opacity = item.lightingNum;
      } else if (item.targetType === 2) {
        console.log("空调风速", item.ktfs, "空调温度", item.ktwd);
        const text = document.getElementById("load");
        //@ts-ignore
        for (let e of text.lastElementChild.children) {
          let first = e.firstElementChild as any;
          let last = e.lastElementChild as any;
          if (first.innerText.indexOf("空调风速") !== -1) {
            last.innerText = Number(item.ktfs) + "m/s";
          } else if (first.innerText.indexOf("空调温度") !== -1) {
            last.innerText = Number(item.ktwd) + "℃";
          }
        }
        // console.log([text]);
        conditioner.value = {
          fs: Number(item.ktfs),
          wd: Number(item.ktwd),
        };
      } else if (item.targetType === 3) {
        console.log("动效走势", item.dxzs);
        const sum = Number(fromState.value.gfdl) + Number(fromState.value.fddl);
        const rzy = sum < Number(targetValue) ? sum : Number(targetValue);
        let line_, line, kwh;
        switch (item.dxzs) {
          case 1:
            line = lineGroup.getObjectByName("日上网");
            //@ts-ignore
            line.material.color.set("#2ecc71");
            //@ts-ignore
            labelColor("rsw_title", "#2ecc71");

            kwh = sum - Number(targetValue);
            //@ts-ignore
            setLabelText("rsw_title", kwh);
            line_ = lineGroup.getObjectByName("日上网_");
            break;
          case 2:
            line = lineGroup.getObjectByName("日购电");
            //@ts-ignore
            line.material.color.set("#c12c1f");

            //@ts-ignore
            labelColor("rgd_title", "#c12c1f");
            kwh = Number(targetValue) - (sum + Number(fromState.value.cndc));
            //@ts-ignore
            setLabelText("rgd_title", kwh);
            line_ = lineGroup.getObjectByName("日购电_");
            break;
          case 3:
            line = lineGroup.getObjectByName("放电量");
            //@ts-ignore
            line.material.color.set("#2ecc71");

            //@ts-ignore
            line.material.color.set("#2ecc71");

            //@ts-ignore
            labelColor("fdl_title", "#2ecc71");
            kwh = Number(fromState.value.cndc);
            //@ts-ignore
            setLabelText("fdl_title", kwh ? (sum + kwh) < Number(targetValue)?kwh:Number(targetValue) - rzy : kwh);
            line_ = lineGroup.getObjectByName("放电量_");
            break;
          case 4:
            line = lineGroup.getObjectByName("日充电");
            //@ts-ignore
            line.material.color.set("#2ecc71");

            //@ts-ignore
            line.material.color.set("#2ecc71");
            //@ts-ignore
            labelColor("rcd_title", "#2ecc71");
            line_ = lineGroup.getObjectByName("日充电_");
            break;
          case 5:
            line = lineGroup.getObjectByName("日自用");
            //@ts-ignore
            line.material.color.set("#2ecc71");

            //@ts-ignore
            line.material.color.set("#2ecc71");
            //@ts-ignore
            labelColor("rzy_title", "#2ecc71");
            //@ts-ignore
            setLabelText("rzy_title", rzy);
            line_ = lineGroup.getObjectByName("日自用_");
            break;
        }
        //@ts-ignore
        line_.visible = true;
      }
    });
  };

  const answer = () => {
    getState.value.forEach((item: any) => {
      loopConditionList(item.strategyConditionList);
    });
  };

  //循环条件列表
  const loopConditionList = (conditionList: any) => {
    conditionList.forEach((item: any) => {
      if (
        qufenType({
          qufen: item.qufen,
          conditionType: item.conditionType,
          targetCondition: item.targetCondition,
          type: item.condition_field_type1,
          type2: item.condition_field_type2,
          val: getFormValue(item.condition_field1),
          val2: getFormValue(item.condition_field2),
          val3: getFormValue(item.condition_field3),
          val4: getFormValue(item.condition_field4),
        }) &&
        qufenType({
          qufen: item.qufen2,
          conditionType: item.conditionType2,
          targetCondition: item.targetCondition2,
          type: item.condition2_field_type1,
          type2: item.condition2_field_type2,
          val: getFormValue(item.condition2_field1),
          val2: getFormValue(item.condition2_field2),
          val3: getFormValue(item.condition2_field3),
          val4: getFormValue(item.condition2_field4),
        }) &&
        qufenType({
          qufen: item.qufen3,
          conditionType: item.conditionType3,
          targetCondition: item.targetCondition3,
          type: item.condition3_field_type1,
          type2: item.condition3_field_type2,
          val: getFormValue(item.condition3_field1),
          val2: getFormValue(item.condition3_field2),
          val3: getFormValue(item.condition3_field3),
          val4: getFormValue(item.condition3_field4),
        })
      ) {
        loopTargetList(item.strategyTargets, item.targetCondition);
      }
    });
  };

  //区分是单条件还是联合条件
  const qufenType = ({
    qufen,
    conditionType,
    type,
    type2,
    val,
    val2,
    val3,
    val4,
    targetCondition,
  }: {
    qufen: number;
    conditionType: number;
    type: number;
    type2: number;
    val: string | number;
    val2: string | number;
    val3: string | number;
    val4: string | number;
    targetCondition: string | number;
  }) => {
    if (qufen === 1) {
      if (val4) {
        return numberConditionType(val4, conditionType, targetCondition);
      }
      return false;
    } else if (qufen === 2) {
      return isConditionMore(
        conditionType,
        type,
        type2,
        val,
        val2,
        val3,
        targetCondition
      );
    }
    return true;
  };

  //计算加减乘除 三个参数分别为计算值 计算类型 计算值 返回值为计算结果
  const calculateType = (
    value: string | number,
    type: number,
    condition: string | number
  ) => {
    if (type) {
      switch (type) {
        case 1:
          return Number(value) + Number(condition);

        case 2:
          return Number(value) - Number(condition);

        case 3:
          return Number(value) * Number(condition);

        case 4:
          return Number(value) / Number(condition);

        default:
          return 0;
      }
    }
    return 0;
  };

  //计算方法
  const calculate = (
    type: number,
    type2: number,
    value: number | string,
    value2: number | string,
    value3: number | string
  ): number => {
    if (type && value && value2) {
      const val = calculateType(value, type, value2);
      if (type2 && value3) {
        return calculateType(val, type2, value3);
      } else {
        return val;
      }
    }
    return 0;
  };

  //联合条件 组合计算判断 返回值为true或false
  const isConditionMore = (
    conditionType: number,
    type: number,
    type2: number,
    val: string | number,
    val2: string | number,
    val3: string | number,
    targetCondition: string | number
  ) => {
    console.log(
      conditionType,
      calculate(type, type2, val, val2, val3),
      targetCondition
    );
    if (
      numberConditionType(
        calculate(type, type2, val, val2, val3),
        conditionType,
        targetCondition
      ) &&
      val
    ) {
      return true;
    }
    return false;
  };

  //根据数字类型判断条件大于小于等于 三个参数分别为判断值 判断条件 判断值 返回值为true或false
  const numberConditionType = (
    value: string | number,
    type: number,
    condition: string | number
  ) => {
    console.log(value, type, condition);
    if (type) {
      switch (type) {
        case 1:
          return Number(value) > Number(condition);
        case 2:
          return Number(value) < Number(condition);
        case 3:
        case 4:
          return value === condition;
        case 5:
          return timeConditionType(value as string, condition as string);
        case 6:
          return Number(value) >= Number(condition);
        case 7:
          return Number(value) <= Number(condition);
        default:
          return false;
      }
    }
    return true;
  };

  //判断日期区间是否在区间内 两个参数分别为时间段("7:00-11:00") 判断时间("00:00")
  const timeConditionType = (time: string, condition: string) => {
    const timeArr = condition.split("-");
    // const conditionArr = condition.split("-");
    if (timeArr.length > 1) {
      const timeStart = new Date(`2020-01-01 ${timeArr[0]}`).getTime();
      const timeEnd = new Date(`2020-01-01 ${timeArr[1]}`).getTime();
      const conditionTime = new Date(`2020-01-01 ${time}`).getTime();
      // const conditionEnd = new Date(`2020-01-01 ${conditionArr[1]}`).getTime();
      return conditionTime >= timeStart && conditionTime <= timeEnd;
    }
  };

  return {
    getState,
    setState,
    getFromState,
    setFromState,
    getConditioner,
  };
};
