import { Message } from 'element-ui'
/*
* ********************
* 通用疏散布局方法封装
* ********************
*/

/*
******************* mapList-数据格式 *******************
  { name:"实验室房间",
    y: 101,
    x: 1,
    w: 100,
    h: 100,
    type: '1',//1房间2楼道3疏散通道
    direction: '1',//1上2右3下4左
    doorList:[{
        h:40,
        toward:"right",
        type:"door",
        w:10,
        x:90,
        y:22,
      }],
  },
  {
    name:"疏散通道",
    y: 101,
    x: 801,
    w: 100,
    h: 100,
    type: '3',//1房间2楼道3疏散通道
    direction: '1',//1上2右3下4左
    doorList:[
      {
        h:40,
        toward:"left",
        type:"door",
        w:10,
        x:0,
        y:34,
      }
    ],
  },
  {
    name:"l01",
    y: 101,
    x: 101,
    w: 100,
    h: 500,
    type: '2',//1房间2楼道3疏散通道
    direction: '1',//1上2右3下4左
    lightList:[
      { type:'lab',w:4,h:4,x:100,y:100},//实验室门点
      { type:'escape',w:4,h:4,x:100,y:100,},//疏散通道门点
      { type:'light',state:'',w:100,h:100,x:100,y:100,},//灯点
      { type:'traffic',w:4,h:4,x:lx,y:ly,},//交通点
      { type:'connect',w:4,h:4,x:0,y:100}//连接点
    ],
  },
*/

/************************************全局变量/刷新方法************************************/
  //边界位置
let borderLeft = null
let borderTop = null
//画布/楼层尺寸
let layerWidth = null
let layerHeight = null
//房间尺寸限制
let minHeight = null
let maxHeight = null
let minWidth = null
let maxWidth = null
//门尺寸数据
let doorHeight = null
let doorWidth = null
//灯尺寸数据
let minLightNum = null
let maxLightNum = null
//交通点检测时触手范围
let tentaclesLength = null
//缩放形状判断
let zoomType = null

export function setJsData(data) {
  borderLeft = data.borderLeft
  borderTop = data.borderTop
  layerWidth = data.layerWidth
  layerHeight = data.layerHeight
  minHeight = data.minHeight
  maxHeight = data.maxHeight
  minWidth = data.minWidth
  maxWidth = data.maxWidth
  doorWidth = data.doorWidth
  doorHeight = data.doorHeight
  minLightNum = data.minLightNum
  maxLightNum = data.maxLightNum
  tentaclesLength = data.tentaclesLength
}

/************************************生成点/点连接 相关************************************/

/*
* 计算目标交通点位置
* 传入:newMapList-布局数据
* 返回:处理好的数据
* 示例:
*   let obj = addDoorPoint(newMapList)
*/
export function addDoorPoint(newMapList) {
  let labNum = 0;
  let escapeNum = 0;
  let corridorNum = 0;
  let lightNum = 0;
  let evacuationList = JSON.parse(JSON.stringify(newMapList));
  for (let i = 0; i < newMapList.length; i++) {
    if (newMapList[i].type == 1 || newMapList[i].type == 3) {
      if(newMapList[i].type == 1){
        newMapList[i].key = 'lab'+labNum;
        evacuationList[i].key = 'lab'+labNum;
        labNum++
      }else if(newMapList[i].type == 3){
        newMapList[i].key = 'escape'+escapeNum;
        evacuationList[i].key = 'escape'+escapeNum;
        escapeNum++
      }
      //生成门点
      let x = 0
      let y = 0
      if (newMapList[i].doorList[0].toward == 'top') {
        x = newMapList[i].x + newMapList[i].doorList[0].x + (doorWidth / 2)
        y = newMapList[i].y - tentaclesLength
      } else if (newMapList[i].doorList[0].toward == 'bottom') {
        x = newMapList[i].x + newMapList[i].doorList[0].x + (doorWidth / 2)
        y = newMapList[i].y + newMapList[i].h + tentaclesLength
      } else if (newMapList[i].doorList[0].toward == 'left') {
        x = newMapList[i].x - tentaclesLength
        y = newMapList[i].y + newMapList[i].doorList[0].y + (doorHeight / 2)
      } else if (newMapList[i].doorList[0].toward == 'right') {
        x = newMapList[i].x + newMapList[i].w + tentaclesLength
        y = newMapList[i].y + newMapList[i].doorList[0].y + (doorHeight / 2)
      }
      for (let j = 0; j < newMapList.length; j++) {
        if (newMapList[j].type == 2) {
          if (x > newMapList[j].x && x < (newMapList[j].x + newMapList[j].w) &&
            y > newMapList[j].y && y < (newMapList[j].y + newMapList[j].h)) {
            let lx = null
            let ly = null
            if (newMapList[i].doorList[0].toward == 'top') {
              let obj = calculateDoorPoint(newMapList[j], newMapList[i], 'top')
              lx = obj.x
              ly = obj.y
            } else if (newMapList[i].doorList[0].toward == 'bottom') {
              let obj = calculateDoorPoint(newMapList[j], newMapList[i], 'bottom')
              lx = obj.x
              ly = obj.y
            } else if (newMapList[i].doorList[0].toward == 'left') {
              let obj = calculateDoorPoint(newMapList[j], newMapList[i], 'left')
              lx = obj.x
              ly = obj.y
            } else if (newMapList[i].doorList[0].toward == 'right') {
              let obj = calculateDoorPoint(newMapList[j], newMapList[i], 'right')
              lx = obj.x
              ly = obj.y
            }
            newMapList[j].lightList.push({
              // key:newMapList[i].type == 1 ? 'lab'+labNum : 'escape'+escapeNum,
              key:newMapList[i].key,
              type: newMapList[i].type == 1 ? 'lab' : 'escape',
              w: 4,
              h: 4,
              x: lx,
              y: ly
            })
          }
        }
      }
    } else if (newMapList[i].type == 2) {
      evacuationList[i].key = 'corridor'+corridorNum;
      corridorNum++
      //生成交通点
      let x1 = null
      let y1 = null
      let x2 = null
      let y2 = null
      if (newMapList[i].w > newMapList[i].h) {
        x1 = newMapList[i].x - tentaclesLength
        y1 = newMapList[i].y + newMapList[i].h / 2
        x2 = newMapList[i].x + newMapList[i].w + tentaclesLength
        y2 = newMapList[i].y + newMapList[i].h / 2
      } else {
        x1 = newMapList[i].x + newMapList[i].w / 2
        y1 = newMapList[i].y - tentaclesLength
        x2 = newMapList[i].x + newMapList[i].w / 2
        y2 = newMapList[i].y + newMapList[i].h + tentaclesLength
      }
      for (let j = 0; j < newMapList.length; j++) {
        if (newMapList[j].type == 2) {
          let lx = null
          let ly = null
          if (x1 > newMapList[j].x && x1 < (newMapList[j].x + newMapList[j].w) &&
            y1 > newMapList[j].y && y1 < (newMapList[j].y + newMapList[j].h)) {
            let obj = calculateTrafficPoint(x1, y1, newMapList[j])
            lx = obj.x
            ly = obj.y
            let num = 0
            for (let o = 0; o < newMapList[j].lightList.length; o++) {
              if (newMapList[j].lightList[o].x == lx && newMapList[j].lightList[o].y == ly) {
                num++
              }
            }
            if (num == 0) {
              newMapList[j].lightList.push({
                type: 'traffic',
                w: 4,
                h: 4,
                x: lx,
                y: ly
              })
            }
            if (newMapList[i].w > newMapList[i].h) {
              newMapList[i].lightList.push({
                type: 'connect',
                w: 4,
                h: 4,
                x: 0,
                y: newMapList[i].h / 2
              })
            } else {
              newMapList[i].lightList.push({
                type: 'connect',
                w: 4,
                h: 4,
                x: newMapList[i].w / 2,
                y: 0
              })
            }
          } else if (x2 > newMapList[j].x && x2 < (newMapList[j].x + newMapList[j].w) &&
            y2 > newMapList[j].y && y2 < (newMapList[j].y + newMapList[j].h)) {
            let obj = calculateTrafficPoint(x2, y2, newMapList[j])
            lx = obj.x
            ly = obj.y
            let num = 0
            for (let o = 0; o < newMapList[j].lightList.length; o++) {
              if (newMapList[j].lightList[o].x == lx && newMapList[j].lightList[o].y == ly) {
                num++
              }
            }
            if (num == 0) {
              newMapList[j].lightList.push({
                type: 'traffic',
                w: 4,
                h: 4,
                x: lx,
                y: ly
              })
            }
            if (newMapList[i].w > newMapList[i].h) {
              newMapList[i].lightList.push({
                type: 'connect',
                w: 4,
                h: 4,
                x: newMapList[i].w - 4,
                y: newMapList[i].h / 2
              })
            } else {
              newMapList[i].lightList.push({
                type: 'connect',
                w: 4,
                h: 4,
                x: newMapList[i].w / 2,
                y: newMapList[i].h - 4
              })
            }
          }
        }
      }
      for (let s = 0; s < newMapList[i].lightList.length; s++){
        if( newMapList[i].lightList[s].type == "light"){
          newMapList[i].lightList[s].key = 'light'+lightNum;
          evacuationList[i].lightList[s].key = 'light'+lightNum;
          lightNum++
        }
      }
    }
  }
  return { mapList: newMapList, newList: connectionPoint(newMapList),evacuationList:evacuationList }
}

