local ShopCell = class("ShopCell", BaseCell)

local BTN_ICON = {"common_btn_green_2", "common_btn_grey_2"}

function ShopCell:init()
    local uiMap = self:getUIMap()
    self.scoreIcon = uiMap["shop_cell.bg.score"]
    self.txScore = uiMap["shop_cell.score.tx_score"]
    self.rewardCell = uiMap["shop_cell.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    self.txBtn = uiMap["shop_cell.ok_btn.text"]
    self.txLimit = uiMap["shop_cell.tx_limit"]
    self.btn = uiMap["shop_cell.ok_btn"]
end

function ShopCell:refresh(id)
    self.txBtn:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_18))

    local cost = DataManager.ActBossRushData:getShopCost(id)
    local costId = GFunc.getRewardId(cost)
    local costNum = GFunc.getRewardNum(cost)
    self.scoreIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
    local color = "#FFFFFF"
    local itemEnough = GFunc.checkCost(costId, costNum, false)
    if not itemEnough then
        -- color = "#FF4F38"
    end
    local str = string.format("<color=%s>%s</color>", color, costNum)
    self.txScore:setText(str)

    local remainCount = DataManager.ActBossRushData:getExchangeRemainCount(id)
    local reward = DataManager.ActBossRushData:getShopReward(id)
    if reward then
        self.rewardCell:refreshByConfig(reward)
    end

    local canExchange = remainCount > 0
    local color = "#FFFFFF"
    if not canExchange then
        -- color = "#FF4F38"
    end
    local str = string.format("<color=%s>%s</color>", color, I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_19, remainCount))
    self.txLimit:setText(str)

    self.btn:addClickListener(function()
        self:onClickReward(id)
    end)

    if not itemEnough then
        canExchange = false
    end
    local icon = canExchange and BTN_ICON[1] or BTN_ICON[2]
    self.btn:setSprite(GConst.ATLAS_PATH.COMMON, icon)
    self.btn:setTouchEnable(canExchange)
end

-- 领取奖励
function ShopCell:onClickReward(id)
    local cost = DataManager.ActBossRushData:getShopCost(id)
    local remainCount = DataManager.ActBossRushData:getExchangeRemainCount(id)
    local reward = DataManager.ActBossRushData:getShopReward(id)
    ModuleManager.CommonManager:showExchangeUI(1, remainCount, reward, cost, function(count)
        ModuleManager.ActBossRushManager:reqExchange(id, count)
    end)
end

return ShopCell