import {
  conditionContainer,
  judgeContainer,
  condition,
  process,
  assemble
} from "@/model/Process";
import JsonHelper from "@/common/jsonHelper";
import { groupType } from "@/enums/process";
import NodeHelper from "@/common/nodeHelper";
export default class SwtionProcess {
  private static processLine = new process();
  private static processList = new Array<IProcess>();
  public addProcess(name?: string): IProcess {
    // let initProcess = new process(this.createId());
    let initProcess = new process(NodeHelper.createId());

    if (name) {
      initProcess.name = name;
    }
    let initAssemble = new assemble(NodeHelper.createId());
    // let initAssemble = new assemble(this.createId());
    let conditions = new condition(NodeHelper.createId());
    // let conditions = new condition(this.createId());
    // let initCondition = new conditionContainer("0-0");
    let initJudge = new judgeContainer(NodeHelper.createId());
    // let initJudge = new judgeContainer(this.createId());
    // initCondition.data.group.push(initJudge);
    conditions.group.push(initJudge);
    initAssemble.condition = conditions;
    initProcess.assemble.push(initAssemble);
    SwtionProcess.processLine = initProcess;
    SwtionProcess.processList.push(initProcess);
    return SwtionProcess.processLine;
  }
  public initProcess(process: Array<IProcess>) {
    SwtionProcess.processList = process;
  }
  public addAssemble(root?: IProcess) {
    root = root ? root : SwtionProcess.processLine;
    let initAssemble = new assemble(NodeHelper.createId());
    // let initAssemble = new assemble(this.createId());
    let conditions = new condition(NodeHelper.createId());
    // let conditions = new condition(this.createId());
    // let initCondition = new conditionContainer("0-0");
    let initJudge = new judgeContainer(NodeHelper.createId());
    // let initJudge = new judgeContainer(this.createId());
    // initCondition.data.group.push(initJudge);
    conditions.group.push(initJudge);
    initAssemble.condition = conditions;
    root.assemble.push(initAssemble);
  }
  public delAssemble(index: number, root?: IProcess) {
    root = root ? root : SwtionProcess.processLine;
    root.assemble.removeAt(index);
  }
  public loadProcess(): Array<IProcess> {
    return SwtionProcess.processList;
  }
  public loadProcessLineById(id?: string): IProcess {
    let line = new process();
    if (id) {
      for (let i = 0; i < SwtionProcess.processList.length; i++) {
        if (SwtionProcess.processList[i].id == id) {
          line = SwtionProcess.processList[i];
          break;
        }
      }
    }
    else {
      line = SwtionProcess.processLine;
    }
    return line;
  }

  public addJudge(parentNode: ICondition, type: groupType): IGroup {
    // debugger;

    // var judgeItem = new judgeContainer(this.createId());
    var judgeItem = new judgeContainer(NodeHelper.createId());
    switch (type) {
      case groupType.judge:
        parentNode.group.push(judgeItem);
        return judgeItem;
      case groupType.condition:
        // var condition = new conditionContainer(this.createId());
        var condition = new conditionContainer(NodeHelper.createId());
        condition.data.group.push(judgeItem);
        parentNode.group.push(condition);
        return condition;
    }

    // console.log(SwtionElement.treelist)
    // element.parent = parentNode;
    // let element = new CollectElement();
  }

