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

function EquipGetEesonateUI:ctor(params)
	self.type = params.type
	self.lvNew = params.lvNew
	self.attrs = params.attrs
	self.attrNexts = params.attrNexts
end

function EquipGetEesonateUI:getPrefabPath()
	return "assets/prefabs/ui/equip/equip_get_resonate_ui.prefab"
end

function EquipGetEesonateUI:setRootUI(ui)
	self.parentUI = ui
end

function EquipGetEesonateUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.root:addClickListener(function()
		self:closeUI()
	end)

	self.bg = uiMap["equip_get_resonate_ui.bg"]
	uiMap["equip_get_resonate_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_33))
	uiMap["equip_get_resonate_ui.bg.name_tx"]:setText(I18N:getGlobalText("EQUIP_HERO_LV_" .. self.type, self.lvNew))

	self.attrBgs = {}
	self.attrNameTxs = {}
	self.attrLayouts = {}
	self.attrCurrValueTxs = {}
	self.attrArrowImgs = {}
	self.attrNextValueTxs = {}
	for i = 1, 2 do
		self.attrBgs[i] = uiMap["equip_get_resonate_ui.bg.attr_bg_" .. i]
		self.attrNameTxs[i] = uiMap["equip_get_resonate_ui.bg.attr_bg_" .. i .. ".attr_name_tx"]
		self.attrLayouts[i] = uiMap["equip_get_resonate_ui.bg.attr_bg_" .. i .. ".layout"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
		self.attrCurrValueTxs[i] = uiMap["equip_get_resonate_ui.bg.attr_bg_" .. i .. ".layout.attr_curr_value_tx"]
		self.attrArrowImgs[i] = uiMap["equip_get_resonate_ui.bg.attr_bg_" .. i .. ".layout.arrow_img"]
		self.attrNextValueTxs[i] = uiMap["equip_get_resonate_ui.bg.attr_bg_" .. i .. ".layout.attr_next_value_tx"]
	end
end

function EquipGetEesonateUI:onRefresh()
	local typeList = {}
	for k, v in pairs(self.attrNexts or {}) do
		table.insert(typeList, {typeName = k, id = GFunc.getAttrIdByName(k)})
	end
	if #typeList <= 0 then
		for k, v in pairs(self.attrs or {}) do
			table.insert(typeList, {typeName = k, id = GFunc.getAttrIdByName(k)})
		end
	end
	table.sort(typeList, function (a, b)
		return a.id < b.id
	end)
	for i = 1, 2 do
		if typeList[i] and typeList[i].typeName then
			local typeName = typeList[i].typeName
			self.attrBgs[i]:setActive(true)
			self.attrNameTxs[i]:setText(GFunc.getAttrNameByType(typeName))
			self.attrCurrValueTxs[i]:setText(GFunc.getFinalAttrValue(typeName, self.attrs[typeName] or 0))
			local meshProCompNow = self.attrCurrValueTxs[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
			local nowTipsNowX = meshProCompNow.preferredWidth
			self.attrCurrValueTxs[i]:setSizeDeltaX(nowTipsNowX)

			self.attrNextValueTxs[i]:setText(GFunc.getFinalAttrValue(typeName, self.attrNexts[typeName] or 0))
			self.attrArrowImgs[i]:setActive(true)
			self.attrNextValueTxs[i]:setActive(true)

			local meshProCompNext = self.attrNextValueTxs[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
			local nowTipsNextX = meshProCompNext.preferredWidth
			self.attrNextValueTxs[i]:setSizeDeltaX(nowTipsNextX)
			self.attrLayouts[i]:RefreshLayout()
		else
			self.attrBgs[i]:setActive(false)
		end
	end
	if #typeList < 2 then
		self.bg:setSizeDeltaY(200)
	else
		self.bg:setSizeDeltaY(260)
	end

	-- self.layout = uiMap["equip_get_resonate_ui.bg.attr_bg.layout"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
	-- self.attrNextValueTx = uiMap["equip_get_resonate_ui.bg.attr_bg.layout.attr_next_value_tx"]
	-- -- self.effect = uiMap["equip_get_resonate_ui.bg.vfx_b13_ui_equip_gongmin_b01"]
	-- -- self.effect:play()
	-- uiMap["equip_get_resonate_ui.bg.attr_bg.attr_name_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.ATTR_ATK))
	-- uiMap["equip_get_resonate_ui.bg.attr_bg.layout.attr_curr_value_tx"]:setText(self.attrNum)
	-- self.attrNextValueTx:setText(self.attrNextNum)
	-- GFunc.setTextLength(self.attrNextValueTx)
	-- self.layout:RefreshLayout()
end

return EquipGetEesonateUI