local UIPrefabObject = require "app/bf/unity/uiprefab_object"
local SummonRewardCell = class("SummonRewardCell", BaseCell)

function SummonRewardCell:init()
    local uiMap = self:getUIMap()
    self.animator = self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.ANIMATOR)
    self.imgIcon = uiMap["summon_reward_cell.bg.img_icon"]
    self.txName = uiMap["summon_reward_cell.bg.tx_name"]
    self.txCount = uiMap["summon_reward_cell.bg.tx_count"]
    self.bg = uiMap["summon_reward_cell.bg"]
    self.effectQlts = {}
    self.effectQlts[2] = uiMap["summon_reward_cell.bg.vfx_b13_chouka_lan_b01"]
    self.effectQlts[3] = uiMap["summon_reward_cell.bg.vfx_b13_chouka_zi_b01"]
    self.effectQlts[4] = uiMap["summon_reward_cell.bg.vfx_b13_chouka_cheng_b01"]
end
function SummonRewardCell:getEffectQlts()
    return self.effectQlts
end
function SummonRewardCell:refresh(reward, index, parentUI)
    self.patentUI = parentUI
    if self.effectQlts[5] then
        self.effectQlts[5]:setActive(false)
    end
    self.effectQlts[4]:setActive(false)
    self.effectQlts[3]:setActive(false)
    self.effectQlts[2]:setActive(false)
    local cfg = DataManager.HeroData:getHeroConfig(reward.item.id)
    self.imgIcon:setSprite(GConst.ATLAS_PATH.ICON_HERO_SUMMON, tostring(cfg.icon))
    self.bg:setSprite(GConst.ATLAS_PATH.UI_SUMMON, "summon_card_" .. cfg.qlt)
    self.txName:setText(ModuleManager.HeroManager:getHeroName(reward.item.id))
    self.txCount:setText("x" .. reward.item.count)
    self.animator:SetTrigger("t_open")
	self.baseObject:performWithDelayGlobal(function()
        self:playEffect(cfg.qlt)
    end, 0.3)
end
function SummonRewardCell:playEffect(qlt)
    if qlt == 5 and self.effectQlts[5] == nil then
        local tranInfo = self.imgIcon:getComponent(GConst.TYPEOF_UNITY_CLASS.TRANSFORM).parent
        if tranInfo then
            local infoBase = UIPrefabObject:create()
            infoBase:initWithPrefab(GConst.EMPTY_STRING, tranInfo)
            infoBase:initPrefabHelper()
            EffectManager:loadUIEffectAsync("assets/prefabs/effects/ui/vfx_b13_chouka_hong_b01.prefab", self.patentUI, infoBase, 0, 
            function(obj)
                if self.effectQlts[5] then
                    self.effectQlts[5]:destroy()
                    self.effectQlts[5] = nil
                end
                self.effectQlts[5] = obj
            end)
        end
    else
        local effect = self.effectQlts[qlt]
        if effect then
            effect:setActive(true)
            effect:play()
        end
    end
    
    -- effect:performDurationDelay(function()
    --     effect:setActive(false)
    -- end)
end

return SummonRewardCell