/*
* 计算目标交通点位置
* 传入:x-落点X,y-落点Y,obj-目标数据,
* 返回:一个对象,里面包含x与y
* 示例: (本方法没有再外部引用,只在本JS内由 addDoorPoint 方法引用)
*   let obj = calculateTrafficPoint(x1,y1,newMapList[j]);
*/
export function calculateTrafficPoint(x, y, obj) {
  let newObj = { x: null, y: null }
  if (obj.w > obj.h) {
    newObj.x = x - obj.x
    newObj.y = obj.h / 2
  } else {
    newObj.x = obj.w / 2
    newObj.y = y - obj.y
  }
  return newObj
}

/*
* 计算门点位置
* 传入:bigObj-父级对象,minObj-子级对象,type-朝向,
* 返回:一个对象,里面包含x与y
* 示例: (本方法没有再外部引用,只在本JS内由 addDoorPoint 方法引用)
*   let obj = calculateDoorPoint(newMapList[j],newMapList[i],'right')
*/
export function calculateDoorPoint(bigObj, minObj, type) {
  //bigObj  范围大的一方   minObj范围小的一方
  let obj = { x: null, y: null }
  if (bigObj.w > bigObj.h) {
    if (type == 'top' || type == 'bottom') {
      obj.x = minObj.x - bigObj.x + minObj.doorList[0].x + doorWidth / 2
      obj.y = bigObj.h / 2
    } else if (type == 'left') {
      obj.x = bigObj.w - 6
      obj.y = bigObj.h / 2
    } else if (type == 'right') {
      obj.x = 2
      obj.y = bigObj.h / 2
    }
  } else {
    if (type == 'top') {
      obj.x = bigObj.w / 2
      obj.y = bigObj.h - 6
    } else if (type == 'bottom') {
      obj.x = bigObj.w / 2
      obj.y = 2
    } else if (type == 'left' || type == 'right') {
      obj.x = bigObj.w / 2
      obj.y = minObj.y - bigObj.y + minObj.doorList[0].y + doorHeight / 2
    }
  }
  return obj
}

/*
* 连接点路径
* 传入:newMapList-布局数据
* 返回:返回连接好路径的数组
* 示例: (本方法没有再外部引用,只在本JS内由 addDoorPoint 方法引用)
*   connectionPoint(newMapList);
*/
export function connectionPoint(newMapList) {
  let list = []
  let doorNum = 0
  let escapeNum = 0
  let lightNum = 0
  let trafficNum = 0
  let trafficNumOne = 0
  // 绑定名称
  for (let i = 0; i < newMapList.length; i++) {
    if (newMapList[i].type == 2) {
      for (let j = 0; j < newMapList[i].lightList.length; j++) {
        if (newMapList[i].lightList[j].type == 'lab') {
          // newMapList[i].lightList[j].name = 'lab' + doorNum
          newMapList[i].lightList[j].name = newMapList[i].lightList[j].key
          newMapList[i].lightList[j].type = 'lab'
          doorNum++
        }
        if (newMapList[i].lightList[j].type == 'escape') {
          // newMapList[i].lightList[j].name = 'escape' + escapeNum
          newMapList[i].lightList[j].name = newMapList[i].lightList[j].key
          newMapList[i].lightList[j].type = 'escape'
          escapeNum++
        } else if (newMapList[i].lightList[j].type == 'light') {
          // newMapList[i].lightList[j].name = 'light' + lightNum
          newMapList[i].lightList[j].name = newMapList[i].lightList[j].key
          newMapList[i].lightList[j].type = 'light'
          lightNum++
        } else if (newMapList[i].lightList[j].type == 'traffic') {
          newMapList[i].lightList[j].name = 'traffic' + trafficNum
          newMapList[i].lightList[j].type = 'traffic'
          trafficNum++
        } else if (newMapList[i].lightList[j].type == 'connect') {
          newMapList[i].lightList[j].name = 'connect' + trafficNumOne
          newMapList[i].lightList[j].type = 'connect'
          trafficNumOne++
        }
      }
    }
  }
  for (let i = 0; i < newMapList.length; i++) {
    if (newMapList[i].type == 2) {
      for (let j = 0; j < newMapList[i].lightList.length; j++) {
        list.push(calculateAssociatedLength(newMapList, newMapList[i], newMapList[i].lightList[j]))
      }
    }
  }
  return list
}

