local LWSticker3DManager = BaseClass("LWSticker3DManager")
local stickerPerfabPatch = "Assets/Main/Prefabs/UI/UIDecoration/mapStickerItem.prefab"
local MarchSticker = require("Scene.LWWorldMarch.MarchSticker")
local DisplaySettings = require("DataCenter.WorldBattle.WorldBattleDisplaySettings")
local Localization = CS.GameEntry.Localization
local Resource = CS.GameEntry.Resource
local sendCd = 10

function LWSticker3DManager:__init()
  self.curStageId = 0
  self.lodLevel = 1
  self.stickerList = {}
  self:AddListener()
end

function LWSticker3DManager:__delete()
  self.curStageId = nil
  self.lodLevel = nil
  for i, sticker in pairs(self.stickerList) do
    if sticker then
      sticker:Delete()
    end
  end
  self.stickerList = nil
  self.sendStickerTim = nil
  self:RemoveListener()
end

function LWSticker3DManager:Startup()
end

function LWSticker3DManager:AddListener()
  EventManager:GetInstance():AddListener(EventId.WORLD_BUILD_OUT_VIEW, self.OnWorldBuildOutView)
  EventManager:GetInstance():AddListener(EventId.ChangeCameraLod, self.OnLodChange)
end

function LWSticker3DManager:RemoveListener()
  EventManager:GetInstance():RemoveListener(EventId.ChangeCameraLod, self.OnLodChange)
  EventManager:GetInstance():RemoveListener(EventId.WORLD_BUILD_OUT_VIEW, self.OnWorldBuildOutView)
end

function LWSticker3DManager.OnLodChange(lod)
  local mgr = DataCenter.LWSticker3DManager
  mgr.lodLevel = lod
end

function LWSticker3DManager:ShowSticker(type, stickerId, uuid, numParam)
  local buildData = DataCenter.BuildManager:GetFunbuildByItemID(BuildingTypes.FUN_BUILD_MAIN)
  if not DisplaySettings.ShowWorldSticker() and buildData and buildData.uid ~= uuid then
    return
  end
  if IsNull(CS.SceneManager.World) or CS.SceneManager:IsInCity() then
    return
  end
  if self.lodLevel > 2 then
    return
  end
  if type == WorldStickerType.March then
    self:ShowMarchSticker(stickerId, uuid, numParam)
  elseif type == WorldStickerType.Build then
    self:ShowCitySticker(stickerId, uuid, numParam)
  end
end

function LWSticker3DManager:SendMapSticker(type, uid, stickerId)
  local curCd = sendCd + 1
  if self.sendStickerTime then
    curCd = (UITimeManager:GetInstance():GetServerTime() - self.sendStickerTime) / 1000
  end
  if curCd >= sendCd then
    self.sendStickerTime = UITimeManager:GetInstance():GetServerTime()
    local numParam
    if stickerId == 5 then
      numParam = math.random(1, 6)
    end
    SFSNetwork.SendMessage(MsgDefines.MapSticker, type, uid, stickerId, LuaEntry.Player:GetCurWorldId(), LuaEntry.Player:GetCrossServerId(), numParam)
    return true
  else
    local time = math.ceil(sendCd - curCd)
    UIUtil.ShowTips(Localization:GetString("map_sticker_limit_tips", time))
    return false
  end
end

function LWSticker3DManager:CreateSticker(parent, bUuid, stickerId, numParam)
  local req = Resource:InstantiateAsync(stickerPerfabPatch)
  local marchSticker = ObjectPool:GetInstance():Load(MarchSticker)
  marchSticker:Init(parent, req)
  req:completed("+", function()
    if req.isError then
      return
    end
    if self.props == nil then
      self.props = CS.UnityEngine.MaterialPropertyBlock()
    end
    marchSticker:OnCreate(Vector3.New(0, 2.3, 0), self.props)
    marchSticker:ShowSticker(stickerId, bUuid, numParam)
  end)
  self.stickerList[bUuid] = marchSticker
end

function LWSticker3DManager:ShowMarchSticker(stickerId, marchUuid, numParam)
  local squad = DataCenter.WorldBattleManager:GetSquad(marchUuid)
  if squad then
    squad:ShowSticker(stickerId, marchUuid, numParam)
  end
end

function LWSticker3DManager.OnWorldBuildOutView(bUuid)
  local self = DataCenter.LWSticker3DManager
  local sticker = self.stickerList[bUuid]
  if not sticker then
    return
  end
  if sticker then
    sticker:Delete()
    self.stickerList[bUuid] = nil
  end
end

function LWSticker3DManager:ShowCitySticker(stickerId, bUuid, numParam)
  local build = CS.SceneManager.World:GetObjectByUuid(bUuid)
  if build then
    local sticker = self.stickerList[bUuid]
    if not sticker then
      local cityModel = build:GetGameObject()
      self:CreateSticker(cityModel, bUuid, stickerId, numParam)
    else
      self.stickerList[bUuid]:ShowSticker(stickerId, bUuid, numParam)
    end
  end
end

return LWSticker3DManager
