import * as THREE from '../build/three.module.js'
class DMT_Light {
  constructor(scene, options) {
    this.mapSizeList = [
      { width: 512, height: 512 },
      { width: 1024, height: 1024 },
      { width: 2048, height: 2048 }
    ]
    // 默认配置
    const defaults = {
      type: 'DirectionalLight', // 光源类型 DirectionalLight 或 PointLight
      used: false, // 是否启用光源
      color: '#ffffff', // 光源颜色
      intensity: 1, // 光源强度
      power: 100, // 光源功率
      targetModelId: '',
      bias: '', // 阴影偏移
      position: { x: 0, y: 0, z: 5 }, // 光源位置
      castShadow: false, // 是否投射阴影
      shadowQuality: 1
    }
    // 合并配置
    this.config = { ...defaults, ...options }
    this.light = null
    this.lightHelper = null
    this.scene = scene
  }
  createDirectionalLight() {
    this.config.type = 'DirectionalLight'
    if (this.config.used) {
      const { color, intensity, position, castShadow, shadowQuality } = this.config
      this.light = new THREE.DirectionalLight(new THREE.Color(color), intensity)
      this.light.position.set(position.x, position.y, position.z)
      if (castShadow) {
        this.light.castShadow = true
        this.light.shadow.bias = this.config.bias
        this.light.shadow.mapSize.width = this.mapSizeList[shadowQuality].width
        this.light.shadow.mapSize.height = this.mapSizeList[shadowQuality].height
      }
      this.scene.add(this.light)
    }
  }
  createPointLight() {
    this.config.type = 'PointLight'
    if (this.config.used) {
      const { color, intensity, position, castShadow, shadowQuality } = this.config
      this.light = new THREE.PointLight(new THREE.Color(color), intensity)
      this.light.position.set(position.x, position.y, position.z)
      this.light.power = this.config.power
      if (castShadow) {
        this.light.castShadow = true
        this.light.shadow.bias = this.config.bias
        this.light.shadow.mapSize.width = this.mapSizeList[shadowQuality].width
        this.light.shadow.mapSize.height = this.mapSizeList[shadowQuality].height
      }
      this.scene.add(this.light)
    }
  }
  updateUsed(used) {
    if (this.config.used === used) return
    this.config.used = used
    if (used) {
      if (this.config.type === 'DirectionalLight') {
        this.createDirectionalLight()
      } else if (this.config.type === 'PointLight') {
        this.createPointLight()
      }
    } else {
      this.removeLight()
    }
  }
  updateColor(color) {
    this.config.color = color
    if (this.light) {
      this.light.color.set(color)
    }
  }
  updateIntensity(intensity) {
    this.config.intensity = intensity
    if (this.light) {
      this.light.intensity = intensity
    }
  }
  updateShadowBias(bias) {
    this.config.bias = bias
    if (this.light && this.light.castShadow) {
      this.light.shadow.bias = bias
    }
  }
  updatePosition(position) {
    this.config.position = position
  }
  showLightHelper(show) {
    if (this.config.type === 'DirectionalLight') {
      if (show && !this.lightHelper) {
        this.lightHelper = new THREE.DirectionalLightHelper(this.light, 1)
        this.scene.add(this.lightHelper)
      } else if (!show && this.lightHelper) {
        this.scene.remove(this.lightHelper)
        this.lightHelper = null
      }
    } else {
      if (show && !this.lightHelper) {
        this.lightHelper = new THREE.PointLightHelper(this.light, 1)
        this.scene.add(this.lightHelper)
      } else if (!show && this.lightHelper) {
        this.scene.remove(this.lightHelper)
        this.lightHelper = null
      }
    }
  }
  updateCastShadow(castShadow) {
    this.config.castShadow = castShadow
    if (this.light) {
      this.light.castShadow = castShadow
    }
  }
  updateShadowQuality(quality) {
    this.config.shadowQuality = quality
    if (this.light && this.light.castShadow) {
      const shadowQuality = this.mapSizeList[quality]
      this.light.shadow.map.setSize(shadowQuality.width, shadowQuality.height)
      this.light.shadow.mapSize.width = shadowQuality.width
      this.light.shadow.mapSize.height = shadowQuality.height
    }
  }
  updatePower(power) {
    this.config.power = power
    if (this.light && this.light instanceof THREE.PointLight) {
      this.light.power = power
    }
  }
  removeLight() {
    if (this.light) {
      this.light.parent.remove(this.light)
      this.light = null
    }
    if (this.lightHelper) {
      this.lightHelper.parent.remove(this.lightHelper)
      this.lightHelper = null
    }
  }
}
export default DMT_Light