/*
* 计算关联/长度
* 传入:newMapList-布局数据,bigObj-父级对象,minObj-子级对象
* 返回:处理好关系的对象
* 示例: (本方法没有再外部引用,只在本JS内由 connectionPoint 方法引用)
*   calculateAssociatedLength(newMapList,newMapList[i],newMapList[i].lightList[j])
*/
export function calculateAssociatedLength(newMapList, bigObj, minObj) {
  let obj = {
    pointName: minObj.name,
    pointDistance: -1,
    pointAttribute: minObj.type == 'escape' ? 1 : (minObj.type == 'light' ? 2 : minObj.type == 'lab' ? 4 : 3),
    pointVOList: []
  }
  if(minObj.type == 'escape' || minObj.type == 'lab'){
    obj.key = minObj.key;
  }else if(minObj.type == 'light'){
    obj.key = minObj.key;
  }
  //按顺序写入假数据
  for (let i = 0; i < newMapList.length; i++) {
    if (newMapList[i].type == 2) {
      for (let j = 0; j < newMapList[i].lightList.length; j++) {
        obj.pointVOList.push({
          pointName: newMapList[i].lightList[j].name,
          pointDistance: newMapList[i].lightList[j].name === minObj.name ? 0 : 65535,
          pointAttribute: newMapList[i].lightList[j].type == 'escape' ? 1 : (newMapList[i].lightList[j].type == 'light' ? 2 : newMapList[i].lightList[j].type == 'lab' ? 4 : 3)
        })
      }
    }
  }
  const x = minObj.x
  const y = minObj.y
  //行径方向判定
  if (bigObj.w > bigObj.h) {
    //横向查找最近点
    let min = null
    let max = null
    for (let i = 0; i < bigObj.lightList.length; i++) {
      if (bigObj.lightList[i].x > x) {
        //右最近
        if (max) {
          if (max.x > bigObj.lightList[i].x) {
            if (bigObj.lightList[i].name !== minObj.name) {
              if (bigObj.lightList[i].type == 'light') {
                if (bigObj.lightList[i].state != '3') {
                  max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
                }
              } else {
                max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            }
          }
        } else {
          if (bigObj.lightList[i].name !== minObj.name) {
            if (bigObj.lightList[i].type == 'light') {
              if (bigObj.lightList[i].state != '3') {
                max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            } else {
              max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
            }
          }
        }
      } else {
        //左最近
        if (min) {
          if (min.x < bigObj.lightList[i].x) {
            if (bigObj.lightList[i].name !== minObj.name) {
              if (bigObj.lightList[i].type == 'light') {
                if (bigObj.lightList[i].state != '4') {
                  min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
                }
              } else {
                min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            }
          }
        } else {
          if (bigObj.lightList[i].name !== minObj.name) {
            if (bigObj.lightList[i].type == 'light') {
              if (bigObj.lightList[i].state != '4') {
                min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            } else {
              min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
            }
          }
        }
      }
    }
    if (min != null) {
      obj.pointVOList[lookingTargetReturnSubscript(min.name, obj.pointVOList)].pointDistance = minObj.x - min.x
    }
    if (max != null) {
      obj.pointVOList[lookingTargetReturnSubscript(max.name, obj.pointVOList)].pointDistance = max.x - minObj.x
    }
    //如果是交通端点-进行跨域连接
    if (minObj.type == 'connect') {
      for (let j = 0; j < newMapList.length; j++) {
        if (newMapList[j].type == 2) {
          for (let k = 0; k < newMapList[j].lightList.length; k++) {
            if (newMapList[j].lightList[k].type == 'traffic') {
              let lx = newMapList[j].x + newMapList[j].lightList[k].x
              let ly = newMapList[j].y + newMapList[j].lightList[k].y
              if (minObj.x > (bigObj.w / 2)) {
                //向右延展
                if (lx > (bigObj.x + bigObj.w) && lx < (bigObj.x + bigObj.w + minWidth) && ly > (bigObj.y + minObj.y - 1) && ly < (bigObj.y + minObj.y + 1)) {
                  obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = lx - bigObj.x - minObj.x
                }
              } else {
                //向左延展
                if (lx > (bigObj.x - minWidth) && lx < bigObj.x && ly > (bigObj.y + minObj.y - 1) && ly < (bigObj.y + minObj.y + 1)) {
                  obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = (bigObj.x + minObj.x) - lx
                }
              }
            }
          }
        }
      }
    } else if (minObj.type == 'traffic') {
      for (let j = 0; j < newMapList.length; j++) {
        if (newMapList[j].type == 2) {
          for (let k = 0; k < newMapList[j].lightList.length; k++) {
            if (newMapList[j].lightList[k].type == 'connect') {
              let lx = newMapList[j].x + newMapList[j].lightList[k].x
              let ly = newMapList[j].y + newMapList[j].lightList[k].y
              //向下延展
              if (lx >= (bigObj.x + minObj.x - 1) && lx <= (bigObj.x + minObj.x + 1) && ly >= (bigObj.y + bigObj.h) && ly <= (bigObj.y + bigObj.h + tentaclesLength)) {
                obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = ly - bigObj.y - minObj.y
              }
              //向上延展
              if (lx >= (bigObj.x + minObj.x - 1) && lx <= (bigObj.x + minObj.x + 1) && ly >= bigObj.y - tentaclesLength && ly <= bigObj.y) {
                obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = bigObj.y - ly
              }
            }
          }
        }
      }
    }
  } else {
    //竖向查找最近点
    let min = null
    let max = null
    for (let i = 0; i < bigObj.lightList.length; i++) {
      if (bigObj.lightList[i].y > y) {
        //下最近
        if (max) {
          if (max.y > bigObj.lightList[i].y) {
            if (bigObj.lightList[i].name !== minObj.name) {
              if (bigObj.lightList[i].type == 'light') {
                if (bigObj.lightList[i].state != '1') {
                  max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
                }
              } else {
                max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            }
          }
        } else {
          if (bigObj.lightList[i].name !== minObj.name) {
            if (bigObj.lightList[i].type == 'light') {
              if (bigObj.lightList[i].state != '1') {
                max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            } else {
              max = JSON.parse(JSON.stringify(bigObj.lightList[i]))
            }
          }
        }
      } else {
        //上最近
        if (min) {
          if (min.y < bigObj.lightList[i].y) {
            if (bigObj.lightList[i].name !== minObj.name) {
              if (bigObj.lightList[i].type == 'light') {
                if (bigObj.lightList[i].state != '2') {
                  min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
                }
              } else {
                min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            }
          }
        } else {
          if (bigObj.lightList[i].name !== minObj.name) {
            if (bigObj.lightList[i].type == 'light') {
              if (bigObj.lightList[i].state != '2') {
                min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
              }
            } else {
              min = JSON.parse(JSON.stringify(bigObj.lightList[i]))
            }
          }
        }
      }
    }
    if (min != null) {
      obj.pointVOList[lookingTargetReturnSubscript(min.name, obj.pointVOList)].pointDistance = minObj.y - min.y
    }
    if (max != null) {
      obj.pointVOList[lookingTargetReturnSubscript(max.name, obj.pointVOList)].pointDistance = max.y - minObj.y
    }
    //如果是交通端点-进行跨域连接
    if (minObj.type == 'connect') {
      for (let j = 0; j < newMapList.length; j++) {
        if (newMapList[j].type == 2) {
          for (let k = 0; k < newMapList[j].lightList.length; k++) {
            if (newMapList[j].lightList[k].type == 'traffic') {
              let lx = newMapList[j].x + newMapList[j].lightList[k].x
              let ly = newMapList[j].y + newMapList[j].lightList[k].y
              if (minObj.y > (bigObj.h / 2)) {
                //向下延展
                if (lx >= (bigObj.x + minObj.x - 1) && lx <= (bigObj.x + minObj.x + 1) && ly >= (bigObj.y + bigObj.h) && ly <= (bigObj.y + bigObj.h + minHeight)) {
                  obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = ly - bigObj.y - minObj.y
                }
              } else {
                //向上延展
                if (lx >= (bigObj.x + minObj.x - 1) && lx <= (bigObj.x + minObj.x + 1) && ly >= bigObj.y - minHeight && ly <= bigObj.y) {
                  obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = bigObj.y - ly
                }
              }
            }
          }
        }
      }
    } else if (minObj.type == 'traffic') {
      for (let j = 0; j < newMapList.length; j++) {
        if (newMapList[j].type == 2) {
          for (let k = 0; k < newMapList[j].lightList.length; k++) {
            if (newMapList[j].lightList[k].type == 'connect') {
              let lx = newMapList[j].x + newMapList[j].lightList[k].x
              let ly = newMapList[j].y + newMapList[j].lightList[k].y
              //向右延展
              if (lx >= (bigObj.x + bigObj.w) && lx <= (bigObj.x + bigObj.w + tentaclesLength) && ly >= (bigObj.y + minObj.y - 1) && ly <= (bigObj.y + minObj.y + 1)) {
                obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = lx - bigObj.x - minObj.x
              }
              //向左延展
              if (lx >= (bigObj.x - tentaclesLength) && lx <= bigObj.x && ly >= (bigObj.y + minObj.y - 1) && ly <= (bigObj.y + minObj.y + 1)) {
                obj.pointVOList[lookingTargetReturnSubscript(newMapList[j].lightList[k].name, obj.pointVOList)].pointDistance = (bigObj.x + minObj.x) - lx
              }
            }
          }
        }
      }
    }
  }
  return obj
}

/*
* 寻找目标位置
* 传入:name-匹配名称,list-目标数组
* 返回:返回下标
* 示例: (本方法没有再外部引用,只在本JS内由 calculateAssociatedLength 方法引用)
*   lookingTargetReturnSubscript(min.name,obj.pointVOList)
*/
export function lookingTargetReturnSubscript(name, list) {
  for (let i = 0; i < list.length; i++) {
    if (list[i].pointName == name) {
      return i
    }
  }
}

/*
* 房间内-门的拖拽判定与移动结果
* 传入:mapList-布局数据,boxIndex-当前选中index,minIndex-子类下标,event-鼠标坐标数据,scrollLeft-画布ref.scrollLeft坐标,scrollTop-画布ref.scrollTop坐标
* 返回:一个对象 包含 朝向-toward,坐标X-x,坐标Y-y
* 示例:
*   let obj = doorMoveJudge(this.mapList,this.boxIndex,this.minIndex,event,scrollLeft,scrollTop);
*/
export function doorMoveJudge(mapList, boxIndex,minIndex, event, scrollLeft, scrollTop) {
  let x = event.clientX + scrollLeft - borderLeft
  let y = event.clientY + scrollTop - borderTop
  let yt = y - mapList[boxIndex].y
  let yb = mapList[boxIndex].y + mapList[boxIndex].h - y
  let xl = x - mapList[boxIndex].x
  let xr = mapList[boxIndex].x + mapList[boxIndex].w - x
  let num = Math.min(yt, yb, xl, xr)
  let obj = JSON.parse(JSON.stringify(mapList[boxIndex]))
  //计算靠近方向
  if (num === yt) {
    obj.h = doorHeight;
    obj.w = doorWidth;
    obj.doorList[minIndex].toward = 'top';
    obj.doorList[minIndex].x = (x - mapList[boxIndex].x - (doorWidth / 2)) < 0 ? 0 : (
          (x - mapList[boxIndex].x + (doorWidth / 2)) > mapList[boxIndex].w ? mapList[boxIndex].w - doorWidth :
          x - mapList[boxIndex].x - (doorWidth / 2))
    obj.doorList[minIndex].y = 2
    if(childrenOverlapCheck(obj)){
      return obj.doorList[minIndex]
    }else{
      return false
    }
  } else if (num === yb) {
    obj.h = doorHeight;
    obj.w = doorWidth;
    obj.doorList[minIndex].toward = 'bottom';
    obj.doorList[minIndex].x = (x - mapList[boxIndex].x - (doorWidth / 2)) < 0 ? 0 : (
          (x - mapList[boxIndex].x + (doorWidth / 2)) > mapList[boxIndex].w ? mapList[boxIndex].w - doorWidth :
            x - mapList[boxIndex].x - (doorWidth / 2))
    obj.doorList[minIndex].y = mapList[boxIndex].h - doorHeight - 2
    if(childrenOverlapCheck(obj)){
      return obj.doorList[minIndex]
    }else{
      return false
    }
  } else if (num === xl) {
    obj.h = doorHeight;
    obj.w = doorWidth;
    obj.doorList[minIndex].toward = 'left';
    obj.doorList[minIndex].x = 2
    obj.doorList[minIndex].y = (y - mapList[boxIndex].y - (doorHeight / 2)) < 0 ? 0 : (
        (y - mapList[boxIndex].y + (doorHeight / 2)) > mapList[boxIndex].h ? mapList[boxIndex].h - doorHeight :
          y - mapList[boxIndex].y - (doorHeight / 2))
    if(childrenOverlapCheck(obj)){
      return obj.doorList[minIndex]
    }else{
      return false
    }
  } else if (num === xr) {
    obj.h = maxWidth;
    obj.w = minWidth;
    obj.doorList[minIndex].toward = 'right';
    obj.doorList[minIndex].x = mapList[boxIndex].w - doorWidth - 2
    obj.doorList[minIndex].y = (y - mapList[boxIndex].y - (doorHeight / 2)) < 0 ? 0 : (
        (y - mapList[boxIndex].y + (doorHeight / 2)) > mapList[boxIndex].h ? mapList[boxIndex].h - doorHeight :
          y - mapList[boxIndex].y - (doorHeight / 2))
    if(childrenOverlapCheck(obj)){
      return obj.doorList[minIndex]
    }else{
      return false
    }
  }
}

/*
* 房间内-灯的拖拽判定与移动结果
* 传入:mapList-布局数据,boxIndex-当前选中index,minIndex-子类下标,event-鼠标坐标数据,scrollLeft-画布ref.scrollLeft坐标,scrollTop-画布ref.scrollTop坐标
* 返回:一个对象 包含 坐标X-x,坐标Y-y
* 示例:
*   let obj = lightMoveJudge(this.mapList,this.boxIndex,this.minIndex,event,scrollLeft,scrollTop);
*/
export function lightMoveJudge(mapList, boxIndex, minIndex, event, scrollLeft, scrollTop) {
  let x = event.clientX + scrollLeft - borderLeft
  let y = event.clientY + scrollTop - borderTop
  let obj = JSON.parse(JSON.stringify(mapList[boxIndex]))
  if (mapList[boxIndex].w > mapList[boxIndex].h) {
    obj.lightList[minIndex].x = x - mapList[boxIndex].x - (maxLightNum / 2) < 10 ? 10 : (
            x - mapList[boxIndex].x - (maxLightNum / 2) + maxLightNum > mapList[boxIndex].w - 10 ? mapList[boxIndex].w - maxLightNum - 10 :
            x - mapList[boxIndex].x - (maxLightNum / 2))
    obj.lightList[minIndex].y = (mapList[boxIndex].h / 2) - (maxLightNum / 2)
    if(childrenOverlapCheck(obj)){
      return obj.lightList[minIndex]
    }else{
      return false
    }
  } else if (mapList[boxIndex].h > mapList[boxIndex].w) {
    obj.lightList[minIndex].x = (mapList[boxIndex].w / 2) - (maxLightNum / 2)
    obj.lightList[minIndex].y = y - mapList[boxIndex].y - (maxLightNum / 2) < 10 ? 10 : (
            y - mapList[boxIndex].y - (maxLightNum / 2) + maxLightNum > mapList[boxIndex].h - 10 ? mapList[boxIndex].h - maxLightNum - 10 :
            y - mapList[boxIndex].y - (maxLightNum / 2))
    if(childrenOverlapCheck(obj)){
      return obj.lightList[minIndex]
    }else{
      return false
    }
  }
}

/*
* 房间移动
* 传入:mapList-布局数据,boxIndex-当前选中index,clientX-当前X坐标,clientY-当前Y坐标,event-鼠标数据
* 返回:一个对象 包含 坐标X-x,坐标Y-y
* 示例:
*   let obj = roomMove(this.mapList,this.boxIndex,this.clientX,this.clientY,event)
*   if(obj.x){
*     this.$set(this.mapList[this.boxIndex], 'x', obj.x)
*   }
*   if (obj.y){
*     this.$set(this.mapList[this.boxIndex], 'y', obj.y)
*   }
*/
export function roomMove(mapList, boxIndex, clientX, clientY, event) {
  let obj = {
    x: null,
    y: null
  }
  if (clientX > event.clientX) {
    if (moveBoundaryCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left') &&
      moveOverlapCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left', mapList, boxIndex)) {
      obj.x = mapList[boxIndex].x - (clientX - event.clientX)
    }
  } else if (event.clientX > clientX) {
    if (moveBoundaryCheck(mapList[boxIndex].x + (event.clientX - clientX) + mapList[boxIndex].w, 'right') &&
      moveOverlapCheck(mapList[boxIndex].x + (event.clientX - clientX), 'right', mapList, boxIndex)) {
      obj.x = mapList[boxIndex].x + (event.clientX - clientX)
    }
  }
  if (clientY > event.clientY) {
    if (moveBoundaryCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top') &&
      moveOverlapCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top', mapList, boxIndex)) {
      obj.y = mapList[boxIndex].y - (clientY - event.clientY)
    }
  } else if (event.clientY > clientY) {
    if (moveBoundaryCheck(mapList[boxIndex].y + (event.clientY - clientY) + mapList[boxIndex].h, 'bottom') &&
      moveOverlapCheck(mapList[boxIndex].y + (event.clientY - clientY), 'bottom', mapList, boxIndex)) {
      obj.y = mapList[boxIndex].y + (event.clientY - clientY)
    }
  }
  return obj
}

/*
* 添加房间
* 传入:e-鼠标数据,mapList-布局数据,borderLeft-左侧边界,borderLeft-上方边界-最小宽度,grab-抓取类型,scrollLeft-画布ref.scrollLeft坐标,scrollTop-画布ref.scrollTop坐标
* 返回:完整的房间对象数据
* 示例:
*   let obj = roomAdd(e,this.mapList,this.grab,this.$refs.maxBigBox.scrollLeft,this.$refs.maxBigBox.scrollTop,);
    if(obj){
      this.mapList.push(obj);
    }
*/
export function roomAdd(e, mapList, grab, scrollLeft, scrollTop) {
  let x = e.clientX + scrollLeft
  let y = e.clientY + scrollTop
  if (x > borderLeft && x < (borderLeft + layerWidth) &&
    y > borderTop && y < (borderTop + layerHeight)) {
    let newX = x - borderLeft - (minWidth / 2)
    let newY = y - borderTop - (minHeight / 2)
    if (moveBoundaryCheck(newX, 'left') && moveBoundaryCheck(newX + minWidth, 'right') && moveBoundaryCheck(newY, 'top') && moveBoundaryCheck(newY + minHeight, 'bottom') &&
      placementOverlapCheck(newX, newY, 'left', mapList) && placementOverlapCheck(newX, newY, 'right', mapList) && placementOverlapCheck(newX, newY, 'top', mapList) && placementOverlapCheck(newX, newY, 'bottom', mapList)) {
      let obj = {
        x: newX,
        y: newY,
        w: minWidth,
        h: minHeight,
        type: grab//1房间2楼道3疏散通道
      }
      if (grab == 1 || grab == 3) {
        obj.doorList = []
      } else if (grab == 2) {
        obj.lightList = []
      }
      return obj
    } else {
      return false
    }
  } else {
    return false
  }
}

/*
* 添加灯
* 传入:event-鼠标坐标数据,scrollLeft-画布ref.scrollLeft坐标,scrollTop-画布ref.scrollTop坐标,mapList-画布数据,grab-抓取类型
* 返回:完整的房间对象数据
* 示例:
*   let obj = lightAdd(event,this.$refs.maxBigBox.scrollLeft,this.$refs.maxBigBox.scrollTop,this.mapList,this.grab);
    if (obj){
      this.mapList[obj.index].lightList.push({
          type:obj.type,
          state:obj.state,
          w:obj.w,
          h:obj.h,
          x:obj.x,
          y:obj.y,
      });
    }
*/
export function lightAdd(event, scrollLeft, scrollTop, mapList, grab) {
  let x = event.clientX + scrollLeft - borderLeft
  let y = event.clientY + scrollTop - borderTop
  for (let i = 0; i < mapList.length; i++) {
    if (x > mapList[i].x && x < (mapList[i].x + mapList[i].w) &&
      y > mapList[i].y && y < (mapList[i].y + mapList[i].h)) {
      if (mapList[i].type == '1') {
        //房间
        if (grab != '4' && grab != '5') {
          Message({
            message: '房间只可添加门',
            type: 'error',
            offset: 100
          })
          return
        }
      } else if (mapList[i].type == '2') {
        //走廊
        if (grab != '6') {
          Message({
            message: '走廊只可添加疏散灯',
            type: 'error',
            offset: 100
          })
          return
        }
      } else if (mapList[i].type == '3') {
        //疏散通道
        if (grab != '4' && grab != '5') {
          Message({
            message: '楼道只可添加门',
            type: 'error',
            offset: 100
          })
          return
        }
        return
      }
      if (mapList[i].w > mapList[i].h) {
        // 横向
        let obj = { index: i, type: 'light', state: '', w: maxLightNum, h: maxLightNum, x: x - mapList[i].x - (maxLightNum / 2), y: (mapList[i].h / 2) - (maxLightNum / 2) }
        let newObj = {w: maxLightNum, h: maxLightNum, x: x - mapList[i].x - (maxLightNum / 2), y: (mapList[i].h / 2) - (maxLightNum / 2)};
        if(mapList[i].lightList[0]){
          let newList = JSON.parse(JSON.stringify(mapList[i]))
          newList.lightList.push(newObj);
          if(childrenOverlapCheck(newList)){
            return obj;
          }else{
            return false
          }
        }else{
          return obj;
        }
      } else {
        let obj ={ index: i, type: 'light', state: '', w: maxLightNum, h: maxLightNum, x: (mapList[i].w / 2) - (maxLightNum / 2), y: y - mapList[i].y - (maxLightNum / 2) }
        let newObj = {w: maxLightNum, h: maxLightNum, x: (mapList[i].w / 2) - (maxLightNum / 2), y: y - mapList[i].y - (maxLightNum / 2)}
        if(mapList[i].lightList[0]){
          let newList = JSON.parse(JSON.stringify(mapList[i]))
          newList.lightList.push(newObj);
          if(childrenOverlapCheck(newList)){
            return obj;
          }else{
            return false
          }
        }else{
          return obj;
        }
      }
    }
  }
}

/*
* 添加门
* 传入:e-鼠标数据,mapList-布局数据,scrollLeft-画布ref.scrollLeft坐标,scrollTop-画布ref.scrollTop坐标,grab-抓取类型,
* 返回:完整的房间对象数据
* 示例:
*   let obj = addDoor(e,this.mapList,this.$refs.maxBigBox.scrollLeft,this.$refs.maxBigBox.scrollTop,this.grab);
    if(obj){
      this.mapList.push(obj);
    }
*/
export function addDoor(event, mapList, scrollLeft, scrollTop, grab) {
  let x = event.clientX + scrollLeft - borderLeft
  let y = event.clientY + scrollTop - borderTop
  for (let i = 0; i < mapList.length; i++) {
    if (x > mapList[i].x && x < (mapList[i].x + mapList[i].w) &&
      y > mapList[i].y && y < (mapList[i].y + mapList[i].h)) {
      if (mapList[i].type == '1' || mapList[i].type == '3') {
        //房间
        if (grab != '4' && grab != '5') {
          this.msgError('房间与楼道只可添加门窗')
          return
        }
      } else if (mapList[i].type == '2') {
        //走廊
        if (grab != '6') {
          this.msgError('走廊只可添加疏散灯')
          return
        }
      }
      let yt = y - mapList[i].y
      let yb = mapList[i].y + mapList[i].h - y
      let xl = x - mapList[i].x
      let xr = mapList[i].x + mapList[i].w - x
      let num = Math.min(yt, yb, xl, xr)
      //计算靠近方向
      if (num === yt) {
        return {
          index: i,
          type: 'door',
          toward: 'top',
          w: doorWidth,
          h: doorHeight,
          x: x - mapList[i].x - (doorWidth / 2),
          y: 2
        }
      } else if (num === yb) {
        return {
          index: i,
          type: 'door',
          toward: 'bottom',
          w: doorWidth,
          h: doorHeight,
          x: x - mapList[i].x - (doorWidth / 2),
          y: mapList[i].h - doorHeight - 2
        }
      } else if (num === xl) {
        return {
          index: i,
          type: 'door',
          toward: 'left',
          w: doorWidth,
          h: doorHeight,
          x: 2,
          y: y - mapList[i].y - (doorHeight / 2)
        }
      } else if (num === xr) {
        return {
          index: i,
          type: 'door',
          toward: 'right',
          w: doorWidth,
          h: doorHeight,
          x: mapList[i].w - doorWidth - 2,
          y: y - mapList[i].y - (doorHeight / 2)
        }
      }
    }
  }
}

/*
* 四角缩放
* 传入:mapList-布局数据,boxIndex-当前选中index,event-鼠标数据,clientX-当前X坐标,clientY-当前Y坐标,type-拖拽方向状态
* 返回:true 或者 false
* 示例:let obj = fourCornersZoom(this.mapList,this.boxIndex,event,this.clientX,this.clientY,this.moveType);
       if(obj){
         this.$set(this.mapList[this.boxIndex],'x',obj.x?obj.x:this.mapList[this.boxIndex].x);
       }
*/
export function fourCornersZoom(mapList, boxIndex, event, clientX, clientY, type) {
  let obj = {}
  if (type == '4') {
    if (clientX > event.clientX) {
      if (moveBoundaryCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left') &&
        moveOverlapCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].w + (clientX - event.clientX), 'w', '+')) {
        obj.x = mapList[boxIndex].x - (clientX - event.clientX)
        obj.w = mapList[boxIndex].w + (clientX - event.clientX)
      }
    } else if (event.clientX > clientX) {
      if (sizeCheck(mapList[boxIndex].w - (event.clientX - clientX), 'w', '-')) {
        obj.x = mapList[boxIndex].x + (event.clientX - clientX)
        obj.w = mapList[boxIndex].w - (event.clientX - clientX)
      }
    }
    if (clientY > event.clientY) {
      if (moveBoundaryCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top') &&
        moveOverlapCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].h + (clientY - event.clientY), 'h', '+')) {
        obj.y = mapList[boxIndex].y - (clientY - event.clientY)
        obj.h = mapList[boxIndex].h + (clientY - event.clientY)
      }
    } else if (event.clientY > clientY) {
      if (sizeCheck(mapList[boxIndex].h - (event.clientY - clientY), 'h', '-')) {
        obj.y = mapList[boxIndex].y + (event.clientY - clientY)
        obj.h = mapList[boxIndex].h - (event.clientY - clientY)
      }
    }
  } else if (type == '3') {
    if (clientX > event.clientX) {
      if (moveBoundaryCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left') &&
        moveOverlapCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].w + (clientX - event.clientX), 'w', '+')) {
        obj.x = mapList[boxIndex].x - (clientX - event.clientX)
        obj.w = mapList[boxIndex].w + (clientX - event.clientX)
      }
    } else if (event.clientX > clientX) {
      if (sizeCheck(mapList[boxIndex].w - (event.clientX - clientX), 'w', '-')) {
        obj.x = mapList[boxIndex].x + (event.clientX - clientX)
        obj.w = mapList[boxIndex].w - (event.clientX - clientX)
      }
    }
    if (clientY > event.clientY) {
      if (sizeCheck(mapList[boxIndex].h - (clientY - event.clientY), 'h', '-')) {
        obj.h = mapList[boxIndex].h - (clientY - event.clientY)
      }
    } else if (event.clientY > clientY) {
      if (moveBoundaryCheck(mapList[boxIndex].y + (event.clientY - clientY) + mapList[boxIndex].h, 'bottom') &&
        moveOverlapCheck(mapList[boxIndex].y + (event.clientY - clientY), 'bottom', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].h + (event.clientY - clientY), 'h', '+')) {
        obj.h = mapList[boxIndex].h + (event.clientY - clientY)
      }
    }
  } else if (type == '2') {
    if (clientX > event.clientX) {
      if (sizeCheck(mapList[boxIndex].w - (clientX - event.clientX), 'w', '-')) {
        obj.w = mapList[boxIndex].w - (clientX - event.clientX)
      }
    } else if (event.clientX > clientX) {
      if (moveBoundaryCheck(mapList[boxIndex].x + (event.clientX - clientX) + mapList[boxIndex].w, 'right') &&
        moveOverlapCheck(mapList[boxIndex].x + (event.clientX - clientX), 'right', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].w + (event.clientX - clientX), 'w', '+')) {
        obj.w = mapList[boxIndex].w + (event.clientX - clientX)
      }
    }
    if (clientY > event.clientY) {
      if (sizeCheck(mapList[boxIndex].h - (clientY - event.clientY), 'h', '-')) {
        obj.h = mapList[boxIndex].h - (clientY - event.clientY)

      }
    } else if (event.clientY > clientY) {
      if (moveBoundaryCheck(mapList[boxIndex].y + (event.clientY - clientY) + mapList[boxIndex].h, 'bottom') &&
        moveOverlapCheck(mapList[boxIndex].y + (event.clientY - clientY), 'bottom', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].h + (event.clientY - clientY), 'h', '+')) {
        obj.h = mapList[boxIndex].h + (event.clientY - clientY)
      }
    }
  } else if (type == '1') {
    if (clientX > event.clientX) {
      if (sizeCheck(mapList[boxIndex].w - (clientX - event.clientX), 'w', '-')) {
        obj.w = mapList[boxIndex].w - (clientX - event.clientX)
      }
    } else if (event.clientX > clientX) {
      if (moveBoundaryCheck(mapList[boxIndex].x + (event.clientX - clientX) + mapList[boxIndex].w, 'right') &&
        moveOverlapCheck(mapList[boxIndex].x + (event.clientX - clientX), 'right', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].w + (event.clientX - clientX), 'w', '+')) {
        obj.w = mapList[boxIndex].w + (event.clientX - clientX)
      }
    }
    if (clientY > event.clientY) {
      if (moveBoundaryCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top') &&
        moveOverlapCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].h + (clientY - event.clientY), 'h', '+')) {
        obj.y = mapList[boxIndex].y - (clientY - event.clientY)
        obj.h = mapList[boxIndex].h + (clientY - event.clientY)
      }
    } else if (event.clientY > clientY) {
      if (sizeCheck(mapList[boxIndex].h - (event.clientY - clientY), 'h', '-')) {
        obj.y = mapList[boxIndex].y + (event.clientY - clientY)
        obj.h = mapList[boxIndex].h - (event.clientY - clientY)
      }
    }
  }
  if (mapList[boxIndex].type == '1' || mapList[boxIndex].type == '3') {
    let newList = JSON.parse(JSON.stringify(mapList[boxIndex]))
    newList.w = obj.w ? obj.w : newList.w
    newList.h = obj.h ? obj.h : newList.h
    obj.doorList = doorDragJudge(newList)
    if (!obj.doorList) {
      return false
    }
  }
  if (mapList[boxIndex].type == '2') {
    let newList = JSON.parse(JSON.stringify(mapList[boxIndex]))
    newList.w = obj.w ? obj.w : newList.w
    newList.h = obj.h ? obj.h : newList.h
    obj.lightList = lightDragJudge(newList)
    if (!obj.lightList) {
      return false
    }
  }
  return obj
}

