local RunesSourceUI = class("RunesSourceUI", BaseUI)

function RunesSourceUI:isFullScreen()
    return false
end

function RunesSourceUI:getPrefabPath()
    return "assets/prefabs/ui/runes/runes_source_ui.prefab"
end

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

function RunesSourceUI:ctor()
    self.curGiftIndex = 1
end

function RunesSourceUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.btnClose = uiMap["runes_source_ui.btn_close"]
    -- 来源界面
    self.panelMain = uiMap["runes_source_ui.panel_main"]
    self.txTitleMain = uiMap["runes_source_ui.panel_main.title.tx_title"]
    self.txTips = uiMap["runes_source_ui.panel_main.img_banner.tx_tips"]
    self.txGet = uiMap["runes_source_ui.panel_main.tx_get"]
    self.txDesc = uiMap["runes_source_ui.panel_main.target.tx_desc"]
    self.btnGo = uiMap["runes_source_ui.panel_main.target.btn_go"]
    self.txGo = uiMap["runes_source_ui.panel_main.target.btn_go.tx_go"]
    -- 礼包界面
    self.panelGift = uiMap["runes_source_ui.panel_gift"]
    self.btn1 = uiMap["runes_source_ui.panel_gift.btns.btn_1"]
    self.txBtn1 = uiMap["runes_source_ui.panel_gift.btns.btn_1.tx_btn"]
    self.select1 = uiMap["runes_source_ui.panel_gift.btns.btn_1.select"]
    self.txSelect1 = uiMap["runes_source_ui.panel_gift.btns.btn_1.select.tx_select"]
    self.btn2 = uiMap["runes_source_ui.panel_gift.btns.btn_2"]
    self.txBtn2 = uiMap["runes_source_ui.panel_gift.btns.btn_2.tx_btn"]
    self.select2 = uiMap["runes_source_ui.panel_gift.btns.btn_2.select"]
    self.txSelect2 = uiMap["runes_source_ui.panel_gift.btns.btn_2.select.tx_select"]
    self.btn3 = uiMap["runes_source_ui.panel_gift.btns.btn_3"]
    self.txBtn3 = uiMap["runes_source_ui.panel_gift.btns.btn_3.tx_btn"]
    self.select3 = uiMap["runes_source_ui.panel_gift.btns.btn_3.select"]
    self.txSelect3 = uiMap["runes_source_ui.panel_gift.btns.btn_3.select.tx_select"]
    self.txTitleGift = uiMap["runes_source_ui.panel_gift.tx_title"]
    self.txTime = uiMap["runes_source_ui.panel_gift.time.tx_time"]
    self.btnBuy = uiMap["runes_source_ui.panel_gift.btn_buy"]
    self.txBuy = uiMap["runes_source_ui.panel_gift.btn_buy.tx_buy"]
    self.giftRewardCells = {}
    for i = 1, 4 do
        self.giftRewardCells[i] = uiMap["runes_source_ui.panel_gift.item_node.pop_reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.POP_REWARD_CELL)
    end

    self.txTitleGift:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_17))
    self.txTitleMain:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_18))
    self.txTips:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_19))
    self.txGet:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_23))
    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_20))
    self.txGo:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_21))

    local gift1 = DataManager.ShopData:getActGiftConfig()[GConst.RunesConst.GIFT_IDS[1]]
    self.txBtn1:setText(GFunc.getFormatPrice(gift1.recharge_id))
    self.txSelect1:setText(GFunc.getFormatPrice(gift1.recharge_id))
    local gift2 = DataManager.ShopData:getActGiftConfig()[GConst.RunesConst.GIFT_IDS[2]]
    self.txBtn2:setText(GFunc.getFormatPrice(gift2.recharge_id))
    self.txSelect2:setText(GFunc.getFormatPrice(gift2.recharge_id))
    local gift3 = DataManager.ShopData:getActGiftConfig()[GConst.RunesConst.GIFT_IDS[3]]
    self.txBtn3:setText(GFunc.getFormatPrice(gift3.recharge_id))
    self.txSelect3:setText(GFunc.getFormatPrice(gift3.recharge_id))

    self.btn1:addClickListener(function()
        if self.curGiftIndex == 1 then
            return
        end
        self.curGiftIndex = 1
        self:onRefresh()
    end)
    self.btn2:addClickListener(function()
        if self.curGiftIndex == 2 then
            return
        end
        self.curGiftIndex = 2
        self:onRefresh()
    end)
    self.btn3:addClickListener(function()
        if self.curGiftIndex == 3 then
            return
        end
        self.curGiftIndex = 3
        self:onRefresh()
    end)
    self.btnGo:addClickListener(function()
        ModuleManager.DungeonRuneManager:showMainUI()
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.GO_DUNGEON_UI)
        self:closeUI()
    end)
    self.btnBuy:addClickListener(function()
        PayManager:purchasePackage(GConst.RunesConst.GIFT_IDS[self.curGiftIndex], PayManager.PURCHARSE_TYPE.ACT_GIFT)
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:bind(DataManager.ShopData, "isDirty", function()
        self:onRefresh()
    end)
end

function RunesSourceUI:onRefresh()
    self.select1:setActive(self.curGiftIndex == 1)
    self.select2:setActive(self.curGiftIndex == 2)
    self.select3:setActive(self.curGiftIndex == 3)

    local giftId = GConst.RunesConst.GIFT_IDS[self.curGiftIndex]
    local gift = DataManager.ShopData:getActGiftConfig()[giftId]

    if gift == nil then
        self.panelGift:setActive(false)
        return
    end
    local remain = DataManager.ShopData:getGiftRemainBuyNum(giftId)
    self.panelGift:setActive(true)
    self.txTime:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_18, remain))
    if remain > 0 then
        self.btnBuy:setTouchEnable(true)
        self.btnBuy:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_1")
        self.txBuy:setText(GFunc.getFormatPrice(gift.recharge_id))
    else
        self.btnBuy:setTouchEnable(false)
        self.btnBuy:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
        self.txBuy:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))
    end

    for index, cell in ipairs(self.giftRewardCells) do
        if gift.reward[index] then
            cell:setVisible(true, 0.8)
            cell:refresh(gift.reward[index].id, gift.reward[index].num, true)
        else
            cell:setVisible(false)
        end
	end
end

return RunesSourceUI