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

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

function IntroductPopUI:isFullScreen()
    return false
end

function IntroductPopUI:getPrefabPath()
    return "assets/prefabs/ui/shop/introduct_pop_ui.prefab"
end

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

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

	self.titleTx = self.uiMap["introduct_pop_ui.bg.title_bg.title"]
    self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_38)) -- 入门礼包

    self.rewardCellList = {}
    for i = 1, MAX_ITEM_NUM do
        table.insert(self.rewardCellList, CellManager:addCellComp(self.uiMap["introduct_pop_ui.bg.pop_reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.POP_REWARD_CELL))
    end
    
    self.descText = self.uiMap["introduct_pop_ui.bg.desc_text"]
    self.descText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_39)) -- 超值S级英雄,仅此一次!

    self.buyBtn = self.uiMap["introduct_pop_ui.bg.buy_btn"]
    self.buyText = self.uiMap["introduct_pop_ui.bg.buy_btn.text"]

	self.buyBtn:addClickListener(function()
		self:onClickFuncBtn()
	end)
    local cfgInfo = PayManager:getGiftConfigInfo(PayManager.PURCHARSE_TYPE.ACT_GIFT, GConst.ShopConst.INTRODUCT_GIFT_ID)
    local rechargeId = cfgInfo.recharge_id
    self.buyText:setText(GFunc.getFormatPrice(rechargeId))

    self.timeText = self.uiMap["introduct_pop_ui.bg.time_node.text"]

    self:_bind()
    self:scheduleGlobal(function()
        self:updateTime()
	end, 1)
	self:refresh()

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

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

function IntroductPopUI:refresh()
    if DataManager.ShopData:getIntroductGiftRemainBuyCount() <= 0 or DataManager.ShopData:getIntroductGiftRemainTime() <= 0 then
        self:closeUI()
    end

    local cfgInfo = PayManager:getGiftConfigInfo(PayManager.PURCHARSE_TYPE.ACT_GIFT, GConst.ShopConst.INTRODUCT_GIFT_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
    self:updateTime()
end

function IntroductPopUI:updateTime()
    local remainTime = DataManager.ShopData:getIntroductGiftRemainTime()
    if remainTime > 0 then
        self.timeText:setText(Time:formatNumTime(remainTime))
    else
        self:closeUI()
    end
end

function IntroductPopUI:onClickFuncBtn()
    PayManager:purchasePackage(GConst.ShopConst.INTRODUCT_GIFT_ID, PayManager.PURCHARSE_TYPE.ACT_GIFT)
end

return IntroductPopUI