local ClientCityVisitorManager = BaseClass("ClientCityVisitorManager")
local AddListeners = function(self)
  self.RefreshAllianceListCallback = BindCallback(self, self.RefreshAllianceList)
  EventManager:GetInstance():AddListener(EventId.SearchAllianceSuccess, self.RefreshAllianceListCallback)
end
local RemoveListener = function(self)
  EventManager:GetInstance():RemoveListener(EventId.SearchAllianceSuccess, self.RefreshAllianceListCallback)
  self.RefreshAllianceListCallback = nil
end
local __init = function(self)
  AddListeners(self)
  self.allianceUid = nil
  self.isEnterGame = nil
end
local __delete = function(self)
  RemoveListener(self)
  self.allianceUid = nil
  self.isEnterGame = nil
end
local ShowAllianceInviteVisitor = function(self)
  local openServerDay = UITimeManager:GetInstance():GetOpenServerDay()
  local configDays = LuaEntry.DataConfig:TryGetNum("alliance_invite_config", "k1", 0)
  if openServerDay > configDays then
    return
  end
  if not DataCenter.BuildManager:HasBuildByIdAndLevel(BuildingTypes.LW_BUILD_ALLIANCE_CENTER, 1) then
    return
  end
  if LuaEntry.Player:IsInAlliance() then
    return
  end
  if not self:GetTodayCanShowSecond(TodayNoSecondConfirmType.AllianceInviteVisitor) then
    return
  end
  self.isEnterGame = true
  SFSNetwork.SendMessage(MsgDefines.AlSearch, 1, 1, "", 0, true)
end

function ClientCityVisitorManager:RefreshAllianceList()
  if self.isEnterGame and SceneUtils.GetIsInCity() then
    self.isEnterGame = false
    local search_al_id_list = DataCenter.AllianceTempListManager:GetSearchAllianceIdList()
    local hasList = table.count(search_al_id_list) > 0
    if hasList then
      local allianceUid, tmpInfo
      for i = 1, table.count(search_al_id_list) do
        tmpInfo = DataCenter.AllianceTempListManager:GetSearchAllianceDataByUid(search_al_id_list[i])
        if tmpInfo and tmpInfo.recruitTotal == 0 then
          allianceUid = tmpInfo.uid
          break
        end
      end
      self.allianceUid = allianceUid
      if string.IsNullOrEmpty(allianceUid) then
        return
      end
      self:CreateAllianceInviteVisitor()
      self:SetTodayNoShowSecond(TodayNoSecondConfirmType.AllianceInviteVisitor)
    end
  end
end

local GetAllianceInviteVisitorData = function(self)
  return DataCenter.CityVisitorManager:CreateOneFakeVisitorDataByEventId(12001, 100012001)
end
local CreateAllianceInviteVisitor = function(self)
  local visitor = self:GetAllianceInviteVisitorData()
  if visitor then
    visitor.allianceUid = self.allianceUid
    DataCenter.CityVisitorManager:TryAddVisitor(visitor, false)
  end
end
local GetTodayCanShowSecond = function(self, showType)
  local time = Setting:GetPrivateString(showType, "")
  if time ~= "" then
    return not UITimeManager:GetInstance():IsSameDayForServer(tonumber(time), UITimeManager:GetInstance():GetServerSeconds())
  end
  return true
end
local SetTodayNoShowSecond = function(self, showType)
  Setting:SetPrivateString(showType, tostring(UITimeManager:GetInstance():GetServerSeconds()))
end
local OpenAllianceInvitePanel = function(self)
  UIManager:GetInstance():OpenWindow(UIWindowNames.UIAllianceInvite, self.allianceUid)
end
ClientCityVisitorManager.__init = __init
ClientCityVisitorManager.__delete = __delete
ClientCityVisitorManager.GetAllianceInviteVisitorData = GetAllianceInviteVisitorData
ClientCityVisitorManager.CreateAllianceInviteVisitor = CreateAllianceInviteVisitor
ClientCityVisitorManager.GetTodayCanShowSecond = GetTodayCanShowSecond
ClientCityVisitorManager.SetTodayNoShowSecond = SetTodayNoShowSecond
ClientCityVisitorManager.ShowAllianceInviteVisitor = ShowAllianceInviteVisitor
ClientCityVisitorManager.OpenAllianceInvitePanel = OpenAllianceInvitePanel
return ClientCityVisitorManager
