local ShopCell = class("ShopCell", BaseCell)

local FREE_BG = "act_common_bg_30"
local COST_BG = "act_common_bg_31"

function ShopCell:refresh(id)
    local uiMap = self:getUIMap()
    local bg = uiMap["shop_cell.bg"]
    local nameTx = uiMap["shop_cell.bg.name_tx"]
    if not self.rewardCell then
        self.rewardCell = CellManager:addCellComp(uiMap["shop_cell.bg.reward_cell"], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    end
    local limitTx = uiMap["shop_cell.bg.limit_tx"] 
    local mask = uiMap["shop_cell.bg.mask"]
    local costImg = uiMap["shop_cell.bg.cost_img"]
    local costTx = uiMap["shop_cell.bg.cost_tx"] 
    local sellOutTx = uiMap["shop_cell.bg.sell_out_tx"]

    sellOutTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_8))

    local reward = DataManager.ActSprintData:getExchangeReward(id)
    local cost = DataManager.ActSprintData:getExchangeCost(id)
    local refreshType = DataManager.ActSprintData:getExchangeRefreshType(id)
    local limit = DataManager.ActSprintData:getExchangeLimit(id)
    local useTimes = DataManager.ActSprintData:getExchangeCount(id)

    local isFree = cost == nil
    local refreshTypeStr
    if refreshType == GConst.ActSprintConst.REFRESH_TYPE.DAY then 
        refreshTypeStr = I18N.GlobalConst.ACT_GOGOGO_DESC_5
    elseif refreshType == GConst.ActSprintConst.REFRESH_TYPE.WEEK then
        refreshTypeStr = I18N.GlobalConst.ACT_GOGOGO_DESC_6
    elseif refreshType == GConst.ActSprintConst.REFRESH_TYPE.ONCE then
        refreshTypeStr = I18N.GlobalConst.ACT_GOGOGO_DESC_7
    end
    local canBuy = useTimes < limit

    bg:setSprite(GConst.ATLAS_PATH.UI_ACT_COMMON, isFree and FREE_BG or COST_BG)
    nameTx:setText()
    self.rewardCell:refreshByConfig(reward)
    limitTx:setText(I18N:getGlobalText(refreshTypeStr, limit - useTimes, limit))
    
    if canBuy then
        if cost then
            costImg:setVisible(true)
            costImg:setSprite(GFunc.getIconRes(cost.id))
            costTx:setVisible(true)
            costTx:setText(cost.num)
            GFunc.centerImgAndTx(costImg, costTx, 5)
        else
            costImg:setVisible(false)
            costTx:setVisible(true)
            costTx:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
            costTx:setSizeDeltaX(160)
            costTx:setAnchoredPositionX(0)
        end
        mask:setVisible(false)
    else
        costImg:setVisible(false)
        costTx:setVisible(false)

        mask:setVisible(true)
    end
    
    self:addClickListener(function()
        if canBuy then
            if cost then
                if GFunc.checkCost(cost.id, cost.num, true) then
                    ModuleManager.ActSprintManager:reqExchange(id, 1)
                end
            else
                ModuleManager.ActSprintManager:reqExchange(id, 1)
            end
        end
    end)
end

return ShopCell