local FourteenDayExchangeCell = class("FourteenDayExchangeCell", BaseCell)

function FourteenDayExchangeCell:init()
    local uiMap = self:getUIMap()
    self.rewardCell = uiMap["fourteen_day_exchange_cell.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    self.txLimit = uiMap["fourteen_day_exchange_cell.tx_limit"]
    self.cost = uiMap["fourteen_day_exchange_cell.cost"]
    self.imgCost = uiMap["fourteen_day_exchange_cell.cost.img_cost"]
    self.txCost = uiMap["fourteen_day_exchange_cell.cost.tx_num"]
    self.lock = uiMap["fourteen_day_exchange_cell.lock"]
    self.txLock = uiMap["fourteen_day_exchange_cell.lock.tx_lock"]

    self:addClickListener(function()
        if DataManager.FourteenDayData:canExchange(self.id) then
            local cost = DataManager.FourteenDayData:getExchangeCost(self.id)
            local remainCount = DataManager.FourteenDayData:getExchangeRemainTime(self.id)
            local reward = DataManager.FourteenDayData:getExchangeReward(self.id)
            ModuleManager.CommonManager:showExchangeUI(1, remainCount, reward, cost, function(count)
                ModuleManager.FourteenDayManager:reqExchangeReward(self.id, count)
            end)
        end
    end)
end

function FourteenDayExchangeCell:refresh(id)
    self.id = id

    if DataManager.FourteenDayData:isExchangeUnlock(self.id) then
        -- 已解锁
        self.cost:setActive(true)
        self.txLimit:setActive(true)
        self.lock:setActive(false)
        local time = DataManager.FourteenDayData:getExchangeRemainTime(self.id)
        if time > 0 then
            self.txLimit:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, time))
        else
            self.txLimit:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, "<color=#FF6464>"..time.."</color>"))
        end
        local costNum = DataManager.FourteenDayData:getExchangeCostNum(self.id)
        if DataManager.FourteenDayData:getExchangeItemNum() >= costNum then
            self.txCost:setText(costNum)
        else
            self.txCost:setText("<color=#FF6464>"..costNum.."</color>")
        end
        GFunc.centerImgAndTx(self.imgCost, self.txCost)
    else
        -- 未解锁
        self.cost:setActive(false)
        self.txLimit:setActive(false)
        self.lock:setActive(true)
        self.txLock:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_33, DataManager.FourteenDayData:getExchangeUnlockCondition(self.id)))
    end
    
    self.rewardCell:refreshByConfig(DataManager.FourteenDayData:getExchangeReward(self.id))
end

return FourteenDayExchangeCell