  public delProcess(id: string, root?: Array<IProcess>): boolean {
    root = root ? root : SwtionProcess.processList;
    if (this.findEleInProcess(id)) {
      for (let i = 0; i < root.length; i++) {
        if (root[i].id == id) {
          root.removeAt(i)
          return true;
        }
      }
    }
    return false;
  }
  public findEleInProcess(id: string) {
    let result = false;
    let ifCondition = null;
    let valueCondition = null;
    for (let i = 0; i < SwtionProcess.processLine.assemble.length; i++) {
      let item = SwtionProcess.processLine.assemble;
      let group = item[i].condition.group;
      for (let j = 0; j < group.length; j++) {
        let tempIf = JsonHelper.findDataByKey(
          group[j],
          "data.if.id",
          id,
          "group"
        )
        let tempValue = JsonHelper.findDataByKey(
          group[j],
          "data.value.id",
          id,
          "group"
        )
        if (tempIf) {
          ifCondition = tempIf;
          break;
        }
        if (tempValue) {
          valueCondition = tempValue;
          break;
        }
      }
    }
    result = ifCondition === null && valueCondition === null;
    if (result) {
      for (let i = 0; i < SwtionProcess.processList.length; i++) {
        let item = SwtionProcess.processList[i].assemble;
        for (let j = 0; j < item.length; j++) {
          let html = item[j].return;
          if (html.indexOf('id="' + id + '"') > -1) {
            result = false;
            break;
          }
        }
      }
    }
    return result;
  }
  public delProcessNode(id: string, root?: IProcess): void {
    let parentNode = this.findConatinerNode(id, root);
    for (let i = 0; i < parentNode.group.length; i++) {
      if (parentNode.group[i].data.id == id) {
        parentNode.group.removeAt(i);
        break;
      }
    }
  }
  public findProcessNode(id: string, root?: IProcess) {
    let node = JsonHelper.findDataByKey(
      root ? root : SwtionProcess.processLine,
      "condition.data.id",
      id,
      "assemble|group"
    )
    return node;
  }
  public findConatinerNode(id: string, root?: IProcess): ICondition {
    let parentNode = JsonHelper.findDataParentByKey(
      root ? root : SwtionProcess.processLine,
      "condition.data.id",
      id,
      "assemble|group"
    ) as ICondition;
    return parentNode;
  }
  public leveConditionlUp(node: ICondition, root?: IProcess) {
    root = root ? root : SwtionProcess.processLine;
    let parentNode = this.findConatinerNode(node.id, root);
    let parentContainer = JsonHelper.findDataByKey(
      parentNode,
      "data",
      node,
      "assemble|group"
    ) as IGroup;
    let index = parentNode.group.indexOf(parentContainer);
    parentNode.group.removeAt(index);
    for (let i = 0; i < node.group.length; i++) {
      parentNode.group.insertAt(index, node.group[i]);
      index++;
    }
  }
  public leveJudgelUp(judge: judgeContainer, root?: IProcess) {
    // if (!parentNode) {
    root = root ? root : SwtionProcess.processLine;
    let parentNode = this.findConatinerNode(judge.data.id, root);
    // }
    let container = this.findConatinerNode(parentNode.id, root);
    let parentContainer = JsonHelper.findDataByKey(
      container,
      "data",
      parentNode,
      "assemble|group"
    ) as IGroup;
    parentNode.group.remove(judge);
    let index = container.group.indexOf(parentContainer);
    if (parentNode.group.length === 0) {
      container.group.removeAt(index);
    }
    container.group.insertAt(index, judge);
  }
  // public leveJudgelUp(node: ICondition, root?: ICondition) {
  //   let parentNode = this.findConatinerNode(node.id, root);
  //   let container = this.findConatinerNode(parentNode.id, root);
  //   container.group = node.group;
  // }
  // public leveConditionlUp(judge: judgeContainer, root?: ICondition) {
  //   // if (!parentNode) {
  //   debugger;
  //   let parentNode = this.findConatinerNode(judge.data.id, root);
  //   // }
  //   let container = this.findConatinerNode(parentNode.id, root);
  //   let parentContainer = new conditionContainer(this.createId());
  //   parentContainer.data = parentNode;
  //   let index = container.group.indexOf(parentContainer);
  //   container.group.removeAt(index);
  //   container.group.insertAt(index, judge);
  // }
  public levelConditionDown(
    parentNode: ICondition,
    judge: judgeContainer
  ): conditionContainer {
    // let condition = new conditionContainer(this.createId());
    let condition = new conditionContainer(NodeHelper.createId());
    let index = parentNode.group.indexOf(judge);
    parentNode.group.removeAt(index);
    // judge.data.id = this.createId();
    judge.data.id = NodeHelper.createId();
    condition.data.group.push(judge);
    parentNode.group.insertAt(index, condition);
    return condition;
    // parentNode.judge.push(
    //   new judge(swtionJudge.createId(parentNode.id, parentNode.judge))
    // );
  }
  public levelJudgeDown(node: ICondition): conditionContainer {
    // let condition = new conditionContainer(this.createId());
    let condition = new conditionContainer(NodeHelper.createId());
    let group = node.group;
    node.group = new Array<IGroup>();
    // condition.data.id = this.createId();
    condition.data.id = NodeHelper.createId();
    condition.data.group = group;
    node.group.push(condition);

    return condition;
    // parentNode.judge.push(
    //   new judge(swtionJudge.createId(parentNode.id, parentNode.judge))
    // );
  }
  //   public static clone(source: ICondition): ICondition {
  //     let result = new processCondition();
  //     result.childCondition = new Array<ICondition>().concat(source.childCondition);
  //     result.id = source.id;
  //     result.judge = new Array<IJudge>().concat(source.judge);
  //     result.relation = source.relation;
  //     return result;
  //   }
  public changeName(id: string, label: string = "") {
    for (let i = 0; i < SwtionProcess.processList.length; i++) {
      let item = SwtionProcess.processList[i].assemble;
      for (let j = 0; j < item.length; j++) {
        let node = document.createElement("div");
        node.innerHTML = item[j].return;
        for (let k = 0; k < node.children.length; k++) {
          if (node.children[k].id === id) {
            node.children[k].innerHTML = node.children[k].innerHTML.replace(/.*<span/g, label + "<span");
          }
        }
        item[j].return = node.innerHTML;
      }
    }
  }
}
