local GrowthFundUI = class("GrowthFundUI", BaseUI)

function GrowthFundUI:isFullScreen()
	return true
end

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

function GrowthFundUI:getCurrencyParams()
    if self.currencyParams == nil then
        self.currencyParams = {
            itemIds = {
				GConst.ItemConst.ITEM_ID_GOLD,
				GConst.ItemConst.ITEM_ID_GEM,
				GConst.ItemConst.ITEM_ID_VIT,
			},
			showType = GConst.CURRENCY_TYPE.HORIZONTAL
        }
    end
 	return self.currencyParams
end

function GrowthFundUI:getPrefabPath()
    return "assets/prefabs/ui/fund/growth_fund_ui.prefab"
end

function GrowthFundUI:ctor()
	self.fundGrade = DataManager.GrowthFundData:getFundGrade()
	self:initGradeInfoList()
end

function GrowthFundUI:initGradeInfoList()
	self.gradeInfoList = DataManager.GrowthFundData:getCurrGradeInfoList()
	local playerLevel = DataManager.PlayerData:getLv()
	local cfg = ConfigManager:getConfig("act_level_fund")
	self.inactiveIndex = nil
	for k, v in ipairs(self.gradeInfoList) do
		if cfg[v] then
			if playerLevel < cfg[v].level then
				self.inactiveIndex = k
				break
			end
		end
	end
	if self.inactiveIndex == nil then
		self.inactiveIndex = #self.gradeInfoList + 1
	end
end

function GrowthFundUI:onClose()
	if self.originTitleBgHeight then
		self.uiMap["growth_fund_ui.title_img.bg"]:setSizeDeltaY(self.originTitleBgHeight)
	end
	if self.heroNode then
		self.heroNode:removeAllChildren()
	end
end

function GrowthFundUI:onLoadRootComplete()
	self.uiMap = self.root:genAllChildren()

	self.uiMap["growth_fund_ui.down.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self:initTitle()
	self:initPayBtns()
	self:initRewards()
	self:bindData()
	BIReport:postPayUIShow(BIReport.GIFT_TYPE.GROWTH_FUND, nil, BIReport.PAY_UI_SHOW_TYPE.CLICK_SHOW)
end

function GrowthFundUI:initTitle()
	self.titleTx = self.uiMap["growth_fund_ui.title_img.title_tx"]
	self.titleDialogBg = self.uiMap["growth_fund_ui.title_img.bg"]
	self.originTitleBgHeight = self.titleDialogBg:fastGetSizeDeltaY()
	self.heroNode = self.uiMap["growth_fund_ui.title_img.hero_node"]
	self.heroNode:removeAllChildren()
end

function GrowthFundUI:initPayBtns()
	local width = GConst.UI_SCREEN_WIDTH

	local freeTx = self.uiMap["growth_fund_ui.title_bg_2.free_tx"]
	freeTx:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE))
	freeTx:setAnchoredPositionX(-width/3)

	self.payBtn1 = self.uiMap["growth_fund_ui.title_bg_2.btn_1"]
	self.payBtn1:addClickListener(function()
		ModuleManager.FundManager:buyLevelFund(DataManager.GrowthFundData:getFundBaseId())
	end)
	self.payBtnTx1 = self.uiMap["growth_fund_ui.title_bg_2.btn_1.text"]

	self.payBtn2 = self.uiMap["growth_fund_ui.title_bg_2.btn_2"]
	self.payBtn2:setAnchoredPositionX(width/3)
	self.payBtn2:addClickListener(function()
		ModuleManager.FundManager:buyLevelFund(DataManager.GrowthFundData:getFundAdvanceId())
	end)
	self.payBtnTx2 = self.uiMap["growth_fund_ui.title_bg_2.btn_2.text"]
end

function GrowthFundUI:initRewards()
	self.scrollRect = self.uiMap["growth_fund_ui.scrollrect"]
	self.scrollRectComp = self.scrollRect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.scrollRectComp:addInitCallback(function()
		return "app/ui/fund/cell/growth_fund_cell"
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.gradeInfoList[index])
	end)
	self.scrollRectComp:clearCells()
	self.scrollRectComp:setTotalCount(0)

	self.progressBg = self.uiMap["growth_fund_ui.scrollrect.viewport.content.progress_bg"]
	self.progressComp = self.uiMap["growth_fund_ui.scrollrect.viewport.content.progress_bg.progress"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.grayImg = self.uiMap["growth_fund_ui.scrollrect.viewport.content.gray_img"]
	self.horizontalLine = self.uiMap["growth_fund_ui.scrollrect.viewport.content.line_img"]
	self.verticalLine1 = self.uiMap["growth_fund_ui.scrollrect.viewport.content.vertical_line_1"]
	self.verticalLine2 = self.uiMap["growth_fund_ui.scrollrect.viewport.content.vertical_line_2"]
end

function GrowthFundUI:bindData()
	self:bind(DataManager.GrowthFundData, "dirty", function()
		if self.fundGrade ~= DataManager.GrowthFundData:getFundGrade() then
			self.fundGrade = DataManager.GrowthFundData:getFundGrade()
			self:initGradeInfoList()
		end
		self:onRefresh()
	end)
end

function GrowthFundUI:onRefresh()
	self:refreshTitle()
	self:refreshPayBtns()
	self:refreshRewards()
end

function GrowthFundUI:refreshTitle()
	local fundGrade = DataManager.GrowthFundData:getFundGrade()
	if fundGrade == 1 then
		self.uiMap["growth_fund_ui.title_img"]:setTexture("assets/arts/textures/background/fund/fund_bg_1.png")
		self.titleDialogBg:setSprite(GConst.ATLAS_PATH.FUND, "fund_bg_9")
		self:showHeroSpine(1)
	elseif fundGrade == 2 then
		self.uiMap["growth_fund_ui.title_img"]:setTexture("assets/arts/textures/background/fund/fund_bg_2.png")
		self.titleDialogBg:setSprite(GConst.ATLAS_PATH.FUND, "fund_bg_1")
		self:showHeroSpine(2)
	end
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.LEVEL_FUND_DESC_1, fundGrade))
	local height = self.titleTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredHeight + 58
	if height < self.originTitleBgHeight then
		height = self.originTitleBgHeight
	end
	self.titleDialogBg:setSizeDeltaY(height)
