local HeroStationData = BaseClass("HeroStationData")
local __init = function(self)
  self.uid = 0
  self.stationId = 0
  self.cdTime = 0
  self.heroInfos = {}
  self.heroInfosBackup = {}
  self.slotUnlockLevelDict = {}
  self.skillList = {}
  self.usableSkillList = {}
  self.isDirty = false
  self.skillUsableCache = false
  self.effectOldVal = nil
end
local __delete = function(self)
  self.uid = nil
  self.stationId = nil
  self.cdTime = nil
  self.heroInfos = nil
  self.heroInfosBackup = nil
  self.slotUnlockLevelDict = nil
  self.skillList = nil
  self.usableSkillList = nil
  self.isDirty = nil
  self.skillUsableCache = nil
  self.effectOldVal = nil
end
local Init = function(self, stationId, serverData)
  self.stationId = stationId
  if serverData ~= nil then
    self:UpdateServerData(serverData)
  end
  local skillTemplateList = DataCenter.HeroStationSkillTemplateManager:GetTemplateListByStationId(stationId)
  if not table.IsNullOrEmpty(skillTemplateList) then
    self.skillList = {}
    self.usableSkillList = {}
    for _, skillTemplate in pairs(skillTemplateList) do
      self.buildId = skillTemplate.building
      table.insert(self.skillList, tonumber(skillTemplate.id))
      if skillTemplate.usable then
        table.insert(self.usableSkillList, tonumber(skillTemplate.id))
      end
    end
  end
  self.isDirty = false
  self.skillUsableCache = false
  self.effectOldVal = nil
end
local UpdateServerData = function(self, serverData)
  if serverData.uid then
    self.uid = serverData.uid
  end
  if serverData.cdTime then
    self.cdTime = serverData.cdTime
  end
  if serverData.heroInfos then
    self.heroInfos = serverData.heroInfos
  end
end
local GetHeroUuids = function(self)
  local heroUuids = {}
  for index = 1, DataCenter.HeroStationManager.HERO_COUNT do
    for _, heroInfo in pairs(self.heroInfos) do
      if heroInfo.index == index then
        local heroData = DataCenter.HeroDataManager:GetHeroByUuid(heroInfo.heroUuid)
        if heroData then
          heroUuids[index] = heroInfo.heroUuid
        end
      end
    end
  end
  return heroUuids
end
local GetHeroUuid = function(self, index)
  for _, heroInfo in pairs(self.heroInfos) do
    if heroInfo.index == index then
      local heroData = DataCenter.HeroDataManager:GetHeroByUuid(heroInfo.heroUuid)
      if heroData then
        return heroInfo.heroUuid
      end
    end
  end
  return nil
end
local SetHero = function(self, index, heroUuid)
  local inserted = false
  for k, v in pairs(self.heroInfos) do
    if v.index == index then
      v.heroUuid = heroUuid
      inserted = true
    elseif v.heroUuid == heroUuid then
      self.heroInfos[k] = nil
    else
      local heroData1 = DataCenter.HeroDataManager:GetHeroByUuid(v.heroUuid)
      local heroData2 = DataCenter.HeroDataManager:GetHeroByUuid(heroUuid)
      if heroData1 and heroData2 and heroData1.heroId == heroData2.heroId then
        self.heroInfos[k] = nil
      end
    end
  end
  if not inserted then
    local heroInfo = {index = index, heroUuid = heroUuid}
    table.insert(self.heroInfos, heroInfo)
  end
end
local RemoveHero = function(self, index)
  for k, v in pairs(self.heroInfos) do
    if v.index == index then
      self.heroInfos[k] = nil
      return true
    end
  end
  return false
end
local BackUpHeroInfos = function(self)
  self.heroInfosBackup = DeepCopy(self.heroInfos)
end
local RestoreHeroInfos = function(self)
  self.heroInfos = self.heroInfosBackup
end
local ToMessageParam = function(self)
  local param = {
    uid = self.uid,
    stationId = self.stationId,
    cdTime = self.cdTime,
    heroInfos = self.heroInfos
  }
  return param
end
HeroStationData.__init = __init
HeroStationData.__delete = __delete
HeroStationData.Init = Init
HeroStationData.UpdateServerData = UpdateServerData
HeroStationData.GetHeroUuids = GetHeroUuids
HeroStationData.GetHeroUuid = GetHeroUuid
HeroStationData.SetHero = SetHero
HeroStationData.RemoveHero = RemoveHero
HeroStationData.BackUpHeroInfos = BackUpHeroInfos
HeroStationData.RestoreHeroInfos = RestoreHeroInfos
HeroStationData.ToMessageParam = ToMessageParam
return HeroStationData
