import { elementAttributeType } from "@/enums/attribute";
import { CollectElement } from "@/model/Element";
import SwtionAttribute from "@/plugins/attribute";
import SwtionElement from "@/plugins/element";

export default class attributeOptions {
  private swtionEle = new SwtionElement();
  public loadAttributeOption(id?: string) {
    let eleList = new CollectElement();
    if (id) {
      eleList = this.swtionEle.findTreeNode(id);
    }
    else {
      eleList = this.swtionEle.loadTree();
    }
    const attributeOptions = [
      {
        id: eleList.id,
        name: eleList.name,
        attribute: eleList.attribute
      }
    ]
    if (eleList.children.length > 0) {
      (attributeOptions[0] as any).children = getChildren(eleList.children);
    }
    function getChildren(source: Array<IBaseElement>): Array<IBaseElement> {
      let result = new Array<any>();
      for (let i = 0; i < source.length; i++) {
        let item = new Object() as any;
        item.id = source[i].id;
        item.name = source[i].name;
        item.type = source[i].type;
        item.attribute = source[i].attribute;
        if (source[i].children.length > 0) {
          item.children = getChildren(source[i].children);
        }
        result.push(item);
      }
      return result;
    }
    return attributeOptions;
  }
  public loadAttributeValueOption(needRef = true) {
    const eleList = this.swtionEle.loadTree();
    // const attributeOptions = [
    //   {
    //     id: ifType.attribute,
    //     name: ifType[ifType.attribute],
    //     children: [
    //       {
    //         id: eleList.id,
    //         name: eleList.name,
    //         children: this.loadEleOption(eleList),
    //       },
    //     ],
    //   },
    // ];

    const attributeOptions = [
      {
        id: eleList.id,
        name: eleList.name,
        children: this.loadEleOption(eleList, needRef),
      }
    ]
    // if (needRef) {
    //   console.log(eleList, attributeOptions)
    // }
    return attributeOptions;
  }
  // private loadEleOptionWithoutRef() {

  // }
  private loadEleOption(source: IBaseElement, needRef = true) {
    let _this = this;
    return eleToOptions(source);
    function eleToOptions(eleList: IBaseElement): any {
      let items = _this.attrToOptions(eleList);
      if (needRef) {
        for (let i = 0; i < items[0].children.length; i++) {
          if (items[0].children[i].type == elementAttributeType.attribute) {
            let children = _this.attrToOptions(items[0].children[i].ref)
            for (let j = 0; j < children.length; j++) {
              let child = children[j].children;
              let childs = [];
              for (let v = 0; v < child.length; v++) {
                if (child[v].type !== elementAttributeType.attribute) {
                  childs.push(child[v]);
                }
              }
              children[j].children = childs;
            }
            (items[0].children[i] as any).children = children;
          }
        }
      }
      for (let i = 0; i < eleList.children.length; i++) {
        items.push({
          id: eleList.children[i].id,
          name: eleList.children[i].name,
          children: eleToOptions(eleList.children[i]) as any,
        });
      }
      return items;
    }

  }
  private attrToOptions(eleList: IBaseElement) {
    let items = [
      {
        id: "self",
        name: "自有属性",
        children: initAttributeValue(eleList.attribute.self),
      },
      {
        id: "parent",
        name: "继承属性",
        children: initAttributeValue(eleList.attribute.parent),
      },
    ];
    return items;
    function initAttributeValue(source: IAttributeValue[]): IAttributeValue[] {
      let result = new Array<IAttributeValue>();
      for (let i = 0; i < source.length; i++) {
        result.push(SwtionAttribute.clone(source[i], true));
      }
      return result;
    }
  }
}