local ArmorFundRewardCell = class("ArmorFundRewardCell", BaseCell)
local FUND_AD_REWARD_ID = 1

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

    self.txTitle = uiMap["armor_fund_cell.tx_title"]
    self.txBtn = uiMap["armor_fund_cell.btn_get.tx_btn"]
    self.imgAd = uiMap["armor_fund_cell.btn_get.img_ad"]
    self.btnGet = uiMap["armor_fund_cell.btn_get"]
    self.rewardsCells = {}
    for i = 1, 3 do
        self.rewardsCells[i] = uiMap["armor_fund_cell.rewards.reward_cell_"..i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    end

    self.btnGet:addClickListener(function()
        -- ad奖励处理
        if self.rewardId == FUND_AD_REWARD_ID and self.armorEntity:isCanWatchFundAd() then
            SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.ARMOR_FUND_AD, function ()
                ModuleManager.DungeonArmorManager:reqFundReward(self.rewardId)
            end)
            return
        end

        if self.rewardId ~= FUND_AD_REWARD_ID and not self.armorEntity:isBoughtFundStage(self.armorEntity:getCurFundStage()) then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_15))
            return
        end

        ModuleManager.DungeonArmorManager:reqFundReward(self.rewardId)
	end)
end

function ArmorFundRewardCell:refresh(id)
    self.rewardId = id
    self.armorEntity = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR)
    local data = ConfigManager:getConfig("dungeon_armor_fund")[id]
    if not data then
        return
    end
    if data.reward then
        local rewards = data.reward
        for idx, cell in pairs(self.rewardsCells) do
            if rewards[idx] then
                cell:setVisible(true)
                cell:refreshByConfig(rewards[idx])
            else
                cell:setVisible(false)
            end
        end
    end

    local levelCfg = ConfigManager:getConfig("chapter_dungeon_armor")[data.dungeon_stage]
    if levelCfg then
        self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_14, levelCfg.chapter, levelCfg.stage))
    else
        self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_13))
    end

    self.imgAd:setActive(false)
    self.btnGet:removeRedPoint()
    if not self.armorEntity:isMeetFundReward(self.rewardId) or self.armorEntity:isGotFundReward(self.rewardId) then
        -- 不满足条件 or 已领取
        self.txBtn:setActive(true)
        if self.armorEntity:isGotFundReward(self.rewardId) then
            self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DONE))
        else
            self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
        end
        self.btnGet:setTouchEnable(false)
        self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
        self.baseObject:setSprite(GConst.ATLAS_PATH.UI_DUGEON_ARMOR, "dungeon_armor_bg_1")
        -- 广告奖励
        if self.rewardId == 1 then
            self.txBtn:setActive(false)
            self.imgAd:setActive(true)
            self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_2")
            self.btnGet:setTouchEnable(true)
            self.btnGet:addRedPoint(50, 30, 0.6)
        end
    else
        -- 满足奖励条件 and 未领取
        self.txBtn:setActive(true)
        self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
        self.btnGet:setTouchEnable(true)
        self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_2")
        self.baseObject:setSprite(GConst.ATLAS_PATH.UI_DUGEON_ARMOR, "dungeon_armor_bg_2")
    end
end

return ArmorFundRewardCell