class WaterFlowControlView {
  constructor(options) {
    this.linkDataArray = [];
    this.browserArray = [];
    this.hBarArray = [];
    this.allClose = [];
    this.myDiagram = null;
  }

  /** 获取处理好的json************************************************* */
  waterFlowControlByDiagramJson(oldJson, myDiagrams) {
    this.myDiagram = myDiagrams;
    this.hBarArray = this.getAllNodeByCategory('HBar');
    if (!this.hBarArray.length) return null;
    let newnodeDataArray = JSON.parse(this.myDiagram.model.toJson()).nodeDataArray;
    this.newJson = JSON.parse(this.myDiagram.model.toJson());
    let oldnodeDataArray = oldJson.nodeDataArray;
    let _oldControlArray = oldnodeDataArray.filter(function (item) {
      return item.switch == '是' && item.category != 'switchCase';
    });
    let _newControlArray = newnodeDataArray.filter(function (item) {
      return item.switch == '是' && item.category != 'switchCase' && item.realVal != '--';
    });
    let diffentArr = this.diffent(_oldControlArray, _newControlArray, 'key', 'switchState');
    if (!diffentArr.length) return null;
    this.linkDataArray = JSON.parse(this.myDiagram.model.toJson()).linkDataArray;
    this.flag = false;
    for (let j = 0; j < newnodeDataArray.length; j++) {
      if (
        newnodeDataArray[j].switch == '是' &&
        newnodeDataArray[j].category != 'switchCase' &&
        oldnodeDataArray[j].switchState &&
        newnodeDataArray[j].realVal != '--'
      ) {
        //代表控制属性值有修改
        this.flag = true;
        let controlValue = newnodeDataArray[j].switchState;
        let isHavingDash = controlValue == '开' ? true : false;
        let key = newnodeDataArray[j].key;
        this.browserArray = [];
        this.browserArray.push(key);
        this.changWaterFlow(key, isHavingDash);
        //判断此条线路上面是否还有其他的控制开关
        let otherControl = false;
        for (let k = 0; k < newnodeDataArray.length; k++) {
          for (let i = 1; i < this.browserArray.length; i++) {
            if (
              newnodeDataArray[k].key == this.browserArray[i] &&
              newnodeDataArray[k].switchState == '关' &&
              controlValue == '开'
            ) {
              otherControl = true;
              break;
            }
          }
        }
        if (!otherControl) {
          this.newJson.linkDataArray = this.linkDataArray;
          //如果从全关状态下 开启摸个按钮
          this.hBarArray.map((obj, index) => {
            if (obj == key) {
              // 合管和状态管是同一个元器件
              this.allClose[index] = isHavingDash;
              for (let temp of this.newJson.linkDataArray) {
                if (temp.from == obj) {
                  this.browserArray = [];
                  this.browserArray.push(temp.to);
                  this.changWaterFlow(temp.to, isHavingDash);
                  this.newJson.linkDataArray = this.linkDataArray;
                }
                if (temp.to == obj) {
                  this.browserArray = [];
                  this.browserArray.push(temp.from);
                  this.changWaterFlow(temp.from, isHavingDash);
                  this.newJson.linkDataArray = this.linkDataArray;
                }
              }
            } else if (this.allClose[index] && controlValue == '开') {
              //合管周边管子水流开启
              this.allClose[index] = false; //现在不是全部关闭
              if (this.isIn(obj, key)) {
                for (let temp of this.newJson.linkDataArray) {
                  if (temp.from == obj) {
                    this.browserArray = [];
                    this.browserArray.push(temp.to);
                    this.changWaterFlow(temp.to, isHavingDash);
                    this.newJson.linkDataArray = this.linkDataArray;
                  }
                }
              } else if (this.isOut(obj, key)) {
                for (let temp of this.newJson.linkDataArray) {
                  if (temp.to == obj) {
                    this.browserArray = [];
                    this.browserArray.push(temp.from);
                    this.changWaterFlow(temp.from, isHavingDash);
                    this.newJson.linkDataArray = this.linkDataArray;
                  }
                }
              }
            }
          });
        }
      }
    }
    if (!this.flag) {
      return null;
    }
    //判断合节点的连接个数
    let te = [];
    this.hBarArray.map((data) => {
      te.push(data);
    });
    for (let z = 0; z < te.length; z++) {
      let key = te[z];
      let open = 0;
      let close = 0;
      let inArray = [];
      let outArray = [];
      this._hBarArray = this.hBarArray.concat();
      delete this._hBarArray[z];
      for (let temp of this.newJson.linkDataArray) {
        if (temp.from == key) inArray.push(temp);
        if (temp.to == key) outArray.push(temp);
      }
      //全部关闭
      let inarrTemp = inArray.filter((obj) => {
        return obj.isHavingDash == false;
      });
      if (inarrTemp.length == inArray.length && inArray.length != 0) {
        this.browserArray = [];
        this.browserArray.push(key);
        this.changWaterFlow(key, false, this._hBarArray);
        this.newJson.linkDataArray = this.linkDataArray;
        this.allClose[z] = true;
      }
      let outarrTemp = outArray.filter((obj) => {
        return obj.isHavingDash == false;
      });
      if (outarrTemp.length == outArray.length && outArray.length != 0) {
        this.browserArray = [];
        this.browserArray.push(key);
        this.changWaterFlow(key, false, this._hBarArray);
        this.newJson.linkDataArray = this.linkDataArray;
        this.allClose[z] = true;
      }
    }
    this.newJson.nodeDataArray.forEach(
      function (item) {
        for (let i = 0; i < te.length; i++) {
          if (item.key == te[i]) {
            item.typeDash = this.allClose[i];
            return false;
          }
        }
      }.bind(this),
    );
    return this.newJson;
  }

