local GoldPigUI = class("GoldPigUI", BaseUI)

function GoldPigUI:isFullScreen()
	return false
end

function GoldPigUI:getPrefabPath()
    return "assets/prefabs/ui/activity/gold_pig/gold_pig_ui.prefab"
end

function GoldPigUI:ctor(params)
	self.goldPigId = DataManager.GoldPigData:getId()
	self.showType = params.showType
end

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

	self:initSpine()

	uiMap["gold_pig_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.GOLD_PIG_TITLE))
	uiMap["gold_pig_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.GOLD_PIG_DESC))

	local levelInfo = DataManager.GoldPigData:getCurrLevelInfo()
	uiMap["gold_pig_ui.bg.value_bg.tx"]:setText(levelInfo.value .. "%")
	local currGem = DataManager.GoldPigData:getCount()
	local maxGem = DataManager.GoldPigData:getMaxCount()
	local percent = currGem / maxGem
	local nowGemTx = uiMap["gold_pig_ui.bg.slider.now_gem_bg.gem_tx"]
	nowGemTx:setText(GFunc.intToString(currGem))
	local nowGemImg = uiMap["gold_pig_ui.bg.slider.now_gem_bg.gem_img"]
	GFunc.centerImgAndTx(nowGemImg, nowGemTx, 0, -4)

	local currGold = DataManager.GoldPigData:getGoldCount()
	local nowGoldTx = uiMap["gold_pig_ui.bg.now_gem_bg.gold_tx"]
	nowGoldTx:setText(GFunc.intToString(currGold))
	local nowGoldImg = uiMap["gold_pig_ui.bg.now_gem_bg.gold_img"]
	GFunc.centerImgAndTx(nowGoldImg, nowGoldTx, 0, -4)

	local sliderWidth = uiMap["gold_pig_ui.bg.slider"]:getRectWidth()
	uiMap["gold_pig_ui.bg.slider.now_gem_bg"]:setAnchoredPositionX((percent - 0.5) * sliderWidth)

	local initialCount = DataManager.GoldPigData:getInitialCount()
	uiMap["gold_pig_ui.bg.begin_text"]:setText(GFunc.intToString(initialCount))
	uiMap["gold_pig_ui.bg.end_text"]:setText(GFunc.intToString(maxGem))

	uiMap["gold_pig_ui.bg.slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = percent
	uiMap["gold_pig_ui.bg.buy_btn"]:addClickListener(function()
		ModuleManager.ActivityManager:buyGoldPig(self.goldPigId)
	end)
	local rechargeId = DataManager.GoldPigData:getRechargeId()
	uiMap["gold_pig_ui.bg.buy_btn.text"]:setText(GFunc.getFormatPrice(rechargeId))

	self:bind(DataManager.GoldPigData, "boughtFlag", function()
		-- 购买后需要关闭界面
		if not DataManager.GoldPigData:getIsOpen() then
			self:closeUI()
		end
	end)

	BIReport:postPayUIShow(BIReport.GIFT_TYPE.GOLD_PIG, self.goldPigId, self.showType)

	self.timeTx = uiMap["gold_pig_ui.bg.time_bg.time_tx"]
	local isFull = DataManager.GoldPigData:getIsFull()
	if isFull then
		uiMap["gold_pig_ui.bg.time_bg"]:setVisible(true)
		self.cdSid = self:scheduleGlobal(function()
			self:updateTime()
		end, 1)
		self:updateTime()
	else
		uiMap["gold_pig_ui.bg.time_bg"]:setVisible(false)
	end
end

function GoldPigUI:initSpine()
	self.titleSpine = self.uiMap["gold_pig_ui.bg.ui_spine_obj"]
	local trackEntry = self.titleSpine:playAnim("open", false)
    local time = self.titleSpine:getAnimation(trackEntry).Duration
    self:performWithDelayGlobal(function()
        self.titleSpine:playAnim("idle", true)
    end, time)
end

function GoldPigUI:updateTime()
	local endTime = DataManager.GoldPigData:getEndTime()
	local remainTime = endTime - Time:getServerTime()
	if remainTime > 0 then
		self.timeTx:setText(GFunc.getTimeStrWithHMS(remainTime))
	else
		self.timeTx:setText("00:00:00")
		if self.cdSid then
			self:unscheduleGlobal(self.cdSid)
			self.cdSid = nil
		end
		local params = {
			content = I18N:getGlobalText(I18N.GlobalConst.GOLD_PIG_CLOSE_DESC),
			boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
			okFunc = function()
				self:closeUI()
			end
		}
		GFunc.showMessageBox(params)
	end
end

return GoldPigUI