import { CollectElement, NormalElement } from "@/model/Element";
import SwtionAttribute from "@/plugins/attribute";
import { nodeType } from "@/enums/element";
import JsonHelper from "@/common/jsonHelper";
import SwtionProcess from "./process";
import { elementAttributeType } from "@/enums/attribute";
export default class SwtionElement {
  private static treelist = new CollectElement();
  public initTree(): CollectElement {
    let rootEle = new CollectElement("0");
    rootEle.name = "根节点";
    SwtionElement.treelist = rootEle;
    return SwtionElement.treelist;
  }
  public loadTree() {
    return SwtionElement.treelist;
  }
  public setTree(ele: CollectElement) {
    SwtionElement.treelist = ele;
  }
  public findTreeNode(id: string, root?: IBaseElement): IBaseElement {
    root = root ? root : SwtionElement.treelist;
    return JsonHelper.findDataByKey(root, "id", id);
  }
  public findChildTreeNode(id: string, root?: IBaseElement): Array<IBaseElement> {
    // debugger;
    root = root ? root : SwtionElement.treelist;
    let result = new Array<IBaseElement>();
    const parent = this.findTreeNode(id, root);
    if (parent) {
      getChildren(parent);
      function getChildren(source: IBaseElement) {
        if (source.children && source.children.length > 0) {
          for (let i = 0; i < source.children.length; i++) {
            result.push(source.children[i]);
            getChildren(source.children[i]);
          }
        }
      }
    }
    return result;
  }
  public addTreeNode(type: nodeType, parentNode: CollectElement): IBaseElement {
    // debugger;
    let element: IBaseElement;
    let key = 0;
    if (parentNode.children.length > 0) {
      let lastId = parentNode.children[parentNode.children.length - 1].id;
      let splitIds = lastId.split("-");
      key = Number(splitIds[splitIds.length - 1]) + 1;
    }
    const id = parentNode.id + "-" + key.toString();
    switch (type) {
      case nodeType.collect:
        element = new CollectElement(id);
        break;
      case nodeType.normal:
      default:
        element = new NormalElement(id);
        break;
    }
    for (let i = 0; i < parentNode.attribute.parent.length; i++) {
      element.attribute.parent.push(SwtionAttribute.clone(parentNode.attribute.parent[i]))
    }
    for (let i = 0; i < parentNode.attribute.self.length; i++) {
      if (parentNode.attribute.self[i].type !== elementAttributeType.attribute) {
        let parent = SwtionAttribute.clone(parentNode.attribute.self[i]);
        parent.parentId = parentNode.attribute.self[i].id.substring(1);
        element.attribute.parent.push(parent)
      }

    }
    // JsonHelper.appendData(SwtionElement.treelist, element, "id", parentNode.id);
    // console.log(SwtionElement.treelist)
    // element.parent = parentNode;
    // let element = new CollectElement();
    parentNode.children.push(element);
    return element;
  }
  public removeTreeNode(id: string, root: IBaseElement): boolean {
    root = root ? root : SwtionElement.treelist;
    let swtionProc = new SwtionProcess();
    let find = false;
    let node = this.findTreeNode(id, root);
    findAttribute(node);

    function findAttribute(source: IBaseElement) {
      for (let i = 0; i < source.attribute.parent.length; i++) {
        if (!swtionProc.findEleInProcess(source.attribute.parent[i].id)) {
          find = true;
          return true;
        }
      }
      for (let i = 0; i < source.attribute.self.length; i++) {
        if (!swtionProc.findEleInProcess(source.attribute.self[i].id)) {
          find = true;
          return true;
        }
      }
      for (let i = 0; i < source.children.length; i++) {
        let result = findAttribute(source.children[i]);
        if (result) {
          return true;
        }
      }
    }
    if (!find) {
      JsonHelper.removeDataByKey(root, "id", id);
    }
    return !find;
  }
}
