local GrowCell = class("GrowCell", BaseCell)
local MAX_REWARD_COUNT = 4

function GrowCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["gift_cell.bg"]
    self.bg:setTexture("assets/arts/textures/background/shop/shop_gift_banner_5.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

    -- 不限时时使用1类型 否则使用2类型
    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 GrowCell:refresh(idx, gift)
    self.gift = gift
    self.actId = gift.current_grow_up_id
    local cfgInfo = DataManager.ShopData:getActGrowUpGiftConfig()[self.actId]
    -- 超值
    self.offText:setText(tostring(cfgInfo.value) .. "%")
    -- 限购
    if cfgInfo.limit then
        self.limitImg:setVisible(true)
        self.limitText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, tostring(cfgInfo.limit))) -- 限购{0}次
    else
        self.limitImg:setVisible(false)
    end
    -- 标题
    self.titleText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_13)) -- 成长礼包
    -- 奖励
    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.actId)
    end)
end

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

function GrowCell:refreshTime()
    local remainTime = DataManager.ShopData:getGrowUpGiftRemainTime()
    if remainTime <= 0 then
        remainTime = 0
    end
    self.timeText:setText(Time:formatNumTime(remainTime))
end

function GrowCell:onClickGift(id)
    PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.GROW_UP_GIFT_NEW)
end

return GrowCell