local FirstRechargePopUI = class("FirstRechargePopUI", BaseUI)
local MAX_ITEM_NUM = 3

function FirstRechargePopUI:ctor(params)
    self.showType = params.showType
end

function FirstRechargePopUI:isFullScreen()
    return false
end

function FirstRechargePopUI:getPrefabPath()
    return "assets/prefabs/ui/shop/first_recharge_pop_ui.prefab"
end

function FirstRechargePopUI:onPressBackspace()
	self:closeUI()
end

function FirstRechargePopUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()
	self.uiMap["first_recharge_pop_ui.close_btn"]:addClickListener(function()
        self:closeUI()
	end)

	self.titleTx = self.uiMap["first_recharge_pop_ui.bg.title_bg.title"]
    self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_34)) -- 首充豪礼

    self.rewardCellList = {}
    for i = 1, MAX_ITEM_NUM do
        table.insert(self.rewardCellList, CellManager:addCellComp(self.uiMap["first_recharge_pop_ui.bg.pop_reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.POP_REWARD_CELL))
    end
    
    self.descText = self.uiMap["first_recharge_pop_ui.bg.desc_text"]
    self.descText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_35)) -- 充值购买任意商品,即可获得如下奖励,仅此一次！

	self.funcBtn = self.uiMap["first_recharge_pop_ui.bg.func_btn"]
	self.btnJumpText = self.uiMap["first_recharge_pop_ui.bg.func_btn.jump_text"]
    self.btnGetText = self.uiMap["first_recharge_pop_ui.bg.func_btn.get_text"]
	self.btnIcon = self.uiMap["first_recharge_pop_ui.bg.func_btn.icon"]

    self.btnJumpText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_36)) -- 商店
    self.btnGetText:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM)) -- 领取

	self.funcBtn:addClickListener(function()
		self:onClickFuncBtn()
	end)

    self:_bind()
	self:refresh()

    -- 上报
    BIReport:postPayUIShow(BIReport.GIFT_TYPE.FIRST_RECHARGE, GConst.ShopConst.FIRST_RECHARGE_ID, self.showType)
end

function FirstRechargePopUI:_bind()
    self:bind(DataManager.ShopData, "isDirty", function()
        self:refresh()
    end)
end

function FirstRechargePopUI:refresh()
    local cfgInfo = PayManager:getGiftConfigInfo(PayManager.PURCHARSE_TYPE.ACT_GIFT, GConst.ShopConst.FIRST_RECHARGE_ID)
	local rewards = cfgInfo.reward or {}
	for i = 1, MAX_ITEM_NUM do
        if i <= #rewards then
            self.rewardCellList[i]:setVisible(true, 0.8)
            self.rewardCellList[i]:refresh(rewards[i].id, rewards[i].num, true)
        else
            self.rewardCellList[i]:setVisible(false)
        end
	end

    -- 对于首充礼包,有跳转商店和领取2个分支
    local canGet = DataManager.ShopData:getHasFirstRechargeReward()
    if canGet then
        self.btnIcon:setVisible(false)
        self.btnJumpText:setVisible(false)
        self.btnGetText:setVisible(true)
    else
        local hasGet = DataManager.ShopData:getHasGotFirstRechargeReward()
        if hasGet then
            self:closeUI()
        else
            self.btnIcon:setVisible(true)
            self.btnJumpText:setVisible(true)
            self.btnGetText:setVisible(false)
        end
    end
end

function FirstRechargePopUI:onClickFuncBtn()
    -- 对于首充礼包,有跳转商店和领取2个分支
    local canGet = DataManager.ShopData:getHasFirstRechargeReward()
    if canGet then
        PayManager:purchasePackage(GConst.ShopConst.FIRST_RECHARGE_ID, PayManager.PURCHARSE_TYPE.ACT_GIFT)
    else
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.GO_SHOP, {page = GConst.ShopConst.PAGE_TYPE.DISCOUNT})
        self:closeUI()
    end
end

return FirstRechargePopUI