/*
* 四边缩放
* 传入:mapList-布局数据,boxIndex-当前选中index,event-鼠标数据,clientX-当前X坐标,clientY-当前Y坐标,type-拖拽方向状态
* 返回:true 或者 false
* 示例:let obj = fourCornersZoom(this.mapList,this.boxIndex,event,this.clientX,this.clientY,this.moveType);
       if(obj){
         this.$set(this.mapList[this.boxIndex],'x',obj.x?obj.x:this.mapList[this.boxIndex].x);
       }
*/
export function fourEdgeZoom(mapList, boxIndex, event, clientX, clientY, type) {
  let obj = {}
  if (type == '4') {
    if (clientX > event.clientX) {
      if (moveBoundaryCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left') &&
        moveOverlapCheck(mapList[boxIndex].x - (clientX - event.clientX), 'left', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].w + (clientX - event.clientX), 'w', '+')) {
        obj.x = mapList[boxIndex].x - (clientX - event.clientX)
        obj.w = mapList[boxIndex].w + (clientX - event.clientX)
      }
    } else if (event.clientX > clientX) {
      if (sizeCheck(mapList[boxIndex].w - (event.clientX - clientX), 'w', '-')) {
        obj.x = mapList[boxIndex].x + (event.clientX - clientX)
        obj.w = mapList[boxIndex].w - (event.clientX - clientX)
      }
    }
  } else if (type == '3') {
    if (clientY > event.clientY) {
      if (sizeCheck(mapList[boxIndex].h - (clientY - event.clientY), 'h', '-')) {
        obj.h = mapList[boxIndex].h - (clientY - event.clientY)
      }
    } else if (event.clientY > clientY) {
      if (moveBoundaryCheck(mapList[boxIndex].y + (event.clientY - clientY) + mapList[boxIndex].h, 'bottom') &&
        moveOverlapCheck(mapList[boxIndex].y + (event.clientY - clientY), 'bottom', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].h + (event.clientY - clientY), 'h', '+')) {
        obj.h = mapList[boxIndex].h + (event.clientY - clientY)
      }
    }
  } else if (type == '2') {
    if (clientX > event.clientX) {
      if (sizeCheck(mapList[boxIndex].w - (clientX - event.clientX), 'w', '-')) {
        obj.w = mapList[boxIndex].w - (clientX - event.clientX)
      }
    } else if (event.clientX > clientX) {
      if (moveBoundaryCheck(mapList[boxIndex].x + (event.clientX - clientX) + mapList[boxIndex].w, 'right') &&
        moveOverlapCheck(mapList[boxIndex].x + (event.clientX - clientX), 'right', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].w + (event.clientX - clientX), 'w', '+')) {
        obj.w = mapList[boxIndex].w + (event.clientX - clientX)
      }
    }
  } else if (type == '1') {
    if (clientY > event.clientY) {
      if (moveBoundaryCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top') &&
        moveOverlapCheck(mapList[boxIndex].y - (clientY - event.clientY), 'top', mapList, boxIndex) &&
        sizeCheck(mapList[boxIndex].h + (clientY - event.clientY), 'h', '+')) {
        obj.y = mapList[boxIndex].y - (clientY - event.clientY)
        obj.h = mapList[boxIndex].h + (clientY - event.clientY)
      }
    } else if (event.clientY > clientY) {
      if (sizeCheck(mapList[boxIndex].h - (event.clientY - clientY), 'h', '-')) {
        obj.y = mapList[boxIndex].y + (event.clientY - clientY)
        obj.h = mapList[boxIndex].h - (event.clientY - clientY)
      }
    }
  }
  if (mapList[boxIndex].type == '1' || mapList[boxIndex].type == '3') {
    let newList = JSON.parse(JSON.stringify(mapList[boxIndex]))
    newList.w = obj.w ? obj.w : newList.w
    newList.h = obj.h ? obj.h : newList.h
    obj.doorList = doorDragJudge(newList)
    if (!obj.doorList) {
      return false
    }
  }
  if (mapList[boxIndex].type == '2') {
    let newList = JSON.parse(JSON.stringify(mapList[boxIndex]))
    newList.w = obj.w ? obj.w : newList.w
    newList.h = obj.h ? obj.h : newList.h
    obj.lightList = lightDragJudge(newList)
    if (!obj.lightList) {
      return false
    }
  }
  return obj
}

