local base = UIBaseContainer
local UIMainTopEffect = BaseClass("UIMainTopEffect", base)
local UIMainFlyRewardCell = require("UI.LWMainUI.Component.UIMainTopEffect.UIMainFlyRewardCell")
local UILWAlertGroup = require("UI.LWMainUI.Component.UIMainTopEffect.UILWAlertGroup")
local flyCellPath = "UICommonRewardItem"

function UIMainTopEffect:OnCreate()
  base.OnCreate(self)
  self.flyCell = self.transform:Find(flyCellPath)
  local ok, errorMsg = pcall(function()
    self:ComponentDefine()
    self:DataDefine()
  end)
  if not ok and errorMsg then
    Logger.LogError(errorMsg)
  end
end

function UIMainTopEffect:Stop()
  self.flyRewardList = {}
  if self.flyCo then
    coroutine.stopwaiting(self.flyCo)
  end
  self:RemoveComponents(UIMainFlyRewardCell)
end

function UIMainTopEffect:OnDestroy()
  self:Stop()
  self:ComponentDestroy()
  base.OnDestroy(self)
end

function UIMainTopEffect:OnEnable()
  base.OnEnable(self)
end

function UIMainTopEffect:OnDisable()
  self:Stop()
  base.OnDisable(self)
end

function UIMainTopEffect:OnAddListener()
  base.OnAddListener(self)
  self:AddUIListener(EventId.UIMainFlyReward, self.CheckFlyReward)
  self:AddUIListener(EventId.UIMainStopFlyReward, self.Stop)
end

function UIMainTopEffect:OnRemoveListener()
  base.OnRemoveListener(self)
  self:RemoveUIListener(EventId.UIMainFlyReward, self.CheckFlyReward)
  self:RemoveUIListener(EventId.UIMainStopFlyReward, self.Stop)
end

function UIMainTopEffect:ComponentDefine()
  self.alertGroup = self:AddComponent(UILWAlertGroup, "")
end

function UIMainTopEffect:ComponentDestroy()
  self.alertGroup = nil
end

function UIMainTopEffect:DataDefine()
  self.flyRewardList = {}
end

function UIMainTopEffect:RefreshData()
  self.flyRewardList = DataCenter.RewardManager:GetMainUIReward()
  DataCenter.RewardManager:ClearMainUIReward()
end

local InnerGetFlyTargetByType = function(self, type)
  if type == RewardType.METAL or type == RewardType.FOOD or type == RewardType.WOOD or type == RewardType.OBSIDIAN or type == RewardType.PETROLEUM then
    return self.view:GetResourcePos(RewardToResType[type])
  elseif type == RewardType.WORKER then
    return self.view:GetSavePos(UIMainSavePosType.VisitorBtn)
  elseif type == RewardType.GOLD then
    return self.view:GetSavePos(UIMainSavePosType.Gold)
  elseif type == RewardType.HERO or type == RewardType.EquipStrengtheningStone or type == RewardType.HERO_EXP then
    return self.view:GetSavePos(UIMainSavePosType.HeroBtn)
  elseif type == RewardType.VISITOR then
    return self.view:GetSavePos(UIMainSavePosType.VisitorBtn)
  end
  return self.view:GetSavePos(UIMainSavePosType.BagBtn)
end
local GetDefault = function(param)
  if param.itemId then
    return DataCenter.RewardManager:GetPicByType(param.rewardType, tonumber(param.itemId)), nil
  else
    return DataCenter.RewardManager:GetPicByType(param.rewardType), nil
  end
end
local GetGoods = function(param)
  local itemId = tonumber(param.itemId)
  return DataCenter.RewardManager:GetPicByType(param.rewardType, itemId), nil
end
local GetHero = function(param)
  local heroId = tonumber(param.itemId)
  local icon = HeroUtils.GetHeroIconPath(heroId)
  return icon, "Assets/Main/Sprites/UI/LWCommon/Sprite/tongyong_cfm_daojukuang_5.png"
end
local GetWorker = function(param)
  return DataCenter.RewardManager:GetPicByType(param.rewardType), "Assets/Main/Sprites/UI/LWCommon/Sprite/tongyong_cfm_daojukuang_5.png"
end
local TypeRewardMap = {
  [RewardType.HERO] = GetHero,
  [RewardType.WORKER] = GetWorker,
  [RewardType.GOODS] = GetGoods,
  Default = GetDefault
}
local fly = function(self, cfgList, delay)
  for _, cfg in pairs(cfgList) do
    local pos = cfg[1]
    local p = cfg[2]
    local icon, bg
    if TypeRewardMap[p.rewardType] then
      icon, bg = TypeRewardMap[p.rewardType](p)
    else
      icon, bg = TypeRewardMap.Default(p)
    end
    if p.rewardType ~= RewardType.WORKER then
      local callback
      if p.rewardType == RewardType.HERO_EXP then
        function callback()
          self.view:ShowHeroExpEffect()
        end
      end
      if p.rewardType == RewardType.METAL or p.rewardType == RewardType.FOOD or p.rewardType == RewardType.WOOD or p.rewardType == RewardType.OBSIDIAN or p.rewardType == RewardType.PETROLEUM then
        UIUtil.DoFlyCustom(icon, "", 10, pos, InnerGetFlyTargetByType(self, p.rewardType), nil, nil, callback, nil, nil, nil, delay, self.transform)
      else
        local width, height
        UIUtil.DoFlyCustom(icon, "", 1, pos, InnerGetFlyTargetByType(self, p.rewardType), width, height, callback, nil, nil, nil, delay, self.transform)
      end
    end
  end
end

function UIMainTopEffect:FlyReward()
  if self.flyCo then
    coroutine.stopwaiting(self.flyCo)
  end
  local flyRewards = self.flyRewardList
  self.flyRewardList = {}
  self.flyCo = coroutine.start(function()
    for i, v in ipairs(flyRewards) do
      coroutine.waitforseconds(0.5)
      fly(self, v)
    end
    self.flyCo = nil
  end)
end

function UIMainTopEffect:CheckFlyReward(cfg)
  if self then
    if type(cfg) == "table" and 0 < #cfg then
      table.insert(self.flyRewardList, cfg)
    end
    if self.view.ctrl:IsVisible() then
      self:FlyReward()
    end
  end
end

function UIMainTopEffect:ShowAlarmEffect(alarmType)
  if self.alertGroup then
    self.alertGroup:ShowAlarmEffect(alarmType)
  end
end

return UIMainTopEffect
