local this = class("formationRuntimeData")
local tInsert = table.insert
local sConcat = string.concat
local camBlendPath = "Program/UIScene/Formations/ast_FormationBlends.asset"
local anyCamName = "**ANY CAMERA**"

function this:ctor(sceneGo)
  self.formationScene = sceneGo
  self.editStatus = L_FormationConst.FormationEditStatus.Overlook
  self.heroPoints = {}
  self.petPoints = {}
  self.modelCache = nil
  self.heroCache = {}
  self.petCache = {}
  self.selectEffectPoint = {}
  self.petFlashObj = {}
  self.placeHeroes = {}
  self.placePets = {}
  self.focusIndex = nil
  self.focusEffect = {}
  self.admissionEffectMap = {}
  self.cameraNodeData = {}
  self.cameraDurationMap = {}
  self.formationType = nil
  self.formationIndex = nil
  self.needRevertFormationScene = false
  self:_parseSceneModel()
end

function this:_parseSceneModel()
  local sceneTrans = self.formationScene.transform
  self.modelCache = sceneTrans:Find("Model/Cache")
  local pointTrans = sceneTrans:Find("Model/Points")
  local selectEffectPointsTrans = sceneTrans:Find("Model/SelectEffect")
  for i = 1, 3 do
    tInsert(self.heroPoints, pointTrans:Find(sConcat("point_hero_", i)))
    tInsert(self.petPoints, pointTrans:Find(sConcat("point_pet_", i)))
    tInsert(self.selectEffectPoint, selectEffectPointsTrans:Find(sConcat("pos", i)))
  end
  local cameraNode = sceneTrans:Find("Camera")
  self.cameraNodeData = {}
  for _, name in pairs(L_FormationConst.FormationCamName) do
    self.cameraNodeData[name] = {}
    self.cameraNodeData[name].obj = cameraNode:Find(name).gameObject
    if L_FormationConst.FormationCamTargetName[name] then
      self.cameraNodeData[name].targetObj = cameraNode:Find(L_FormationConst.FormationCamTargetName[name]).gameObject
    end
  end
  self.cameraDurationMap = {}
  local blenderSettingsHandle = C_LuaUtility.LoadBattleCinemachineBlenderSettingsConfig(camBlendPath)
  local blenderSettings = blenderSettingsHandle.config
  local customBlends = blenderSettings.m_CustomBlends
  for ii = 0, customBlends.Length - 1 do
    local blend = customBlends[ii]
    local fromName = blend.m_From
    local toName = blend.m_To
    local time = blend.m_Blend.BlendTime
    if fromName == anyCamName and toName ~= anyCamName then
      for _, name in pairs(L_FormationConst.FormationCamName) do
        self.cameraDurationMap[name] = self.cameraDurationMap[name] or {}
        self.cameraDurationMap[name][toName] = time
      end
    elseif toName == anyCamName and fromName ~= anyCamName then
      self.cameraDurationMap[fromName] = self.cameraDurationMap[fromName] or {}
      for _, name in pairs(L_FormationConst.FormationCamName) do
        self.cameraDurationMap[fromName][name] = time
      end
    elseif toName ~= anyCamName and fromName ~= anyCamName then
      self.cameraDurationMap[fromName] = self.cameraDurationMap[fromName] or {}
      self.cameraDurationMap[fromName][toName] = time
    end
  end
  C_LuaUtility.DestroyBattleCinemachineBlenderSettingsConfig(blenderSettingsHandle)
end

function this:destroy()
  if L_CommonUtil.isValid(self.formationScene) then
    C_GameObject.Destroy(self.formationScene)
  end
end

return this
