import { message } from './index'
export default class CtDotLineArea {
  constructor() {
    this.point = null // 采集的三维点
    this.point_digital = [] // 三维点坐标
    this.line = null // 采集的线
    this.line_point = [] // 线段拐点的三维点
    this.line_digital = [] // 线坐标 应为二维坐标数组
    this.area = null // 采集的面
    this.area_point = [] // 面拐点的三维点
    this.area_digital = [] // 面坐标 应为三维坐标数组
    this.dotName = '' // 孪生体名称
    this.tempLine = [] // 临时线
  }

  /**
   * @description 创建点线面入口
   * @param {string} type 创建类型 dot：点 line：线 area：面
   */
  intoCollect(type) {
    const map = {
      dot: {
        text: '开始采集点位数据，左键选择，右键双击保存并退出',
        fn: this.collectPoint.bind(this),
      },
      line: {
        text: '开始采集线数据，左键选点，右键取消上一个点位，右键双击保存并退出',
        fn: this.collectLine.bind(this),
      },
      area: {
        text: '开始采集面数据，左键选点，右键取消上一个点位，右键双击保存并退出',
        fn: this.collectArea.bind(this),
      },
    }
    this.popTip(map[type].text)
    map[type].fn()
  }

  // 采集点
  collectPoint() {
    this.point_digital = []
    document.body.style.cursor = 'crosshair'
    app.on(
      'click',
      (e) => {
        // 采点
        if (e.pickedPosition && e.button === 0) {
          if (this.point) {
            this.point.destroy()
          }
          this.point = this.ctPoint(e.pickedPosition)
          this.point_digital = e.pickedPosition
        }
      },
      'collectPoint'
    )
    //   退出采点
    app.once(
      'dblclick',
      (e) => {
        if (e.button === 2) {
          app.off('click', null, 'collectPoint')
          document.body.style.cursor = 'default'
          if (this.point) {
            this.point.destroy()
          }
          this.copy(this.point_digital)
        }
      },
      'quitCollectPoint'
    )
  }

  //   创建三维点
  /* eslint-disable class-methods-use-this */
  ctPoint(pos) {
    return new THING.Sphere({
      name: 'newPoint',
      localPosition: pos,
      scale: [1, 1, 1],
    })
  }

  //   采集线
  collectLine() {
    this.line_digital = []
    document.body.style.cursor = 'crosshair'
    // 采集线
    app.on(
      'click',
      (e) => {
        // 采集线
        if (e.button === 0 && e.pickedPosition) {
          // 采点
          const [x, y, z] = e.pickedPosition
          const dotPos = [x, y + 0.05, z]
          // 创建点
          this.line_point.push(this.ctPoint(dotPos))
          this.line_digital.push(dotPos)
          this.ctCltLine()
          if (this.line_digital.length) {
            this.updateLine([dotPos])
          }
        } else if (e.button === 2 && this.line_point.length) {
          // 删除上一个点
          const delPoint = this.line_point.pop()
          delPoint.destroy()
          // 重新创建线
          this.line_digital.pop()
          this.ctCltLine()
          if (this.line_digital.length) {
            this.updateLine([this.line_digital[this.line_digital.length - 1]])
          }
        }
      },
      'collectLine'
    )
    app.on(
      'dblclick',
      (e) => {
        if (e.button === 2) {
          this.quitUpdataLine()

          app.off('click', null, 'collectLine')
          document.body.style.cursor = 'default'
          if (this.line) {
            this.line.destroy()
          }
          this.line_point.forEach((obj) => {
            try {
              obj.destroy()
            } catch (eve) {
              console.warn(eve)
            }
          })
          this.line_point = []
          this.copy(this.line_digital)

          app.off('dblclick', null, 'quitCollectLine')
        }
      },
      'quitCollectLine'
    )
  }

  //   创建采集线
  ctCltLine() {
    if (this.line) {
      this.line.destroy()
    }
    if (this.line_digital.length > 1) {
      this.line = this.ctLine(this.line_digital)
    }
  }

  // 创建三维线
  ctLine(path) {
    const line = new THING.PixelLine({
      name: 'Pixelline',
      selfPoints: path,
      style: {
        color: '#eb3941',
        sideType: THING.SideType.Double,
      },
    })
    line.pickable = false // fix: 采集时鼠标点击到线时 e.pickedPosition 会是 null, 采集不到坐标位置
    return line
  }

