local TournArenaGiftCell = class("TournArenaGiftCell", BaseCell)

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

    self.txLimit = uiMap["tourn_arena_gift_cell.tx_limit"]
    self.txBuy = uiMap["tourn_arena_gift_cell.tx_buy"]
    self.imgOff = uiMap["tourn_arena_gift_cell.off_img"]
    self.txOff = uiMap["tourn_arena_gift_cell.off_img.tx_off"]
    self.rewards = {}
    for i = 1, 3 do
        table.insert(self.rewards, uiMap["tourn_arena_gift_cell.reward_cell_"..i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
    end

    self:addClickListener(function()
        if DataManager.TournArenaData:getGiftRemainTime(self.id) > 0 then
            PayManager:purchasePackage(self.id, PayManager.PURCHARSE_TYPE.ACT_GIFT)
        end
    end)
end

function TournArenaGiftCell:refresh(id)
    self.id = id
    local cfg = DataManager.ShopData:getActGiftConfig()[self.id]
    local time = DataManager.TournArenaData:getGiftRemainTime(self.id)

    self.txLimit:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, time))

    for idx, reward in ipairs(self.rewards) do
        if cfg.reward[idx] then
            reward:setVisible(true)
            reward:refreshByConfig(cfg.reward[idx])
        else
            reward:setVisible(false)
        end
    end

    local buyStr
    if cfg.recharge_id then
        self.baseObject:setSprite(GConst.ATLAS_PATH.ACT_COMMON, "act_common_bg_6")
        self.imgOff:setActive(true)
        self.txOff:setText(cfg.value .. "%")
        buyStr = GFunc.getFormatPrice(cfg.recharge_id)
    else
        self.baseObject:setSprite(GConst.ATLAS_PATH.ACT_COMMON, "act_common_bg_5")
        self.imgOff:setActive(false)
        buyStr = I18N:getGlobalText(I18N.GlobalConst.STR_FREE)
    end

    if time <= 0 then
        buyStr = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20)
    end
    self.txBuy:setText(buyStr)
end

return TournArenaGiftCell