/*
* 门列表坐标修正
* 传入:obj-当前拖拽对象
* 返回:修正后的门列表数组
* 示例:obj.doorList = doorDragJudge(newList);
*/
export function doorDragJudge(obj) {
  for (let i = 0; i < obj.doorList.length; i++) {
    if (obj.doorList[i].toward == 'left') {
      obj.doorList[i].x = 0
      obj.doorList[i].y = (obj.doorList[i].y + doorHeight) >= obj.h ? obj.h - doorHeight : obj.doorList[i].y
    } else if (obj.doorList[i].toward == 'right') {
      obj.doorList[i].x = obj.w - doorWidth
      obj.doorList[i].y = (obj.doorList[i].y + doorHeight) >= obj.h ? obj.h - doorHeight : obj.doorList[i].y
    } else if (obj.doorList[i].toward == 'top') {
      obj.doorList[i].x = (obj.doorList[i].x + doorWidth) >= obj.w ? obj.w - doorWidth : obj.doorList[i].x
      obj.doorList[i].y = 0
    } else if (obj.doorList[i].toward == 'bottom') {
      obj.doorList[i].x = (obj.doorList[i].x + doorWidth) >= obj.w ? parseInt(obj.w - doorWidth) : parseInt(obj.doorList[i].x)
      obj.doorList[i].y = obj.h - doorHeight
    }
  }
  if (childrenOverlapCheck(obj)) {
    return obj.doorList[0] ? obj.doorList : []
  } else {
    return false
  }
}

