local this = class("cameraController_npcShop", require("ui.manager.camera.controller.cameraController"))
local PATH_CAMERA = "Program/Camera/pre_cinemachineVirtualNpcCamera.prefab"
local _shopGroupTpl = L_GameTpl:getShopGroupTpl()

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

function this:switchIn(preMode, params)
  self:initVirCamera()
  self.cameraTactics:SetEnabled(true)
end

function this:switchOut(nextMode)
  C_CameraManager.SetBrainBlendStyle(L_Const.blendStyle.cut)
  self:onDestroy()
end

function this:bindEntity(entity)
  self.cameraTactics.cineMachineCamera.Follow = entity:GetTransform()
  self.cameraTactics.cineMachineCamera.LookAt = entity:GetTransform()
  self:refreshVirCameraOffsetNew(entity)
end

function this:refreshVirCameraOffsetNew(entity)
  local body = self.virCamera:GetCinemachineComponent(0)
  local trackOffset = body.m_TrackedObjectOffset
  local unitId = entity:GetUnitId()
  trackOffset.y = L_GameUtil.getUnitCameraOffset(unitId)
  local curShopGroupId = L_ShopStore:getCurOpenShopGroupId()
  if 0 < curShopGroupId then
    local shopGroupCfg = _shopGroupTpl:getTplById(curShopGroupId)
    if shopGroupCfg then
      local npcCameraParam = _shopGroupTpl:getNpcCameraParam(shopGroupCfg)
      trackOffset.x = trackOffset.x + npcCameraParam.x
      trackOffset.y = trackOffset.y + npcCameraParam.y
      trackOffset.z = trackOffset.z + npcCameraParam.z
    end
  end
  body.m_TrackedObjectOffset = trackOffset
  local horizontal = self.virCamera:GetCinemachineComponent(1).m_HorizontalAxis
  local rotation = L_Vector3.getRot(entity:GetTransform())
  horizontal.Value = horizontal.Value + tonumber(string.format("%.2f", rotation.y))
  self.virCamera:GetComponent(typeof(CS.Cinemachine.CinemachineVirtualCamera)):GetCinemachineComponent(1).m_HorizontalAxis = horizontal
end

function this:setBlendStyle(style, time)
  C_CameraManager.SetBrainBlendStyle(style)
  C_CameraManager.SetBrainBlendTime(time / 1000)
end

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

function this:onDestroy()
  if L_CommonUtil.isValid(self.virCamera) then
    C_GameObject.Destroy(self.virCamera.gameObject)
  end
  self.virCamera = nil
  C_CameraManager.RemoveTactics(self.cameraTactics)
  self.entity = nil
end

return this
