import { attributeType, elementAttributeType } from "@/enums/attribute";
import { AttributeValue } from "@/model/Attribute";
import SwtionElement from "@/plugins/element";
import SwtionProcess from "./process";
import NodeHelper from "@/common/nodeHelper";
export default class SwtionAttribute {
  public appendAttributeValue(
    prefix: attributeType,
    value: IAttributeValue,
    nodeId: string,
    source?: Array<any>
  ): IAttributeValue {
    if (!source) {
      source = this.loadAttribute(prefix, nodeId);
    }
    // value.id = this.createId(prefix, nodeId, source);
    value.id = NodeHelper.createId(value.type.toString());

    switch (value.type) {
      case elementAttributeType.checkbox:
        value.value = [];
        break;
    }
    source.push(value);
    this.setRef(nodeId, source);
    this.setAttribute(prefix, value, nodeId);
    return value;
  }
  public appendOption(
    prefix: attributeType,
    value: IOption,
    nodeId: string,
    source: Array<IOption>
  ): Array<IOption> {
    // value.id = this.createId(prefix, nodeId, source);
    value.id = NodeHelper.createId();
    source.push(value);
    return source;
  }
  // public createId(prefix: attributeType, nodeId: string, source: Array<any>) {
  //   let key = 0;
  //   if (source.length > 0) {
  //     let lastId = source[source.length - 1].id;
  //     let splitIds = lastId.split("-");
  //     key = Number(splitIds[splitIds.length - 1]) + 1;
  //   }
  //   return nodeId + "-" + prefix + "-" + key;
  // }
  private loadAttribute(prefix: attributeType, nodeId: string): Array<any> {
    let source = new Array<any>();
    const swtionEle = new SwtionElement();
    const node = swtionEle.findTreeNode(nodeId);
    switch (prefix) {
      case attributeType.attribute:
        source = (node as IBaseElement).attribute.self;
        break;
    }
    return source;
  }
  public findAttributeValueById(id: string, source: Array<any>) {
    for (let i = 0; i < source.length; i++) {
      if (source[i].id == id) {
        return source[i];
      }
    }
  }
  public setRef(nodeId: string, allValue: Array<IAttributeValue>) {
    // console.log(allValue.toString())
    // let newSelf = allValue.concat();
    // console.log(newSelf===allValue)
    // for (let i = 0; i < newSelf.length; i++) {
    //   if (newSelf[i].type != elementAttributeType.attribute) {
    //     let id = newSelf[i].id;
    //     // let temp = SwtionAttribute.clone(newSelf[i]);
    //     newSelf[i].id = elementAttributeType.attribute.toString() + id.substring(1);
    //     // temp.id = id
    //     // newSelf.push(temp);
    //   }
    //   else {
    //     newSelf.removeAt(i);
    //     i--;
    //   }
    // }
    let newSelf = new Array<IAttributeValue>();
    for (let i = 0; i < allValue.length; i++) {
      if (allValue[i].type != elementAttributeType.attribute) {
        let id = allValue[i].id;
        let temp = SwtionAttribute.clone(allValue[i]);
        temp.id = elementAttributeType.attribute.toString() + id.substring(1);
        newSelf.push(temp);
      }
    }
    const swtionEle = new SwtionElement();
    checkNode(swtionEle.loadTree())
    function checkNode(node: IBaseElement) {
      for (let i = 0; i < node.attribute.self.length; i++) {
        let self = node.attribute.self[i];
        if (self.ref.id == nodeId) {
          self.ref.attribute.self = new Array<IAttributeValue>().concat(newSelf);
        }
        // node.attribute.self[i] = self;
      }
      if (node.children && node.children.length > 0) {
        for (let i = 0; i < node.children.length; i++) {
          checkNode(node.children[i]);
        }
      }
    }
  }
  public setAttribute(
    prefix: attributeType,
    value: IAttributeValue,
    nodeId: string
  ): IBaseElement[] {
    const swtionEle = new SwtionElement();
    const childNodes = swtionEle.findChildTreeNode(nodeId);
    switch (prefix) {
      case attributeType.attribute:
        for (let i = 0; i < childNodes.length; i++) {
          if (value.type !== elementAttributeType.attribute) {
            let parent = SwtionAttribute.clone(value);
            parent.parentId = value.id.substring(1);;
            childNodes[i].attribute.parent.push(parent);
          }
        }
        break;
    }
    return childNodes;
  }
  public editAttributeValue(id: string, newValue: any, source: Array<any>) {
    let item = this.findAttributeValueById(id, source);
    let i = source.indexOf(item);
    source.removeAt(i);
    source.insertAt(i, newValue);
  }
  public removeAttribute(
    prefix: attributeType,
    value: IAttributeValue,
    nodeId: string,
    source?: Array<any>
  ): boolean {
    const swtionEle = new SwtionElement();
    const childNodes = swtionEle.findChildTreeNode(nodeId);
    if (!source) {
      source = this.loadAttribute(prefix, nodeId);
    }
    let swtionProc = new SwtionProcess();
    if (swtionProc.findEleInProcess(value.id)) {
      switch (prefix) {
        case attributeType.attribute:
          for (let i = 0; i < source.length; i++) {
            if (source[i].id == value.id) {
              source.removeAt(i);
              i--;
            }
          }
          for (let i = 0; i < childNodes.length; i++) {
            let parents = childNodes[i].attribute.parent;
            for (let j = 0; j < parents.length; j++) {
              if (parents[j].id == value.id) {
                parents.removeAt(j);
                j--;
              }
            }
          }
          return true;
      }
    }
    return false;
  }
  public static clone(source: IAttributeValue, all = false): IAttributeValue {
    let result = new AttributeValue();
    result.name = source.name;
    result.id = all ? source.id : NodeHelper.createId();
    result.type = source.type;
    result.options = source.options;
    result.ref = source.ref;
    result.prop = source.prop;
    if (source.parentId) {
      result.parentId = source.parentId;
    }
    return result;
  }
}