  /** 获取所有合管id************************************************* */
  getAllNodeByCategory(category) {
    let nodeDataArray = JSON.parse(this.myDiagram.model.toJson()).nodeDataArray;
    let retVal = [];
    for (let j = 0; j < nodeDataArray.length; j++) {
      if (
        nodeDataArray[j].category == category ||
        (nodeDataArray[j].hbControl && nodeDataArray[j].hbControl == '是')
      ) {
        retVal.push(nodeDataArray[j].key);
        if (this.allClose[retVal.length - 1] == undefined) this.allClose.push(false);
      }
    }
    return retVal;
  }

  /***/
  changWaterFlow(key, isHavingDash, hBarArray) {
    hBarArray = hBarArray ? hBarArray : this.hBarArray.concat();
    if (this.isInArray(hBarArray, key)) {
      //判断key是否是合管
      return;
    }
    for (let j = 0; j < this.linkDataArray.length; j++) {
      let temp = this.linkDataArray[j];
      if (temp.from == key && temp.from != temp.to) {
        this.linkDataArray[j].isHavingDash = isHavingDash;
        if (!this.isInArray(this.browserArray, temp.to)) {
          this.browserArray.push(temp.to);
          this.changWaterFlow(temp.to, isHavingDash, hBarArray);
        }
      } else if (temp.to == key && temp.to != temp.from) {
        this.linkDataArray[j].isHavingDash = isHavingDash;
        if (!this.isInArray(this.browserArray, temp.from)) {
          this.browserArray.push(temp.from);
          this.changWaterFlow(temp.from, isHavingDash, hBarArray);
        }
      }
    }
  }

  /***/
  isInArray(array, key) {
    let retValue = false;
    for (let j = 0; j < array.length; j++) {
      if (array[j] == key) {
        retValue = true;
        break;
      }
    }
    return retValue;
  }

  isIn(hBarKey, nodeKey) {
    let val = false;
    if (hBarKey == nodeKey) {
      //判断key是否是合管
      val = true;
      return val;
    }
    for (let j = 0; j < this.linkDataArray.length; j++) {
      let temp = this.linkDataArray[j];
      if (temp.from && temp.from == nodeKey && temp.from != temp.to) {
        val = val == true ? true : this.isIn(hBarKey, temp.to);
      }
    }
    return val;
  }

  isOut(hBarKey, nodeKey) {
    let val = false;
    if (hBarKey == nodeKey) {
      //判断key是否是合管
      val = true;
      return val;
    }
    for (let j = 0; j < this.linkDataArray.length; j++) {
      let temp = this.linkDataArray[j];
      if (temp.to && temp.to == nodeKey && temp.to != temp.from) {
        val = val == true ? true : this.isOut(hBarKey, temp.from);
      }
    }
    return val;
  }

  diffent(fArr, cArr, key, field) {
    let diffRes = [];
    let fDatas = [];
    let cDatas = [];
    for (let i in fArr) {
      let flg = false;
      for (let j in cArr) {
        if (cArr[j][key] === fArr[i][key] && cArr[j][field] === fArr[i][field]) {
          flg = true;
          break;
        }
      }
      if (!flg) {
        fDatas.push(fArr[i]);
      }
    }
    for (let i in cArr) {
      let flg = false;
      for (let j in fArr) {
        if (fArr[j][field] === cArr[i][field] && fArr[j][field] === cArr[i][field]) {
          flg = true;
          break;
        }
      }
      if (!flg) {
        cDatas.push(cArr[i]);
      }
    }
    diffRes.push(...cDatas.concat(fDatas));
    return diffRes;
  }
}

export default WaterFlowControlView;
