local this = class("modulePhotoGroup_lightArrange", require("ui.pages.photo.group.modulePhotoGroup_base"))
local _screenPosToUI = Unity.RectTransformUtility.ScreenPointToLocalPointInRectangle

function this.bind()
  return {
    modulePhotoLightSetting = {
      moduleName = "pages/photo/submodule/modulePhotoLightSetting"
    },
    isActive_lightDrag = false,
    listTab = {
      moduleName = "pages/photo/cell/cellPhotoLightTab"
    },
    tabId = 0
  }
end

function this.methods()
  return {
    onDrag_light = function(self, screenPos)
      self.modules.modulePhotoLightSetting:onDrag_light(screenPos)
      self.isDragging = true
    end,
    onEndDrag_light = function(self, screenPos)
      self.isDragging = false
    end,
    onClick_lightTab = function(self, tabId)
      if self.bind.tabId == tabId then
        return
      end
      self.bind.tabId = tabId
      self:onSelectLight(tabId)
    end,
    modulePhotoLightSetting = {
      setLightOn = function(self, id, isOn)
        self:refreshTabStatus(id, isOn)
      end,
      setLightColor = function(self, lightData)
        self:refreshTabColor(lightData.lightId)
      end
    }
  }
end

function this:open()
  if not self.lightData then
    self.lightData = {}
    self:initLightData()
  end
  self.modules.modulePhotoLightSetting.groupBase = self
  self:initTab()
  self.bind.tabId = 1
end

function this:show()
  self.isDragging = false
  self.timer = Timer.repeated(0, self.update, self)
end

function this:refresh()
end

function this:initLightData()
  for i = 1, 3 do
    local data = require("ui.pages.photo.submodule.photoLightData").new()
    data.lightId = i
    self.lightData[i] = data
    data:reset(true)
  end
end

function this:initTab()
  self.bind.listTab:clear()
  for i = 1, 3 do
    local tab = {
      tabId = i,
      tabGroup = self.bindComponents.tabGroup
    }
    self.bind.listTab:insert(tab)
  end
end

function this:onSelectLight(id)
  if id <= 0 or 3 < id then
    return
  end
  if self.lightData[id] then
    self.modules.modulePhotoLightSetting:initWithData(self.lightData[id])
    self.bind.isActive_lightDrag = self.lightData[id].isOn
    self:syncDragBtnPos(self.lightData[id])
  end
end

function this:refreshTabColor(id)
  local m = self.modules.listTab[id]
  if m then
    self.bind.listTab:change(id, {
      color = self.lightData[id].color
    })
  end
end

function this:refreshTabStatus(id, isOn)
  local m = self.modules.listTab[id]
  if m then
    self.bind.listTab:change(id, {
      isOn = isOn,
      color = self.lightData[id].color
    })
    self.bind.isActive_lightDrag = self.lightData[id].isOn
    self:syncDragBtnPos(self.lightData[id])
  end
end

function this:syncDragBtnPos(lightData)
  if lightData and lightData.isOn and self.bind.isActive_lightDrag and not self.isDragging then
    local screenPos = C_CameraManager.mainCamera:WorldToScreenPoint(lightData.position)
    local v2 = C_Vector2(screenPos.x, screenPos.y)
    local _, pos = _screenPosToUI(self.bindComponents.btnDrag_light.transform.parent, v2, C_CameraManager.uiCamera)
    L_Vector3.setAnchored(self.bindComponents.btnDrag_light, pos)
  end
end

function this:hide()
  Timer.remove(self.timer)
end

function this:close()
  for i, v in pairs(self.lightData) do
    v:destroyObj()
  end
end

function this:update()
  self:syncDragBtnPos(self.lightData[self.bind.tabId])
end

return this
