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

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

function ItemTips:getPrefabPath()
	return "assets/prefabs/ui/tips/item_tips.prefab"
end

function ItemTips:init()
	local uiMap = self.root:genAllChildren()
    self.bg = uiMap["item_tips.bg1.bg"]
    self.bg1 = uiMap["item_tips.bg1"]
    self.nameTx = uiMap["item_tips.bg1.bg.name_tx"]
    self.descTx = uiMap["item_tips.bg1.bg.desc_tx"]
    self.icon = uiMap["item_tips.bg1.bg.icon"]
end

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

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

	local item = DataManager.BagData.ItemData:getItemById(self.params.id)
	self.nameTx:setText(item:getName())
	self.descTx:setText(item:getDesc())
	self.icon:setSprite(item:getIconRes())
    self.bg:addSizeDelta(0, self.descTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredHeight)
    if self.tarCornerScreenPos then
        self:locate(self.location, self.originSizeDelta, self.bg, self.tarCornerScreenPos)
    end
end

function ItemTips: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

return ItemTips