end

function GrowthFundUI:refreshPayBtns()
	if not DataManager.GrowthFundData:getIsBoughtBase() then
		local rechargeId =  DataManager.GrowthFundData:getCurrGradeBaseRechargeId()
		self.payBtnTx1:setText(GFunc.getFormatPrice(rechargeId))
		self.payBtn1:setTouchEnable(true)
	else
		self.payBtnTx1:setText(I18N:getGlobalText(I18N.GlobalConst.ALREADY_ACTIVE))
		self.payBtn1:setTouchEnable(false)
	end
	if not DataManager.GrowthFundData:getIsBoughtAdvance() then
		local rechargeId =  DataManager.GrowthFundData:getCurrGradeAdvanceRechargeId()
		self.payBtnTx2:setText(GFunc.getFormatPrice(rechargeId))
		self.payBtn2:setTouchEnable(true)
	else
		self.payBtnTx2:setText(I18N:getGlobalText(I18N.GlobalConst.ALREADY_ACTIVE))
		self.payBtn2:setTouchEnable(false)
	end
end

function GrowthFundUI:refreshRewards()
	local count = #self.gradeInfoList
	if self.scrollRectComp:getTotalCount() <= 0 then
		self.scrollRectComp:refillCells(count)
		local minIndex = DataManager.GrowthFundData:getMinUnclaimedRewardIndex()
		if minIndex > 1 then
			self.scrollRectComp:moveToIndex(minIndex)
		end
	elseif self.scrollRectComp:getTotalCount() ~= count then -- 打开界面的时候定位到当前可领取的最低等级奖励，如果没有则定位到当前等级
		self.scrollRectComp:clearCells()
		self.scrollRectComp:refillCells(count)
	else
		self.scrollRectComp:updateAllCell()
	end

	local topRecoveryOffset = self.scrollRectComp:getTopRecoveryOffset()
	local contentWidth = self.scrollRect:getRectWidth()
	local cellWidth = self.scrollRectComp:getCellHeight()
	self.progressBg:setAnchoredPosition(contentWidth/3, -topRecoveryOffset - cellWidth/2)
	self.progressBg:setSizeDeltaX(cellWidth*(count - 1))
	local percent = (self.inactiveIndex - 2) / (count - 1)
	if percent < 0 then
		percent = 0
	end
	self.progressComp.value = percent

	self.grayImg:setAnchoredPositionY(-topRecoveryOffset - (self.inactiveIndex - 1)*cellWidth)
	self.grayImg:setSizeDeltaY(cellWidth*count + GConst.UI_SCREEN_HEIGHT)

	local lineHeight = topRecoveryOffset + cellWidth*count + GConst.UI_SCREEN_HEIGHT*2
	self.verticalLine1:setAnchoredPositionX(contentWidth/3)
	self.verticalLine1:setSizeDeltaY(lineHeight)

	self.verticalLine2:setAnchoredPositionX(contentWidth*2/3)
	self.verticalLine2:setSizeDeltaY(lineHeight)

	self.horizontalLine:setAnchoredPositionY(-topRecoveryOffset - (self.inactiveIndex - 1)*cellWidth)
end

function GrowthFundUI:showHeroSpine(index)
	if self.heroSpineIndex == index then
		return
	end
	self.heroSpineIndex = index
	local heroId = 0
	if index == 1 then
		heroId = GFunc.getConstValue("level_fund_hero_1")
	elseif index == 2 then
		heroId = GFunc.getConstValue("level_fund_hero_2")
	end
	if heroId <= 0 then
		return
	end
	local hero = DataManager.HeroData:getHeroById(heroId)
	if not hero then
		return
	end
	SpineManager:loadHeroAsync(hero:getModelId(), self.heroNode, function(spineObject)
		if self:isClosed() then
			spineObject:destroy()
			return
		end
		if self.heroSpine then
			self.heroSpine:destroy()
		end
		spineObject:playAnimation("idle", true)
		self.heroSpine = spineObject
	end)
end

return GrowthFundUI