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

function FormationTips:ctor(params)
    self.tarCornerScreenPos = params.tarCornerScreenPos
    self.location = params.location
    self.formation = params.formation
end

function FormationTips:getPrefabPath()
	return "assets/prefabs/ui/tips/formation_tips.prefab"
end

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

function FormationTips:init()
	local uiMap = self.root:genAllChildren()
    self.bg = uiMap["formation_tips.bg.bg"]
    self.heroFormation = uiMap["formation_tips.bg.hero_formation_comp"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_FORMATION_COMP)
	self.arrow = uiMap["formation_tips.bg.bg.arrow"]
end

function FormationTips:onLoadRootComplete()
	self:init()
    local tipsBgTransform = self.bg:getTransform()
    self.originSizeDelta = tipsBgTransform.sizeDelta
	self.originPivot = tipsBgTransform.pivot
	self.originAnchoredPosition = tipsBgTransform.anchoredPosition
	self.originLocalPosition = tipsBgTransform.localPosition

	self.heroFormation:refreshByEntitys(self.formation)
end

function FormationTips:onRefresh()
	self.root:addClickListener(function ()
		self:closeUI()
	end)

    if self.tarCornerScreenPos then
        self:locate(self.location, self.originSizeDelta, self.bg, self.tarCornerScreenPos)
    end
end

function FormationTips:onClose()
	if self.originSizeDelta then
		local tipsBgTransform = self.bg:getTransform()
		tipsBgTransform.sizeDelta = self.originSizeDelta
		tipsBgTransform.pivot = self.originPivot
		tipsBgTransform.anchoredPosition = self.originAnchoredPosition
		tipsBgTransform.localPosition = self.originLocalPosition
	end
end

function FormationTips:adaptWhenOverScreen(...)
	local xOffset, _ = BaseTips.adaptWhenOverScreen(self, ...)
	if self.arrow then
		self.arrow:setAnchoredPositionX(110 - xOffset)
	end
end

return FormationTips