/*
* 灯列表坐标修正
* 传入:obj-当前拖拽对象
* 返回:修正后的灯列表数组
* 示例:obj.lightList = lightDragJudge(newList);
*/
export function lightDragJudge(obj) {
  if (obj.w > obj.h) {
    zoomType = 1
    if (obj.lightList[1]) {
      if (obj.lightList[0].x == obj.lightList[1].x) {
        for (let o = 0; o < obj.lightList.length; o++) {
          obj.lightList[o].x = JSON.parse(JSON.stringify(obj.lightList[o].y))
          obj.lightList[o].y = obj.h / 2 - maxLightNum / 2
        }
      } else {
        for (let i = 0; i < obj.lightList.length; i++) {
          obj.lightList[i].y = (obj.h / 2) - (maxLightNum / 2)
          obj.lightList[i].x =
            obj.lightList[i].x <= 10 ? 10 : (
              (obj.lightList[i].x + maxLightNum) >= (obj.w - 10) ? (obj.w - maxLightNum - 10) :
                obj.lightList[i].x)
        }
      }
    } else {
      for (let i = 0; i < obj.lightList.length; i++) {
        obj.lightList[i].y = (obj.h / 2) - (maxLightNum / 2)
        obj.lightList[i].x =
          obj.lightList[i].x <= 10 ? 10 : (
            (obj.lightList[i].x + maxLightNum) >= (obj.w - 10) ? (obj.w - maxLightNum - 10) :
              obj.lightList[i].x)
      }
    }
  } else if (obj.h > obj.w) {
    zoomType = 2
    if (obj.lightList[1]) {
      if (obj.lightList[0].y == obj.lightList[1].y) {
        for (let o = 0; o < obj.lightList.length; o++) {
          obj.lightList[o].y = JSON.parse(JSON.stringify(obj.lightList[o].x))
          obj.lightList[o].x = obj.w / 2 - maxLightNum / 2
        }
      } else {
        for (let i = 0; i < obj.lightList.length; i++) {
          obj.lightList[i].x = (obj.w / 2) - (maxLightNum / 2)
          obj.lightList[i].y =
            obj.lightList[i].y <= 10 ? 10 : (
              (obj.lightList[i].y + maxLightNum) >= (obj.h - 10) ? (obj.h - maxLightNum - 10) :
                obj.lightList[i].y)
        }
      }
    } else {
      for (let i = 0; i < obj.lightList.length; i++) {
        obj.lightList[i].x = (obj.w / 2) - (maxLightNum / 2)
        obj.lightList[i].y =
          obj.lightList[i].y <= 10 ? 10 : (
            (obj.lightList[i].y + maxLightNum) >= (obj.h - 10) ? (obj.h - maxLightNum - 10) :
              obj.lightList[i].y)
      }
    }
  } else {
    if (zoomType == 1) {
      obj.w = obj.w - 1
      obj.h = obj.h + 1
    } else if (zoomType == 2) {
      obj.w = obj.w + 1
      obj.h = obj.h - 1
    }
  }

  let num1 = 0
  let num2 = 0
  for (let o = 0; o < obj.lightList.length; o++) {
    num1 = num1 < obj.lightList[o].x ? obj.lightList[o].x : num1
    num2 = num2 < obj.lightList[o].y ? obj.lightList[o].y : num2
  }
  if (zoomType == 1 && ((num2 + maxLightNum) > obj.h - 10)) {
    return false
  } else if (zoomType == 2 && ((num1 + maxLightNum) > obj.w - 10)) {
    return false
  }
  if (childrenOverlapCheck(obj)) {
    return obj.lightList[0] ? obj.lightList : []
  } else {
    return false
  }
}

