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

-- 目前礼包样式一致，章节/新手/助力/成长/金币礼包都通用

function ChapterCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["gift_cell.bg"]
    self.bg:setTexture("assets/arts/textures/background/shop/shop_gift_banner_2.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 ChapterCell:refresh(idx, cfgInfo, totalCount, arrowCallback)
    -- 箭头
    local showLeftArrow = true
    local showRightArrow = true
    if idx == 1 then
        showLeftArrow = false
    end
    if idx == totalCount then
        showRightArrow = false
    end
    self.leftArrow:setVisible(showLeftArrow)
    self.rightArrow:setVisible(showRightArrow)

	self.leftArrow:addClickListener(function()
        if idx > 1 then
            arrowCallback(1)
        end
    end)
    self.rightArrow:addClickListener(function()
        if idx < totalCount then
            arrowCallback(-1)
        end
    end)
    -- 超值
    self.offText:setText(tostring(cfgInfo.value * 100) .. "%")
    -- 限购
    self.limitText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, 1)) -- 限购1次
    -- 标题
    self.titleText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_19, tostring(cfgInfo.chapter))) -- 第{0}章礼包
    -- 奖励
    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(false)

    self.clickArea:addClickListener(function()
        self:onClickGift(cfgInfo.id)
    end)
end

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

function ChapterCell:onClickGift(id)
    PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.CHAPTER_GIFT)
end

return ChapterCell