local ArenaGiftSellCell = class("ArenaGiftSellCell", BaseCell)
local MAX_REWARD_COUNT = 4
local BASE_CELL_HEIGHT = 320

local BG = {
    [90102] = "assets/arts/textures/background/shop/arena_gift_banner_1.png",
    [90202] = "assets/arts/textures/background/shop/arena_gift_banner_2.png",
    [90302] = "assets/arts/textures/background/shop/arena_gift_banner_3.png",
    [90402] = "assets/arts/textures/background/shop/arena_gift_banner_4.png",
}

function ArenaGiftSellCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["gift_cell.bg"]
    if BG[DataManager.ArenaData:getGiftId()] then
        self.bg:setTexture(BG[DataManager.ArenaData:getGiftId()])
    end

    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 ArenaGiftSellCell:refresh()
    local actGiftId = DataManager.ArenaData:getGiftId()
    local cfgInfo = ConfigManager:getConfig("act_gift")[actGiftId]
    -- 超值
    if cfgInfo.value then
        self.offImg:setVisible(true)
        self.offText:setText(tostring(cfgInfo.value) .. "%")
    else
        self.offImg:setVisible(false)
    end
    -- 限购
    self.limitText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, 1)) -- 限购1次
    -- 标题
    self.titleText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_40)) -- 竞技场特惠
    -- 奖励
    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(actGiftId)
    end)
end

function ArenaGiftSellCell:getCellHeight()
	return BASE_CELL_HEIGHT -- 竞技场固定1个
end

function ArenaGiftSellCell:getIsOpen()
    return DataManager.ArenaData:getGiftId() ~= nil
end

function ArenaGiftSellCell:refreshTime()
    local remainTime = DataManager.ArenaData:getGiftRemainTime()
    self.timeText:setText(Time:formatNumTime(remainTime))
end

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

function ArenaGiftSellCell:onClickGift(id)
    -- PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.ACT_GIFT)
    ModuleManager.ArenaManager:showGiftPopUI(BIReport.PAY_UI_SHOW_TYPE.CLICK_SHOW)
end

return ArenaGiftSellCell