local DefenceWallDataManager = BaseClass("ArmyFormationDataManager")
local Localization = CS.GameEntry.Localization
local __init = function(self)
  self.defenceWallData = {}
  self.defenceFormationMaxSize = 0
  self.defFormationFirstMaxCount = 0
  self.defFormationSecondMaxCount = 0
  self.defFormationThirdMaxCount = 0
  self.defDomeMaxNum = 0
  self.defDomeAddSpeed = 0
  self.fixPercentOnce = 0
  self.guardArmyInfo = nil
  self.state = false
  EventManager:GetInstance():AddListener(EventId.EffectNumChange, self.UpdateValueSignal)
end
local __delete = function(self)
  self.defenceWallData = nil
  self.guardArmyInfo = nil
  self.state = false
  EventManager:GetInstance():RemoveListener(EventId.EffectNumChange, self.UpdateValueSignal)
end
local InitData = function(self, message)
  self:UpdateValue()
  self:UpdateDefenceWallData(message.defend_wall)
  if message.defend_wall ~= nil then
    local dic = message.defend_wall
    if dic.cityBroken ~= nil then
      self.state = dic.cityBroken
      if self.state == true then
        Logger.Log("show broken")
        UIUtil.ShowMessage(Localization:GetString("300543"), 1, GameDialogDefine.CONFIRM, GameDialogDefine.CANCEL, function()
          GoToUtil.GotoMainBuildPos()
        end, function()
          GoToUtil.GotoMainBuildPos()
        end, function()
          GoToUtil.GotoMainBuildPos()
        end)
      end
    end
  end
end
local UpdateDefenceWallData = function(self, message)
  if message ~= nil then
    self.defenceWallData = DefenceWallData.New()
    self.defenceWallData:ParseData(message)
  end
end
local UpdateCurDurability = function(self, message)
  if message.uid ~= nil then
    local uid = message.uid
    if uid == LuaEntry.Player.uid and message.nowDurability ~= nil and self.defenceWallData ~= nil then
      self.defenceWallData:SetCurDurability(message.nowDurability)
    end
  end
end
local UpdateColdDownTime = function(self, message)
  if message.lastGoldRecoverDurabilityTime ~= nil and self.defenceWallData ~= nil then
    self.defenceWallData:SetColdDownTime(message.lastGoldRecoverDurabilityTime)
  end
end
local UpdateValueSignal = function(data)
  DataCenter.DefenceWallDataManager:UpdateValue()
end
local UpdateValue = function(self)
  self.defenceFormationMaxSize = LuaEntry.Effect:GetGameEffect(EffectDefine.APS_DEFENCE_FORMATION_SIZE)
  self.defFormationFirstMaxCount = LuaEntry.Effect:GetGameEffect(EffectDefine.APS_DEFENCE_FORMATION_FIRST_HERO_COUNT)
  self.defFormationSecondMaxCount = LuaEntry.Effect:GetGameEffect(EffectDefine.APS_DEFENCE_FORMATION_SECOND_HERO_COUNT)
  self.defFormationThirdMaxCount = LuaEntry.Effect:GetGameEffect(EffectDefine.APS_DEFENCE_FORMATION_THIRD_HERO_COUNT)
  self.defDomeMaxNum = 0
  self.defDomeAddSpeed = 0
  self.fixPercentOnce = LuaEntry.DataConfig:TryGetNum("city_wall", "k1")
  self.fixDiamond = LuaEntry.DataConfig:TryGetNum("city_wall", "k2")
  self.fixColdDownTime = LuaEntry.DataConfig:TryGetNum("city_wall", "k3")
  local buildLevelTemplate = DataCenter.BuildTemplateManager:GetBuildingLevelTemplate(BuildingTypes.FUN_BUILD_MAIN, DataCenter.BuildManager.MainLv)
  if buildLevelTemplate ~= nil then
    self.defDomeMaxNum = buildLevelTemplate:GetDefenceWallMax()
    self.defDomeAddSpeed = buildLevelTemplate:GetDefenceWallCoverSpeed()
  end
end
local GetConfigData = function(self)
  local oneData = {}
  oneData.defenceFormationMaxSize = self.defenceFormationMaxSize
  oneData.defFormationFirstMaxCount = self.defFormationFirstMaxCount
  oneData.defFormationSecondMaxCount = self.defFormationSecondMaxCount
  oneData.defFormationThirdMaxCount = self.defFormationThirdMaxCount
  oneData.defDomeMaxNum = 0
  oneData.defDomeAddSpeed = 0
  local buildLevelTemplate = DataCenter.BuildTemplateManager:GetBuildingLevelTemplate(BuildingTypes.FUN_BUILD_MAIN, DataCenter.BuildManager.MainLv)
  if buildLevelTemplate ~= nil then
    oneData.defDomeMaxNum = buildLevelTemplate:GetDefenceWallMax()
    oneData.defDomeAddSpeed = buildLevelTemplate:GetDefenceWallCoverSpeed()
  end
  oneData.fixPercentOnce = self.fixPercentOnce
  oneData.fixDiamond = self.fixDiamond
  oneData.fixColdDownTime = self.fixColdDownTime
  return oneData
end
local GetDefenceWallData = function(self)
  return self.defenceWallData
end
local GetMaxDefenceNum = function(self)
  return self.defenceFormationMaxSize
end
local UpdateGuardArmyInfo = function(self, message)
  if message.armyInfo ~= nil then
    self.guardArmyInfo = PBController.ParsePb1(message.armyInfo, "protobuf.ArmyUnitInfo")
  end
end
local GetGuardArmyInfo = function(self)
  return self.guardArmyInfo
end

function DefenceWallDataManager:IsInShield()
  if not self.defenceWallData then
    return false
  end
  local protectEndTime = self.defenceWallData.protectEndTime
  return protectEndTime - UITimeManager:GetInstance():GetServerTime() > 0
end

function DefenceWallDataManager:GetBreakState()
  return self.state
end

function DefenceWallDataManager:GetScoutAlarmIsOn()
  if self.investigationIsOn == nil then
    self.investigationIsOn = CommonUtil.PlayerPrefsGetBool("Investigation", true)
  end
  return self.investigationIsOn
end

function DefenceWallDataManager:RefreshScoutAlarmIsOn()
  self.investigationIsOn = CommonUtil.PlayerPrefsGetBool("Investigation", true)
end

DefenceWallDataManager.__init = __init
DefenceWallDataManager.__delete = __delete
DefenceWallDataManager.UpdateCurDurability = UpdateCurDurability
DefenceWallDataManager.UpdateDefenceWallData = UpdateDefenceWallData
DefenceWallDataManager.UpdateValue = UpdateValue
DefenceWallDataManager.UpdateValueSignal = UpdateValueSignal
DefenceWallDataManager.GetConfigData = GetConfigData
DefenceWallDataManager.GetDefenceWallData = GetDefenceWallData
DefenceWallDataManager.InitData = InitData
DefenceWallDataManager.UpdateColdDownTime = UpdateColdDownTime
DefenceWallDataManager.GetMaxDefenceNum = GetMaxDefenceNum
DefenceWallDataManager.UpdateGuardArmyInfo = UpdateGuardArmyInfo
DefenceWallDataManager.GetGuardArmyInfo = GetGuardArmyInfo
return DefenceWallDataManager
