local this = class("cameraController_photo", require("ui.manager.camera.controller.cameraController"))
local PATH_CAMERA = "Program/Camera/pre_photoCamera.prefab"
local _Camera_Dis_Hero = L_GameConstTpl:getValue("CAMERA_DIS_HERO", true, 2)
local _Camera_Dis_Mount = L_GameConstTpl:getValue("PHOTOGRAPH_CAMERA_MOUNT", true, 2)
local _Camera_Dis = L_GameClientTpl:getValue("PHOTO_CAMERA_SNAPSHOT")
local _MaxDis = 10
local _MinDis = 1

function this:ctor(mode)
  this.super.ctor(self, mode)
end

function this:switchIn()
  self:initVirCamera()
end

function this:switchOut()
  self:onDestroy()
end

function this:bindEntity(entity)
  if entity == L_PlayerManager:getMainHeroEntity() then
    C_CameraManager.SetFollowTarget(nil)
    local lookAt = C_CameraManager.GetLookAtNode()
    local follow = C_CameraManager.GetFollowNode()
    self.virCamera.LookAt = lookAt.transform
    self.virCamera.Follow = follow.transform
  else
    C_CameraManager.SetFollowTarget(entity:getTransform())
  end
end

function this:refreshVirCameraOffset(entity)
  self:refreshTrack(entity)
  self:refreshPov(entity)
end

function this:onDestroy()
  self:refreshRotation(0, true)
  if L_CommonUtil.isValid(self.virCamera) then
    C_GameObject.Destroy(self.virCamera.gameObject)
  end
  if L_CommonUtil.isValid(self.cameraTactics) then
    C_CameraManager.ExitAndCompleteCamera(self.cameraTactics)
    self.cameraTactics:Dispose()
    C_CameraManager.RemoveTactics(self.cameraTactics)
  end
  self.virCamera = nil
  self.cameraTactics = nil
end

function this:initVirCamera()
  local goVirCam = L_GameUtil.loadVirtualCamera(nil, PATH_CAMERA)
  self.virCamera = goVirCam:GetComponent(typeof(C_CinemachineVirtualCamera))
  self.fixedFollow = goVirCam:GetComponent(typeof(C_CameraFixedFollow))
  self.cameraTactics = C_CameraManager.CreateCommonTactics(goVirCam, C_CinemachineBlendType.Cut, true)
end

function this:refreshTrack(entity)
  self.fixedFollow:SwitchControllingEntity(CS.Lens.Gameplay.Modules.BigWorld.EEventType.SwitchControlEntity, nil)
end

function this:refreshPov(entity)
  local rotation = L_Vector3.getRot(entity:getTransform())
  self.cameraTactics:SetCameraPOVHorizontal(rotation.y + 180)
  self.cameraTactics:SetCameraPOVVertical(0)
  self.cameraTactics:SetCameraFov(60)
  local dis = _Camera_Dis[2] or 2
  self.cameraTactics:SetCameraDis(dis)
end

function this:refreshRotation(angle, isAbsolute)
  local node = self.virCamera.transform.parent
  if isAbsolute then
    L_Vector3.setLocalRot(node, L_Vector3.getTemp(angle, angle, angle))
  else
    L_Vector3.setLocalRot(node, L_Vector3.getTemp(0, 0, 0))
    local axis = self.virCamera.transform.forward
    local pos = node.position
    node:RotateAround(pos, axis, angle)
  end
end

function this:resetDistanceLimit()
  local fixFollow = L_CameraManager:getFixedFollow()
  fixFollow.distanceMax = _MaxDis
  fixFollow.distanceMin = _MinDis
end

return this
