local ArenaGradingRewardCell = class("ArenaGradingRewardCell", BaseCell)

-- 光效
local EFFECT_LIGHT = "assets/prefabs/effects/ui/vfx_ui_arena_gift_b01.prefab"

local COMMON_GRADING_POSY = 265
local UNLOCK_GRADING_POSY = 375

function ArenaGradingRewardCell:init()
    local uiMap = self:getUIMap()

    -- 段位spine
    self.gradingNode = uiMap["arena_grading_reward_cell.grading"]
    self.spineGradingRoot = uiMap["arena_grading_reward_cell.grading.spine_root"]
    self.txGrading = uiMap["arena_grading_reward_cell.grading.tx_grading"]
    -- 进度
    self.progress = uiMap["arena_grading_reward_cell.progress"]
    self.imgProgCur = uiMap["arena_grading_reward_cell.progress.img_prog_cur"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.imgProgMax = uiMap["arena_grading_reward_cell.progress.img_prog_max"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.txGradingValue = uiMap["arena_grading_reward_cell.progress.tx_value"]
    -- 游标
    self.tagProg = uiMap["arena_grading_reward_cell.cur_prog"]
    self.txTag = uiMap["arena_grading_reward_cell.cur_prog.tx_tag"]
    -- 通用奖励
    self.commonReward = uiMap["arena_grading_reward_cell.common_reward"]
    self.txDesc = uiMap["arena_grading_reward_cell.common_reward.tx_desc"]
    self.maskReward = uiMap["arena_grading_reward_cell.common_reward.mask"]
    self.rootEffect = uiMap["arena_grading_reward_cell.common_reward.root_effect"]
    self.rewards = {}
    for i = 1, 4 do
        table.insert(self.rewards, uiMap["arena_grading_reward_cell.common_reward.rewards.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
    end
    -- 英雄解锁
    self.heroUnlock = uiMap["arena_grading_reward_cell.hero_unlock"]
    self.txUnlock = uiMap["arena_grading_reward_cell.hero_unlock.tx_unlock"]
    self.heroList = uiMap["arena_grading_reward_cell.hero_unlock.hero_list"]
    self.heros = {}
    for i = 1, 6 do
        table.insert(self.heros, uiMap["arena_grading_reward_cell.hero_unlock.hero_list.item_hero_" .. i])
    end

    self.commonReward:addClickListener(function()
        if DataManager.ArenaData:canGetGradingReward(self.id) and not DataManager.ArenaData:isReceivedGradingReward(self.id) then
            ModuleManager.ArenaManager:reqGradingReward(self.id)
        end
	end)
end

function ArenaGradingRewardCell:setUI(root)
    self.uiRoot = root
end

function ArenaGradingRewardCell:refresh(id)
    self.id = id
    local cfg = DataManager.ArenaData:getGradingRewardCfg()[self.id]
    local curPoint = DataManager.ArenaData:getScore()
    local maxPoint = DataManager.ArenaData:getMaxScore()
    local curProg = DataManager.ArenaData:getGradingRewardProgressByScore(self.id, curPoint)
    local maxProg = DataManager.ArenaData:getGradingRewardProgressByScore(self.id, maxPoint)

    -- Logger.logHighlight(id.."当前进度:"..cfg.score.."/" .. curProg .."/"..maxProg)

    self.spineGradingRoot:removeAllChildren()
    local gradingId = DataManager.ArenaData:getGradingIdFromScore(cfg.score)
    SpineManager:loadUISpineWidgetAsync(DataManager.ArenaData:getGradingIconName(gradingId).."_spine", self.spineGradingRoot, function(spineObject)
        spineObject:setSkin(DataManager.ArenaData:getGradingNumName(gradingId))
        spineObject:playAnim("idle", true)
    end)
    self.txGrading:setText(DataManager.ArenaData:getGradingName(gradingId))

    self.baseObject:setSizeDeltaY(DataManager.ArenaData:getGradingRewardItemHeight(self.id))
    if cfg.unlock_hero and #cfg.unlock_hero > 0 then
        -- 解锁英雄
        self.gradingNode:setActive(true)
        self.gradingNode:setAnchoredPositionY(UNLOCK_GRADING_POSY)
        self.heroUnlock:setActive(true)

        if maxPoint >= cfg.score then
            self.txUnlock:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_38))
        else
            self.txUnlock:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_37))
        end

        for idx, hero in ipairs(self.heros) do
            if cfg.unlock_hero[idx] then
                hero:setActive(true)
                local heroInfo = ConfigManager:getConfig("hero")[cfg.unlock_hero[idx]]
                hero:setSprite(GConst.ATLAS_PATH.ICON_ITEM, "frame_"..heroInfo.qlt)
                local map = hero:genAllChildren()
                map["img_hero"]:setSprite(GConst.ATLAS_PATH.ICON_HERO, heroInfo.icon)

                hero:addClickListener(function()
                    ModuleManager.HeroManager:showHeroDetailUI(cfg.unlock_hero[idx], true)
                end)
            else
                hero:setActive(false)
            end
        end
    else
        if DataManager.ArenaData:isGradingMin(self.id) then
            self.gradingNode:setActive(true)
            self.gradingNode:setAnchoredPositionY(COMMON_GRADING_POSY)
        else
            self.gradingNode:setActive(false)
        end
        self.heroUnlock:setActive(false)
    end

    local minScore = DataManager.ArenaData:getSeasonGradingMinScore()
    self.txTag:setText(curPoint - minScore)
    self.txGradingValue:setText(cfg.score - minScore)
    self.imgProgCur.value = curProg
    self.imgProgMax.value = maxProg

    if DataManager.ArenaData:getCurGradingRewardId() == self.id then
        self.tagProg:setVisible(true)
        local rootHeight = self.baseObject:fastGetSizeDeltaY()
        local posY = rootHeight * curProg

        -- 高度超框处理
        if self.id == 1 or self.id == #DataManager.ArenaData:getGradingRewardCfg() then
            local tagHeight = self.tagProg:fastGetSizeDeltaY()
            if posY < tagHeight / 2  then
                posY = tagHeight / 2
            end
        end
        self.tagProg:setAnchoredPositionY(posY)
        self.baseObject:getTransform():SetAsLastSibling()
    else
        self.tagProg:setVisible(false)
    end

    local canGet = DataManager.ArenaData:canGetGradingReward(self.id)
    local isReceived = DataManager.ArenaData:isReceivedGradingReward(self.id)
    self.maskReward:setVisible(not canGet)
    if canGet then
        if isReceived then
            self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_5))
        else
            self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_42))
        end
    else
        self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_6))
    end

    self.rootEffect:removeAllChildren()
    if canGet and not isReceived then
        EffectManager:loadUIEffectAsync(EFFECT_LIGHT, self.uiRoot, self.rootEffect, GConst.UI_EFFECT_ORDER.LEVEL5, function(obj)
            obj:setAnchoredPosition(0, 0)
            obj:play()
        end)
    end

    for idx, reward in ipairs(self.rewards) do
        if cfg.reward[idx] then
            reward:addClickListener(nil)
            reward:setVisible(true)
            reward:refreshByConfig(cfg.reward[idx], isReceived, isReceived)
            reward:showLight(canGet and not isReceived, self.uiRoot)
            if canGet and not isReceived then
                reward:addClickListener(function()
                    if DataManager.ArenaData:canGetGradingReward(self.id) and not DataManager.ArenaData:isReceivedGradingReward(self.id) then
                        ModuleManager.ArenaManager:reqGradingReward(self.id)
                    end
                end)
            end
        else
            reward:setVisible(false)
        end
    end
end

return ArenaGradingRewardCell