local ArmorSellCell = class("ArmorSellCell", BaseCell)
local MAX_REWARD_COUNT = 4
local BASE_CELL_HEIGHT = 320
local GIFT_TYPE = PayManager.PURCHARSE_ACT_TYPE.ARMOR_GIFT

function ArmorSellCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["gift_cell.bg"]
    self.bg:setTexture("assets/arts/textures/background/shop/shop_gift_banner_7_1.png")

    self.leftArrow = uiMap["gift_cell.left_arrow"]
    self.rightArrow = uiMap["gift_cell.right_arrow"]

    self.offImg = uiMap["gift_cell.bg.off_img"]
    self.offText = uiMap["gift_cell.bg.off_img.text"]

    self.limitImg = uiMap["gift_cell.bg.limit_img"]
    self.limitText = uiMap["gift_cell.bg.limit_img.text"]

    self.titleText = uiMap["gift_cell.bg.title_text"]

    self.reweardLayout = uiMap["gift_cell.bg.reward_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT) -- RefreshLayout()
    if not self.rewardCellList then
        self.rewardCellList = {}
        for i = 1, MAX_REWARD_COUNT do
            self.rewardCellList[i] = CellManager:addCellComp(uiMap["gift_cell.bg.reward_node.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.GIFT_REWARD_CELL)
        end
    end
    self.priceText = uiMap["gift_cell.bg.price"]
    self.timeImg = uiMap["gift_cell.bg.time_img"]
    self.timeText = uiMap["gift_cell.bg.time_img.text"]
    self.clickArea = uiMap["gift_cell.bg.click_area"]
end

function ArmorSellCell:refresh()
    local actGiftId = DataManager.ShopData:getGift(GIFT_TYPE).id
    if actGiftId and actGiftId > 0 then
        self.actGiftId = actGiftId
        local cfgInfo = ConfigManager:getConfig("act_gift")[self.actGiftId]
        -- 超值
        self.offText:setText(tostring(cfgInfo.value) .. "%")
        -- 限购
        self.limitText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, 1)) -- 限购1次
        -- 标题
        self.titleText:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_25))
        -- 奖励
        local rewardList = cfgInfo.reward
        local count = rewardList and #rewardList or 0
        for i = 1, MAX_REWARD_COUNT do
            if i <= count then
                self.rewardCellList[i]:getBaseObject():setActive(true)
                self.rewardCellList[i]:refreshByConfig(rewardList[i])
            else
                self.rewardCellList[i]:getBaseObject():setActive(false)
            end
        end
        self.reweardLayout:RefreshLayout()
        -- 价格
        self.priceText:setText(GFunc.getFormatPrice(cfgInfo.recharge_id))
        -- 限时
        self.timeImg:setVisible(true)
        self:refreshTime()

        self.clickArea:addClickListener(function()
            self:onClickGift(self.actGiftId)
        end)
    else -- 倒计时结束的情况
        if self.actGiftId then
            DataManager.ShopData:setDirty()
            self.actGiftId = nil
        end
    end
end

function ArmorSellCell:getCellHeight()
	return BASE_CELL_HEIGHT -- 固定1个
end

function ArmorSellCell:getIsOpen()
    return DataManager.ShopData:getGift(GIFT_TYPE) ~= nil 
end

function ArmorSellCell:setVisible(visible)
	self.baseObject:setVisible(visible)
end

function ArmorSellCell:onClickGift(id)
    PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.ACT_GIFT)
end

function ArmorSellCell:refreshTime()
    local remainTime = 0
    if self.actGiftId and self.actGiftId > 0 then
        remainTime = DataManager.ShopData:getGiftRemainTime(GIFT_TYPE)
        if remainTime <= 0 then
            remainTime = 0
        end
    end
    self.timeText:setText(Time:formatNumTime(remainTime))
end

function ArmorSellCell:getCellCount()
    if self.actGiftId and self.actGiftId > 0 then
        return 1
    else
        return 0
    end
end

return ArmorSellCell