local ArmorFundUI = class("ArmorFundUI", BaseUI)

function ArmorFundUI:isFullScreen()
	return false
end

function ArmorFundUI:showCommonBG()
	return false
end

function ArmorFundUI:getPrefabPath()
	return "assets/prefabs/ui/dungeon_armor/armor_fund_ui.prefab"
end

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

function ArmorFundUI:ctor()
end

function ArmorFundUI:onCover()
end

function ArmorFundUI:onReshow()
end

function ArmorFundUI:onClose()
end

function ArmorFundUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

    self.txTitle = uiMap["armor_fund_ui.content.title.tx_title"]
    self.btnClose = uiMap["armor_fund_ui.content.close_btn"]
    self.scrollrectComp = uiMap["armor_fund_ui.content.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.txDesc = uiMap["armor_fund_ui.content.buy_node.tx_desc"]
    self.btnBuy = uiMap["armor_fund_ui.content.buy_node.btn_buy"]
    self.txBuy = uiMap["armor_fund_ui.content.buy_node.btn_buy.tx_buy"]

    self.armorEntity = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR)

	self.btnBuy:addClickListener(function()
        PayManager:purchasePackage(self.curGiftId, PayManager.PURCHARSE_TYPE.ACT_GIFT)
	end)
	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self:bind(self.armorEntity, "isDirty", function()
		self:onRefresh()
	end)
	self:bind(DataManager.ShopData, "isDirty", function()
		self:onRefresh()
	end)
end

function ArmorFundUI:onRefresh()
    self.curStage = self.armorEntity:getCurFundStage()
    self.curGiftId = self.armorEntity:getFundStageGiftId(self.curStage)
	self.txTitle:setText(self.armorEntity:getFundStageTitle())
	self:showRewardList()
	self:refreshBuy()
end

function ArmorFundUI:showRewardList()
	local datas = self.armorEntity:getFundRewardIdsSort()
	self.scrollrectComp:addInitCallback(function()
		return "app/ui/dungeon_armor/cell/armor_fund_reward_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(datas[index])
	end)
	self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#datas)
end

function ArmorFundUI:refreshBuy()
    local gift = DataManager.ShopData:getActGiftConfig()[self.curGiftId]
    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_16) .. "  <size=40>" .. gift.value .. "%</size>")
    if not self.armorEntity:isBoughtFundStage(self.curStage) then
        -- 可购买
	    self.txBuy:setText(GFunc.getFormatPrice(gift.recharge_id))
        self.btnBuy:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_2")
        self.btnBuy:setTouchEnable(true)
    else
        -- 已购买
	    self.txBuy:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))
        self.btnBuy:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
        self.btnBuy:setTouchEnable(false)
    end
end

return ArmorFundUI