  // 采集面
  collectArea() {
    this.area_digital = []
    document.body.style.cursor = 'crosshair'
    app.on(
      'click',
      (e) => {
        if (e.button === 0 && e.pickedPosition) {
          const [x, y, z] = e.pickedPosition
          const dotPos = [x, y + 0.05, z]
          this.area_point.push(this.ctPoint(dotPos))
          this.area_digital.push(dotPos)
          this.ctCltArea()
          if (this.area_digital.length === 1) {
            this.updateLine([dotPos])
          } else if (this.area_digital.length > 1) {
            this.updateLine([this.area_digital[0], dotPos])
          }
        } else if (e.button === 2 && this.area_point.length) {
          // 删除上一个点
          const delPoint = this.area_point.pop()
          delPoint.destroy()
          // 重新创建面
          this.area_digital.pop()
          this.ctCltArea()
          if (this.area_digital.length === 1) {
            this.updateLine(this.area_digital)
          } else if (this.area_digital.length > 1) {
            this.updateLine([
              this.area_digital[0],
              this.area_digital[this.area_digital.length - 1],
            ])
          }
        }
      },
      'collectArea'
    )
    app.on(
      'dblclick',
      (e) => {
        if (e.button === 2) {
          app.off('click', null, 'collectArea')
          this.quitUpdataLine()

          document.body.style.cursor = 'default'
          if (this.area) {
            this.area.destroy()
          }
          this.area_point.forEach((obj) => {
            obj.destroy()
          })
          this.area_point = []
          app.off('dblclick', null, 'quitCollectArea')
          this.copy(this.area_digital)
        }
      },
      'quitCollectArea'
    )
  }

  ctCltArea() {
    if (this.area) {
      this.area.destroy()
    }
    if (this.area_digital.length > 2) {
      this.area = this.ctArea(this.area_digital)
    }
  }

  // 创建三维面
  ctArea(data) {
    return new THING.PlaneRegion({
      selfPoints: data,
      style: {
        color: '#c8eefe',
        outlineColor: '#eb3941',
      },
    })
  }

  // 采集孪生体名称
  getDotName() {
    tipInfo('左键单击选择孪生体，右键双击退出', 5000)
    document.body.style.cursor = 'pointer'
    app.on(
      'click',
      (e) => {
        // 采点
        if (e.object && e.object.name) {
          this.dotName = e.object.name
        }
      },
      'getPointName'
    )
    app.on(
      'dblclick',
      (e) => {
        if (e.button === 2) {
          app.off('click', null, 'gettPointName')
          document.body.style.cursor = 'default'
          this.dotName = ''
          app.off('dblclick', null, 'quitGetPointName')
        }
      },
      'quitGetPointName'
    )
  }

  // 采集摄像头位置
  getCamera() {
    const cameraInfo = [app.camera.position, app.camera.target]
    this.copy(cameraInfo)
  }

  copy(text) {
    const input = document.createElement('input')
    document.body.appendChild(input)
    input.setAttribute('value', JSON.stringify(text))
    input.select()
    if (document.execCommand('copy')) {
      document.execCommand('copy')
    }
    document.body.removeChild(input)
    message.success('信息已经复制在您的剪贴板中')
  }

  // 虚拟线
  updateLine(originDot) {
    app.off('mouseMove', null, 'mouseMoveCHangeLine')
    app.on(
      'mouseMove',
      (e) => {
        if (!e.pickedPosition) {
          return
        }

        if (this.tempLine.length) {
          this.tempLine[0].points = [originDot[0], e.pickedPosition]
          if (originDot[1] && this.tempLine[1]) {
            this.tempLine[1].points = [originDot[1], e.pickedPosition]
          } else {
            const line2 = new THING.PixelLine({
              name: 'Pixelline',
              point: [originDot[1], e.pickedPosition],
              style: {
                color: '#eb3941',
                sideType: THING.SideType.Double,
              },
            })
            line2.pickable = false
            this.tempLine.push(line2)
          }
        } else {
          const line1 = new THING.PixelLine({
            name: 'Pixelline',
            point: [originDot[0], e.pickedPosition],
            style: {
              color: '#eb3941',
              sideType: THING.SideType.Double,
            },
          })
          line1.pickable = false
          this.tempLine.push(line1)
        }
      },
      'mouseMoveCHangeLine'
    )
  }

  quitUpdataLine() {
    app.off('mouseMove', null, 'mouseMoveCHangeLine')
    this.tempLine.forEach((item) => {
      item.destroy()
    })
    this.tempLine = []
  }

  //   弹出文本提示
  popTip(text) {
    return message.info(text)
  }

  onDestroy(){
    this.inter && clearTimeout(this.inter)
  }
}
