local BountyBuyUI = class("BountyBuyUI", BaseUI)

function BountyBuyUI:ctor()
	self.endTime = DataManager.BountyData:getEndTime()
end

function BountyBuyUI:isFullScreen()
	return false
end

function BountyBuyUI:getPrefabPath()
	return "assets/prefabs/ui/bounty/bounty_buy_ui.prefab"
end

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

function BountyBuyUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.uiMap = uiMap
	local closeBtn = uiMap["bounty_buy_ui.bg.close_btn"]
	closeBtn:addClickListener(function()
		self:closeUI()
	end)

	self:initTitle()
	self:initTime()
	self:initRewards()
	self:initBtns()

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

function BountyBuyUI:initTitle()
	self.uiMap["bounty_buy_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BOUNTY_PURCHASE_NAME))
end

function BountyBuyUI:initTime()
	self.timeTx = self.uiMap["bounty_buy_ui.bg.time_tx"]
end

function BountyBuyUI:initRewards()
	self.uiMap["bounty_buy_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
	local preferredWidth = self.uiMap["bounty_buy_ui.bg.desc_tx"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
	self.uiMap["bounty_buy_ui.bg.left_arrow"]:setAnchoredPositionX(-30 - preferredWidth/2)
	self.uiMap["bounty_buy_ui.bg.right_arrow"]:setAnchoredPositionX(30 + preferredWidth/2)

	local list = DataManager.BountyData:getAllProRewards()
	for i = 1, 10 do
		local cell = self.uiMap["bounty_buy_ui.bg.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
		if list[i] then
			cell:setVisible(true, 0.7)
            cell:refreshByConfig(list[i])
		else
			cell:setVisible(false)
        end
	end
end

function BountyBuyUI:initBtns()
	self.uiMap["bounty_buy_ui.bg.name_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.BOUNTY_PURCHASE_NAME))
	self.uiMap["bounty_buy_ui.bg.name_2"]:setText(I18N:getGlobalText(I18N.GlobalConst.BOUNTY_PURCHASE_NAME))
	self.uiMap["bounty_buy_ui.bg.desc_tx_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.BOUNTY_DESC_1))
	self.uiMap["bounty_buy_ui.bg.desc_tx_2"]:setText(I18N:getGlobalText(I18N.GlobalConst.BOUNTY_DESC_2))

	local rechargeId = DataManager.BountyData:getRechargeId(false)
	self.uiMap["bounty_buy_ui.bg.buy_btn.tx"]:setText(GFunc.getFormatPrice(rechargeId))
	local proRechargeId = DataManager.BountyData:getRechargeId(true)
	self.uiMap["bounty_buy_ui.bg.buy_btn_advanced.tx"]:setText(GFunc.getFormatPrice(proRechargeId))

	local buyBtn = self.uiMap["bounty_buy_ui.bg.buy_btn"]
	buyBtn:addClickListener(function()
		self:closeUI()
		ModuleManager.BountyManager:buyBounty(false)
	end)

	local buyBtnAdvanced = self.uiMap["bounty_buy_ui.bg.buy_btn_advanced"]
	buyBtnAdvanced:addClickListener(function()
		local level = DataManager.BountyData:getLevel()
		local maxLevel = DataManager.BountyData:getMaxLevel()
		local buyProBountyAddLevels = DataManager.BountyData:getBuyProBountyAddLevelCount()
		if level > maxLevel - buyProBountyAddLevels then
			local params = {
				content = I18N:getGlobalText(I18N.GlobalConst.BOUNTY_DESC_4),
				boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
				okFunc = function()
					self:closeUI()
					ModuleManager.BountyManager:buyBounty(true)
				end
			}
			GFunc.showMessageBox(params)
		else
			self:closeUI()
			ModuleManager.BountyManager:buyBounty(true)
		end
	end)
end

function BountyBuyUI:updateTime()
	local remainTime = self.endTime - Time:getServerTime()
	if remainTime < 0 then
		UIManager:closeUnderUI(self)
		return self:closeUI()
	end
	if remainTime > 86400 then
		self.timeTx:setText(GFunc.getTimeStrWithDHM(remainTime))
	else
		self.timeTx:setText(GFunc.getTimeStrWithHMS2(remainTime))
	end
end

return BountyBuyUI