/************************************ 检查相关 ************************************/

/*
* 检查门朝向附近是否有楼道
* 传入:mapList-布局数据
* 返回:true 或者 false并提示错误
* 示例:
*   if(checkDoorTo(this.mapList)){}
*/
export function checkDoorTo(mapList) {
  for (let i = 0; i < mapList.length; i++) {
    if (mapList[i].type == 1 || mapList[i].type == 3) {
      let x = 0
      let y = 0
      if (mapList[i].doorList[0].toward == 'top') {
        x = mapList[i].x + mapList[i].doorList[0].x + (doorWidth / 2)
        y = mapList[i].y - (minHeight / 2)
      } else if (mapList[i].doorList[0].toward == 'bottom') {
        x = mapList[i].x + mapList[i].doorList[0].x + (doorWidth / 2)
        y = mapList[i].y + mapList[i].h + (minHeight / 2)
      } else if (mapList[i].doorList[0].toward == 'left') {
        x = mapList[i].x - (minHeight / 2)
        y = mapList[i].y + mapList[i].doorList[0].y + (doorHeight / 2)
      } else if (mapList[i].doorList[0].toward == 'right') {
        x = mapList[i].x + mapList[i].w + (minHeight / 2)
        y = mapList[i].y + mapList[i].doorList[0].y + (doorHeight / 2)
      }
      let num = 0
      for (let j = 0; j < mapList.length; j++) {
        if (mapList[j].type == 2) {
          if (x > mapList[j].x && x < (mapList[j].x + mapList[j].w) &&
            y > mapList[j].y && y < (mapList[j].y + mapList[j].h)) {
            num++
          }
        }
      }
      if (num == 0) {
        Message({
          message: '有门的位置朝向不正确',
          type: 'error',
          offset: 100
        })
        return false
      }
    }
  }
  return true
}

/*
* 检查通道内灯是否都已绑定朝向
* 传入:mapList-布局数据
* 返回:true 或者 false并提示错误
* 示例:
*   if(checkLightDirection(this.mapList){}
*/
export function checkLightDirection(mapList) {
  for (let i = 0; i < mapList.length; i++) {
    if (mapList[i].type == 2) {
      for (let j = 0; j < mapList[i].lightList.length; j++) {
        if (!mapList[i].lightList[j].state) {
          Message({
            message: '请检查所有的灯是否都已选择朝向',
            type: 'error',
            offset: 100
          })
          return false
        }
      }
    }
  }
  return true
}

/*
* 检查楼层是否有房间/逃生通道/楼道
* mapList-布局数据
* 返回:true 或者 false并提示错误
* 示例:
*   if(checkRoom(this.mapList){}
*/
export function checkRoom(mapList) {
  //实验室数量
  let num1 = 0
  //逃生点数量
  let num2 = 0
  //楼道数量
  let num3 = 0
  //实验室门数量
  let num4 = 0
  for (let i = 0; i < mapList.length; i++) {
    if (mapList[i].type == 1 || mapList[i].type == 3) {
      if (mapList[i].type == 1) {
        num1++
      } else if (mapList[i].type == 3) {
        num2++
      }
      if (mapList[i].doorList[0]) {
        num4++
      }
    } else if (mapList[i].type == 2) {
      num3++
    }
  }
  if (num1 === 0) {
    Message({
      message: '请添加房间',
      type: 'error',
      offset: 100
    })
    return false
  }
  if (num2 === 0) {
    Message({
      message: '请添加应急出口',
      type: 'error',
      offset: 100
    })
    return false
  }
  if (num3 == 0) {
    Message({
      message: '请添加楼道',
      type: 'error',
      offset: 100
    })
    return false
  }
  if (num4 < (num1 + num2)) {
    Message({
      message: '请在每个房间与应急出口内添加门',
      type: 'error',
      offset: 100
    })
    return false
  }
  return true
}

/*
* 移动边界检查
* 传入:px-目标坐标,type-方向
* 返回:true 或者 false
* 示例:(本JS内部调用)
*   if(moveBoundaryCheck(mapList[boxIndex].y + (event.clientY - clientY)+mapList[boxIndex].h,'bottom'){}
*/
export function moveBoundaryCheck(px, type) {
  if (type == 'left') {
    return px > 0
  } else if (type == 'right') {
    return px < layerWidth
  } else if (type == 'top') {
    return px > 0
  } else if (type == 'bottom') {
    return px < layerHeight
  }
}

/*
* 移动重叠检查
* 传入:px-目标坐标,type-方向,mapList-布局数据,boxIndex-当前选中index
* 返回:true 或者 false
* 示例:(本JS内部调用)
*   if(moveOverlapCheck(mapList[boxIndex].x - (clientX - event.clientX),'left',mapList,boxIndex)){}
*/
export function moveOverlapCheck(px, type, mapList, boxIndex) {
  let num = 0
  let x1 = null
  let y1 = null
  let x2 = null
  let y2 = null
  if (type == 'left') {
    x1 = px
    y1 = mapList[boxIndex].y
    x2 = px
    y2 = mapList[boxIndex].y + mapList[boxIndex].h
  } else if (type == 'right') {
    x1 = px + mapList[boxIndex].w
    y1 = mapList[boxIndex].y
    x2 = px + mapList[boxIndex].w
    y2 = mapList[boxIndex].y + mapList[boxIndex].h
  } else if (type == 'top') {
    x1 = mapList[boxIndex].x
    y1 = px
    x2 = mapList[boxIndex].x + mapList[boxIndex].w
    y2 = px
  } else if (type == 'bottom') {
    x1 = mapList[boxIndex].x
    y1 = px + mapList[boxIndex].h
    x2 = mapList[boxIndex].x + mapList[boxIndex].w
    y2 = px + mapList[boxIndex].h
  }
  for (let i = 0; i < mapList.length; i++) {
    if (boxIndex != i) {
      if (
        (x1 > mapList[i].x && x1 < (mapList[i].x + mapList[i].w) && y1 > mapList[i].y && y1 < (mapList[i].y + mapList[i].h)) ||
        (x2 > mapList[i].x && x2 < (mapList[i].x + mapList[i].w) && y2 > mapList[i].y && y2 < (mapList[i].y + mapList[i].h))
      ) {
        num++
      }
      if (type == 'top' && x1 <= mapList[i].x && x2 >= (mapList[i].x + mapList[i].w) && y1 < (mapList[i].y + mapList[i].h) && y1 > mapList[i].y) {
        num++
      }
      if (type == 'bottom' && x1 <= mapList[i].x && x2 >= (mapList[i].x + mapList[i].w) && y1 > mapList[i].y && y1 < (mapList[i].y + mapList[i].h)) {
        num++
      }
      if (type == 'left' && y1 <= mapList[i].y && y2 >= (mapList[i].y + mapList[i].h) && x1 < (mapList[i].x + mapList[i].w) && x1 > mapList[i].x) {
        num++
      }
      if (type == 'right' && y1 <= mapList[i].y && y2 >= (mapList[i].y + mapList[i].h) && x1 > mapList[i].x && x1 < (mapList[i].x + mapList[i].w)) {
        num++
      }
    }
  }
  return num == 0
}

