local GiftPopUI = class("GiftPopUI", BaseUI)

local MAX_ITEM_NUM = 4
local MAX_GIFT_ID = 90102
local SPINE_NAME = {
    [90102] = {"ui_gift_arena_1_born", "ui_gift_arena_1_idle"},
    [90202] = {"ui_gift_arena_2_born", "ui_gift_arena_2_idle"},
    [90302] = {"ui_gift_arena_3_born", "ui_gift_arena_3_idle"},
    [90402] = {"ui_gift_arena_4_born", "ui_gift_arena_4_idle"},
}

function GiftPopUI:ctor(params)
	params = params or {}

	self.actType = PayManager.PURCHARSE_TYPE.ACT_GIFT
    self.actId = DataManager.ArenaData:getGiftId()
    self.showType = params.showType

    DataManager.ArenaData:cleaShowPopGift()
end

function GiftPopUI:isFullScreen()
    return false
end

function GiftPopUI:getPrefabPath()
    return "assets/prefabs/ui/arena/arena_gift_pop_ui.prefab"
end

function GiftPopUI:onClose()
end

function GiftPopUI:onLoadRootComplete()
    if not self.actType or not self.actId then
        self:closeUI()
        return
    end

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

	self.titleTx = self.uiMap["arena_gift_pop_ui.bg.banner.title"]

    self.rewardCellList = {}
    for i = 1, MAX_ITEM_NUM do
        table.insert(self.rewardCellList, CellManager:addCellComp(self.uiMap["gift_pop_ui.bg.item_node.pop_reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.POP_REWARD_CELL))
    end
    self.itemNodeLayout =  self.uiMap["gift_pop_ui.bg.item_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)

    self.timeNode = self.uiMap["gift_pop_ui.bg.time_node"]
    self.timeText = self.uiMap["gift_pop_ui.bg.time_node.text"]
	self.buyBtn = self.uiMap["gift_pop_ui.bg.buy_btn"]
	self.buyBtnTx = self.uiMap["gift_pop_ui.bg.buy_btn.text"]
    self.textOrigin = self.uiMap["arena_gift_pop_ui.bg.buy_btn.text_origin"]
    self.desctx = self.uiMap["arena_gift_pop_ui.bg.desc"]

	self.buyBtn:addClickListener(function()
		self:onClickGift()
	end)

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

function GiftPopUI:refresh()
    self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_40))
    self.desctx:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_POP_GIFT_DESC_1))

    local cfgInfo = PayManager:getGiftConfigInfo(self.actType, self.actId)

	local rewards = cfgInfo.reward or {}
	for i = 1, MAX_ITEM_NUM do
        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
	end
	self.itemNodeLayout:RefreshLayout()

    local rechargeId = cfgInfo.recharge_id
    if rechargeId then
	    self.buyBtnTx:setText(GFunc.getFormatPrice(rechargeId))
        if self.actId ~= MAX_GIFT_ID then
            self.textOrigin:setVisible(true)
            local maxCfgInfo = PayManager:getGiftConfigInfo(self.actType, MAX_GIFT_ID)
            if maxCfgInfo and maxCfgInfo.recharge_id then
                self.textOrigin:setText(GFunc.getFormatPrice(maxCfgInfo.recharge_id))
            end
            self.buyBtnTx:setAnchoredPositionY(-8)
        else
            self.buyBtnTx:setAnchoredPositionY(6)
            self.textOrigin:setVisible(false)
        end
    end

    -- 上报
    local giftType = PayManager:getGiftType(self.actType, self.actId)
    BIReport:postPayUIShow(giftType, self.actId, self.showType)

    self:updateTime()
end

function GiftPopUI:updateTime()
    local hasTime = true
    local remainTime = DataManager.ArenaData:getGiftRemainTime()
    if remainTime <= 0 then
        self:closeUI()
    end
    self.timeText:setText(Time:formatNumTime(remainTime))
    self.timeNode:setVisible(hasTime)
end

function GiftPopUI:onClickGift()
    PayManager:purchasePackage(self.actId, self.actType)
end

function GiftPopUI:showSpine()
    local uiMap = self.root:genAllChildren()
    local spineObj = uiMap["arena_gift_pop_ui.bg.banner.ui_spine_obj"]
    local spineInfo = SPINE_NAME[self.actId]
    if spineInfo then
        spineObj:playAnimComplete(spineInfo[1], false, true, function()
            spineObj:playAnim(spineInfo[2], true, true)
        end)
    end
end

return GiftPopUI