local this = class("modulePhotoBev", G_UIModuleBase)

function this.bind()
  return {imgBevIcon = "", imgBev = ""}
end

function this.methods()
  return {
    onClick_Bev = function(self)
      if L_UI:getPage("pagePhotographScene") ~= nil then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_task_photo_01"))
        return
      end
      self:triggerBev()
    end,
    onClick_jump = function(self)
      L_PlayerManager:fireJump()
    end,
    onClick_dodge = function(self)
      L_PlayerManager:fireEvade()
    end
  }
end

function this:open()
  L_GameUtil.setJoyStick(self.bindComponents.joystick)
  self:bindScreenTouch("+")
  self:bindCameraJoystick()
  self:resetScreen()
end

function this:close()
  self:bindScreenTouch("-")
  self:unbindCameraJoystick()
  if self.cameraJoystickTimer then
    Timer.remove(self.cameraJoystickTimer)
  end
end

function this:bindCameraJoystick()
  local joystick = self.bindComponents.cameraJoystick
  joystick.onPointerDownHandler = L_CommonUtil.handle(self.onCameraJoystickDragStart, self)
  joystick.onPointerUpHandler = L_CommonUtil.handle(self.onCameraJoystickDragEnd, self)
end

function this:unbindCameraJoystick()
  local joystick = self.bindComponents.cameraJoystick
  joystick.onPointerDownHandler = nil
  joystick.onPointerUpHandler = nil
end

function this:onCameraJoystickDragStart()
  if not self.cameraJoystickTimer then
    self.cameraJoystickTimer = Timer.repeated(0, function()
      local joystick = self.bindComponents.cameraJoystick
      local num = 0.011
      self:setScreen(-joystick.Horizontal * num, joystick.Vertical * num)
    end)
  else
    self.cameraJoystickTimer:resume()
  end
  self.parent:translateGrid(true)
end

function this:onCameraJoystickDragEnd()
  if self.cameraJoystickTimer then
    self.cameraJoystickTimer:pause()
  end
  self.parent:translateGrid(false)
end

function this:bindScreenTouch(sign)
  self.onClickHandle = self.onClickHandle or L_CommonUtil.handle(self.onTouch_click, self)
  C_ScreenTouch.OnUI_Click(sign, self.onClickHandle)
end

function this:onTouch_click(gesture)
  if gesture.go ~= self.bindComponents.rTransBev.gameObject then
    return
  end
  if not L_PhotoManager:checkPhotoCanTouchClick() then
    return
  end
  local mediator = L_PhotoManager:getMediator()
  if mediator.bevMode == L_Const.photoBevMode.capture then
    self:emit("onClick_capture")
  elseif mediator.bevMode == L_Const.photoBevMode.recording then
    self:emit("onClick_recording")
  end
end

function this:resetScreen()
  local _gameTpl = L_GameTpl:getGameConstTpl()
  self.screenBoard = {
    rangX = _gameTpl:getValue("PHOTO_X_MAX", true),
    rangY = _gameTpl:getValue("PHOTO_Y_MAX", true),
    speed = _gameTpl:getValue("PHOTO_MOVE", true),
    curX = 0.5,
    curY = 0.5
  }
  local tatics = L_CameraManager:getCameraTactics()
  tatics:SetCameraScreen(self.screenBoard.curX, self.screenBoard.curY)
end

function this:setScreen(x, y)
  local tatics = L_CameraManager:getCameraTactics()
  x = self.screenBoard.curX + x
  y = self.screenBoard.curY + y
  self.screenBoard.curX = math.clamp(x, 0.5 - self.screenBoard.rangX, 0.5 + self.screenBoard.rangX)
  self.screenBoard.curY = math.clamp(y, 0.5 - self.screenBoard.rangY, 0.5 + self.screenBoard.rangY)
  tatics:SetCameraScreen(x, y)
end

function this:getMediators()
  local calls = {
    bevMode = handler(self, this.translateBev)
  }
  return calls
end

function this:triggerBev()
  local mediator = L_PhotoManager:getMediator()
  mediator.bevMode = mediator.bevMode == L_Const.photoBevMode.capture and L_Const.photoBevMode.recording or L_Const.photoBevMode.capture
end

function this:translateBev(mode)
  local sp = mode == L_Const.photoBevMode.capture and "UI/Atlas/Photo/tex_photo_btn_photograph.png" or "UI/Atlas/Photo/tex_photo_btn_video.png"
  self.bind.imgBev = sp
  local icon = mode == L_Const.photoBevMode.capture and "UI/Atlas/Photo/tex_photo_btn_model2.png" or "UI/Atlas/Photo/tex_photo_btn_model1.png"
  self.bind.imgBevIcon = icon
  self.bindComponents.imgBevIcon:SetNativeSize()
end

return this