/*
* 尺寸检查
* 传入:px-目标尺寸,type-宽高类型,state-加减类型
* 返回:true 或者 false
* 示例:(本JS内部调用)
*   if(sizeCheck(px,type,state)){}
*/
export function sizeCheck(px, type, state) {
  let num = 0
  if (type == 'w' && state == '+') {
    if (px > maxWidth) {
      num++
    }
  } else if (type == 'w' && state == '-') {
    if (px < minWidth) {
      num++
    }
  } else if (type == 'h' && state == '+') {
    if (px > maxHeight) {
      num++
    }
  } else if (type == 'h' && state == '-') {
    if (px < minHeight) {
      num++
    }
  }
  return num == 0
}

/*
* 放置重叠检查
* 传入:x-目标X坐标,y-目标Y坐标,type-方向,mapList-画布数据
* 返回:true 或者 false
* 示例:(本JS内部调用)
*   if(placementOverlapCheck(x,y,type,mapList)){}
*/
export function placementOverlapCheck(x, y, type, mapList) {
  let num = 0
  let x1 = null
  let y1 = null
  let x2 = null
  let y2 = null
  let xOne = x
  let xTwo = x + minWidth
  let yOne = y
  let yTwo = y + minHeight
  if (type == 'left') {
    x1 = x
    y1 = y
    x2 = x
    y2 = y + minHeight
  } else if (type == 'right') {
    x1 = x + minWidth
    y1 = y
    x2 = x + minWidth
    y2 = y + minHeight
  } else if (type == 'top') {
    x1 = x
    y1 = y
    x2 = x + minWidth
    y2 = y
  } else if (type == 'bottom') {
    x1 = x
    y1 = y + minHeight
    x2 = x + minWidth
    y2 = y + minHeight
  }
  for (let i = 0; i < mapList.length; i++) {
    // if(this.boxIndex != i){
    if (
      (xOne > mapList[i].x && xOne < (mapList[i].x + mapList[i].w) && yOne > mapList[i].y && yOne < (mapList[i].y + mapList[i].h)) ||
      (xOne > mapList[i].x && xOne < (mapList[i].x + mapList[i].w) && yTwo > mapList[i].y && yTwo < (mapList[i].y + mapList[i].h)) ||
      (xTwo > mapList[i].x && xTwo < (mapList[i].x + mapList[i].w) && yOne > mapList[i].y && yOne < (mapList[i].y + mapList[i].h)) ||
      (xTwo > mapList[i].x && xTwo < (mapList[i].x + mapList[i].w) && yTwo > mapList[i].y && yTwo < (mapList[i].y + mapList[i].h))
    ) {
      num++
    }
    if (type == 'top' && x1 <= mapList[i].x && x2 >= (mapList[i].x + mapList[i].w) && y1 < (mapList[i].y + mapList[i].h) && y1 > mapList[i].y) {
      num++
    }
    if (type == 'bottom' && x1 <= mapList[i].x && x2 >= (mapList[i].x + mapList[i].w) && y1 > mapList[i].y && y1 < (mapList[i].y + mapList[i].h)) {
      num++
    }
    if (type == 'left' && y1 <= mapList[i].y && y2 >= (mapList[i].y + mapList[i].h) && x1 < (mapList[i].x + mapList[i].w) && x1 > mapList[i].x) {
      num++
    }
    if (type == 'right' && y1 <= mapList[i].y && y2 >= (mapList[i].y + mapList[i].h) && x1 > mapList[i].x && x1 < (mapList[i].x + mapList[i].w)) {
      num++
    }
    // }
  }
  return num == 0
}

/*
* 门/灯 重叠检查
* 传入:bigObj-父数据,minObj-子数据,index-当前下标
* 返回:true 或者 false
* 示例:(本JS内部调用)
*   if(placementOverlapCheck(x,y,type,mapList)){}
*/
export function childrenOverlapCheck(bigObj) {
  if (bigObj.type == 1 || bigObj.type == 3) {
    let num = 0
    for (let i = 0; i < bigObj.doorList.length; i++) {
      let x1 = bigObj.doorList[i].x
      let y1 = bigObj.doorList[i].y
      let x2 = bigObj.doorList[i].x + bigObj.doorList[i].w
      let y2 = bigObj.doorList[i].y + bigObj.doorList[i].h
      for (let o = 0; o < bigObj.doorList.length; o++) {
        if (i != o) {
          if (x1 >= bigObj.doorList[o].x && x1 <= (bigObj.doorList[o].x + bigObj.doorList[o].w) &&
            y1 >= bigObj.doorList[o].y && y1 <= (bigObj.doorList[o].y + bigObj.doorList[o].h)) {
            num++
          }
          if (x1 >= bigObj.doorList[o].x && x1 <= (bigObj.doorList[o].x + bigObj.doorList[o].w) &&
            y2 >= bigObj.doorList[o].y && y2 <= (bigObj.doorList[o].y + bigObj.doorList[o].h)) {
            num++
          }
          if (x2 >= bigObj.doorList[o].x && x2 <= (bigObj.doorList[o].x + bigObj.doorList[o].w) &&
            y1 >= bigObj.doorList[o].y && y1 <= (bigObj.doorList[o].y + bigObj.doorList[o].h)) {
            num++
          }
          if (x2 >= bigObj.doorList[o].x && x2 <= (bigObj.doorList[o].x + bigObj.doorList[o].w) &&
            y2 >= bigObj.doorList[o].y && y2 <= (bigObj.doorList[o].y + bigObj.doorList[o].h)) {
            num++
          }
        }
      }
    }
    return num == 0
  } else if (bigObj.type == 2) {
    let num = 0
    for (let i = 0; i < bigObj.lightList.length; i++) {
      let x1 = bigObj.lightList[i].x
      let y1 = bigObj.lightList[i].y
      let x2 = bigObj.lightList[i].x + bigObj.lightList[i].w
      let y2 = bigObj.lightList[i].y + bigObj.lightList[i].h
      for (let o = 0; o < bigObj.lightList.length; o++) {
        if (i != o) {
          if (x1 >= bigObj.lightList[o].x && x1 <= (bigObj.lightList[o].x + bigObj.lightList[o].w) &&
            y1 >= bigObj.lightList[o].y && y1 <= (bigObj.lightList[o].y + bigObj.lightList[o].h)) {
            num++
          }
          if (x1 >= bigObj.lightList[o].x && x1 <= (bigObj.lightList[o].x + bigObj.lightList[o].w) &&
            y2 >= bigObj.lightList[o].y && y2 <= (bigObj.lightList[o].y + bigObj.lightList[o].h)) {
            num++
          }
          if (x2 >= bigObj.lightList[o].x && x2 <= (bigObj.lightList[o].x + bigObj.lightList[o].w) &&
            y1 >= bigObj.lightList[o].y && y1 <= (bigObj.lightList[o].y + bigObj.lightList[o].h)) {
            num++
          }
          if (x2 >= bigObj.lightList[o].x && x2 <= (bigObj.lightList[o].x + bigObj.lightList[o].w) &&
            y2 >= bigObj.lightList[o].y && y2 <= (bigObj.lightList[o].y + bigObj.lightList[o].h)) {
            num++
          }
        }
      }
    }
    return num == 0
  }
}

/*
* 检查实验室/灯设置
* 传入:mapList-布局数据
* 返回:true 或者 false
* 示例:(本JS内部调用)
*   if(checkInstall(this.mapList){}
*/
export function checkInstall(mapList){
  for(let i=0;i<mapList.length;i++){
    if(mapList[i].type == 1){
      if(!mapList[i].roomType){
        Message({
          message: '请给每一个房间设定参数',
          type: 'error',
          offset: 100
        })
        return false
      }
    }else if(mapList[i].type == 2){
      for(let o=0;o<mapList[i].lightList.length;o++){
        if(!mapList[i].lightList[o].state){
          Message({
            message: '请给每一个灯设定参数',
            type: 'error',
            offset: 100
          })
          return false
        }
      }
    }
  }
  return true
}
