local this = class("modulePhotoGroup_wind", require("ui.pages.photo.group.modulePhotoGroup_base"))

function this.bind()
  return {txtForce = ""}
end

function this.methods()
  return {
    onValueChanged_force = function(self, val)
      self:setVal(L_Const.photoParam.windForce, val)
    end
  }
end

function this:open()
  L_PhotoManager:addListener(L_PhotoManager.event.photoTemplateChange, self.onEvent_photoTemplateChange, self)
  self:bindScreenTouch("+")
  local tpl = self.paramTpl:getTplById(L_Const.photoParam.windForce)
  local name = self.paramTpl:getName(tpl)
  self.bind.txtForce = name
  self:refreshSliderArea(L_Const.photoParam.windForce)
  self:initWindParam()
  self:refreshUI()
end

function this:close()
  L_PhotoManager:removeListener(L_PhotoManager.event.photoTemplateChange, self.onEvent_photoTemplateChange, self)
  self:bindScreenTouch("-")
end

function this:onEvent_photoTemplateChange()
  self:initWindParam()
  self:refreshUI()
end

function this:initWindParam()
  self.windSou = {}
  local wind = C_RealWeather.GetWeatherWind()
  if wind then
    local dir = wind:GetDirection()
    local power = wind:GetPower()
    self.windSou[L_Const.photoParam.windDirection] = dir
    self.windSou[L_Const.photoParam.windForce] = power
  end
end

function this:refreshUI()
  local dir = self.windSou[L_Const.photoParam.windDirection]
  if dir then
    local camera = L_CameraManager:getSceneCamera()
    local camRot = L_Vector3.getRot(camera.transform)
    self.bindComponents.rTransDir:SetLocalEulerAngles(0, 0, -dir.y + camRot.y + 90)
  end
  local power = self.windSou[L_Const.photoParam.windForce]
  if power then
    self.bindComponents.slider.value = power * 50
  end
end

function this:setVal(id, val)
  local wind = C_RealWeather.GetWeatherWind()
  if wind == nil then
    return
  end
  if id == L_Const.photoParam.windDirection then
    wind:SetDirection(val)
  elseif id == L_Const.photoParam.windForce then
    C_RealWeather.SetPower(val / 50)
  end
end

function this:bindScreenTouch(sign)
  self.onDragStartHandle = self.onDragStartHandle or L_CommonUtil.handle(self.onTouch_dragStart, self)
  self.onDragHandle = self.onDragHandle or L_CommonUtil.handle(self.onTouch_drag, self)
  C_ScreenTouch.OnUI_DragStart(sign, self.onDragStartHandle)
  C_ScreenTouch.OnUI_Drag(sign, self.onDragHandle)
end

function this:onTouch_dragStart(gesture)
  self.inDir = gesture.go == self.bindComponents.rTransDrag.gameObject
end

function this:onTouch_drag()
  if not self.inDir then
    return
  end
  local dir = self.bindComponents.rTransDir
  local screenPos = C_CameraManager.uiCamera:WorldToScreenPoint(dir.position)
  local v3 = CS.UnityEngine.Input.mousePosition - screenPos
  local angle = math.atan(v3.y, v3.x) * (180 / math.pi)
  dir:SetLocalEulerAngles(0, 0, angle)
  local camera = L_CameraManager:getSceneCamera()
  local camRot = L_Vector3.getRot(camera.transform)
  self:setVal(L_Const.photoParam.windDirection, L_Vector3.getTemp(0, -angle + camRot.y + 90, 0))
end

return this
