local BaseTips = require "app/ui/tips/base_tips"
local HelpTips = class("HelpTips", BaseTips)

local BG_COLOR = BF.Color(0, 0, 0, 0.94)
local BG_COLOR_A = BF.Color(0, 0, 0, 0)

function HelpTips:showCommonBG()
	return true
end

function HelpTips:ctor(params)
	self.params = params
	self.params.type = self.params.type or GConst.TipsConst.HELP_TIPS_TYPE.NORMAL
	self.targetObj = params.targetObj
end

function HelpTips:getPrefabPath()
	return "assets/prefabs/ui/tips/help_tips.prefab"
end

function HelpTips:onClose()
	self:clearAni()
end

function HelpTips:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	-- 默认样式
	self.styleNormal = uiMap["help_tips.style_normal"]
	self.normalTxTitle = uiMap["help_tips.style_normal.title_tx"]
	self.normaBtnClose = uiMap["help_tips.style_normal.close_btn"]
	self.normaScrollView = uiMap["help_tips.style_normal.ScrollView"]
	self.normaContent = uiMap["help_tips.style_normal.ScrollView.Viewport.Content"]
	self.normaDesc = uiMap["help_tips.style_normal.ScrollView.Viewport.Content.desc"]
	-- 竞技场样式
	self.styleArena = uiMap["help_tips.style_arena"]
	self.arenaTxTitle = uiMap["help_tips.style_arena.title_tx"]
	self.arenaBtnClose = uiMap["help_tips.style_arena.btn_close"]
	self.arenaTxClose = uiMap["help_tips.style_arena.btn_close.tx_close"]
	self.arenaContent = uiMap["help_tips.style_arena.ScrollView.Viewport.Content"]
	self.arenaTxDesc = uiMap["help_tips.style_arena.ScrollView.Viewport.Content.tx_desc"]

	self.normaBtnClose:addClickListener(function ()
		self:playCloseAni(function()
			self:closeUI()
		end)
	end)
	self.arenaBtnClose:addClickListener(function ()
		self:closeUI()
	end)
	self.root:addClickListener(function()
		if self.params.type == GConst.TipsConst.HELP_TIPS_TYPE.NORMAL then
			self:playCloseAni(function()
				self:closeUI()
			end)
		else
			self:closeUI()
		end
	end)
end

function HelpTips:onRefresh()
	self.styleNormal:setActive(false)
	self.styleArena:setActive(false)
	if self.params.type == GConst.TipsConst.HELP_TIPS_TYPE.ARENA then
		self:showStyleArena()
	else
		self:showStyleNormal()
	end
end

function HelpTips:showStyleNormal()
	self.styleNormal:setActive(true)
	self:playOpenAni()
	self.normalTxTitle:setText(self.params.title or I18N:getGlobalText(I18N.GlobalConst.HELP_DESC))
	self.normaDesc:setText(self.params.desc)

	local meshProComp = self.normaDesc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
	local height = meshProComp.preferredHeight
    self.normaContent:setSizeDeltaY(height + 32)
	self.normaContent:setAnchoredPosition(0, 0)
end

function HelpTips:showStyleArena()
	self.styleArena:setActive(true)
	self.arenaTxTitle:setText(self.params.title)
	self.arenaTxClose:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK))
	self.arenaTxDesc:setText(self.params.desc)

	local height = self.arenaTxDesc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredHeight
    self.arenaContent:setSizeDeltaY(height + 5)
	self.arenaContent:setAnchoredPosition(0, 0)
end

function HelpTips:playOpenAni()
	self:clearAni()

	self.styleNormal:setLocalScale(1, 1, 1)
	local bgImage = self.root:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE)
	bgImage.color = BG_COLOR
	if self.targetObj then
		local uiCamera = UIManager:getUICameraComponent()
		local rectTransform = self.targetObj:getComponent(GConst.TYPEOF_UNITY_CLASS.RECTTRANSFORM)
		local pivot = rectTransform.pivot
		local rect = rectTransform.rect
		local tarCorner = rectTransform:TransformPoint(BF.Vector3(rect.width*(0.5 - pivot.x), rect.height*(0.5 - pivot.y), 0))

		local screenPoint = uiCamera:WorldToScreenPoint(tarCorner)
		self.tarCornerLp = CS.BF.Utils.RectTransformScreenPointToLocalPointInRectangle(UIManager:getMainCanvasTransform(), screenPoint.x, screenPoint.y, uiCamera)

		local sizeDelta = self.styleNormal:getSizeDelta()
		local targetX, targetY = sizeDelta.x / 2, sizeDelta.y / 2

		local pivotX, pivotY
		if self.tarCornerLp.x > targetX then
			pivotX = 1
		elseif self.tarCornerLp.x < - targetX then
			pivotX = 0
		else
			pivotX = 0.5 * (1 + self.tarCornerLp.x / targetX)
		end

		if self.tarCornerLp.y > targetY then
			pivotY = 1
		elseif self.tarCornerLp.y < - targetY then
			pivotY = 0
		else
			pivotY = 0.5 * (1 + self.tarCornerLp.y / targetY)
		end

		self.styleNormal:getTransform().pivot = BF.Vector2(pivotX, pivotY)
		self.styleNormal:setAnchoredPosition(self.tarCornerLp.x, self.tarCornerLp.y)
		self.styleNormal:setLocalScale(0, 0, 0)
		bgImage.color = BG_COLOR_A
		self.scaleAniSe = self.styleNormal:createBindTweenSequence()
		targetX = (pivotX - 0.5) * sizeDelta.x
		targetY = (pivotY - 0.5) * sizeDelta.y
		self.scaleAniSe:Append(self.styleNormal:getTransform():DOLocalMove(BF.Vector2(targetX, targetY), 0.1))
		self.scaleAniSe:Join(self.styleNormal:getTransform():DOScale(1, 0.1))
		self.scaleAniSe:Join(bgImage:DOFade(BG_COLOR.a, 0.1))
	else
		self.tarCornerLp = BF.Vector2(0, 0)
		self.styleNormal:getTransform().pivot = BF.Vector2(0.5, 0.5)
		self.styleNormal:setAnchoredPosition(self.tarCornerLp.x, self.tarCornerLp.y)
	end
end

function HelpTips:playCloseAni(callback)
	self:clearAni()

	if self.targetObj then
		local bgImage = self.root:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE)
		self.scaleAniSe = self.styleNormal:createBindTweenSequence()
		self.scaleAniSe:Append(self.styleNormal:getTransform():DOLocalMove(BF.Vector2(self.tarCornerLp.x, self.tarCornerLp.y), 0.1))
		self.scaleAniSe:Join(self.styleNormal:getTransform():DOScale(0, 0.1))
		self.scaleAniSe:Join(bgImage:DOFade(0, 0.1))
		self.scaleAniSe:OnComplete(function()
			if callback then
				callback()
			end
		end)
	else
		self.styleNormal:setLocalScale(1, 1, 1)
		self.styleNormal:setAnchoredPosition(self.tarCornerLp.x, self.tarCornerLp.y)
		if callback then
			callback()
		end
	end
end

function HelpTips:clearAni()
	if self.scaleAniSe then
		self.scaleAniSe:Kill()
		self.scaleAniSe = nil
	end
end

return HelpTips