local EquipResonateUI = class("EquipResonateUI", BaseUI)

function EquipResonateUI:onClose()
end
function EquipResonateUI:isFullScreen()
	return false
end
function EquipResonateUI:ctor(params)
	params = params or {}
	self.page = params.page or GConst.EquipConst.RESONATE_PAGE.LV_UP
	self.slotId = params and params.slotId
end

function EquipResonateUI:getPrefabPath()
	return "assets/prefabs/ui/equip/equip_resonate_ui.prefab"
end

function EquipResonateUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

	uiMap["equip_resonate_ui.mask"]:addClickListener(function()
		self:closeUI()
	end)
	uiMap["equip_resonate_ui.bg.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	if not self.equipCells then
		self.equipCells = {}
		for i = 1, 6 do
			local cell = uiMap["equip_resonate_ui.bg.equip_list.equip_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.EQUIP_CELL)
			cell:addClickListener(function()
				local uid = DataManager.EquipData:getPartEquipUid(self.slotId, self.part)
				if uid ~= 0 then
					self.part = i
					self:onRefresh()
				end
			end)
			table.insert(self.equipCells, cell)
		end
	end

	self.lvTx = uiMap["equip_resonate_ui.bg.page_info.lv_tx"]
	self.tipsTx = uiMap["equip_resonate_ui.bg.page_info.tips_tx"]
	-- self.attrNameTx = uiMap["equip_resonate_ui.bg.page_info.attr_bg.attr_name_tx"]
	-- self.attrCurrValueTx = uiMap["equip_resonate_ui.bg.page_info.attr_bg.layout.attr_curr_value_tx"]
	-- self.arrowImg = uiMap["equip_resonate_ui.bg.page_info.attr_bg.layout.arrow_img"]
	-- self.attrNextValueTx = uiMap["equip_resonate_ui.bg.page_info.attr_bg.layout.attr_next_value_tx"]
	-- self.layout = uiMap["equip_resonate_ui.bg.page_info.attr_bg.layout"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)

	uiMap["equip_resonate_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_4))
	uiMap["equip_resonate_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_6))

	self.btnTxs = {I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_1), I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_2), I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_3)}
	self.pageBtns = {}
	self.pageBtnTxs = {}
	for i = 1, 3 do
		self.pageBtns[i] = uiMap["equip_resonate_ui.bg.page_btn_" .. i]
		self.pageBtnTxs[i] = uiMap["equip_resonate_ui.bg.page_btn_" .. i .. ".text"]
		self.pageBtns[i]:addClickListener(function()
			if self.page == i then
				return
			end
			self.page = i
			self:onRefresh()
		end)
	end

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

	self:bind(DataManager.EquipData, "isDirty", function()
		self:onRefresh()
	end)
end

function EquipResonateUI:onRefresh()
	self:refreshEquipCell()
	self:refreshPageBtn()
	self:refreshPage()
end

function EquipResonateUI:refreshEquipCell()
	for part = 1, 6 do
		local eid = DataManager.EquipData:getPartEquipUid(self.slotId, part)
		if eid and eid > 0 then
			local equipEntity = DataManager.EquipData:getEquipByUid(eid)
			self.equipCells[part]:refresh(equipEntity, self.slotId)
		else
			self.equipCells[part]:refreshEmpty(part)
		end
	end
end

function EquipResonateUI:refreshPageBtn()
	for i = 1, 3 do
		if self.page == i then
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_menu_1")
			self.pageBtnTxs[i]:setText(self.btnTxs[i])
		else
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_menu_2")
			self.pageBtnTxs[i]:setText(self.btnTxs[i])
		end
	end
end

function EquipResonateUI:refreshPage()
	if self.page == GConst.EquipConst.RESONATE_PAGE.LV_UP then
		self:refreshPageLv()
	elseif self.page == GConst.EquipConst.RESONATE_PAGE.QLT then
		self:refreshPageQlt()
	elseif self.page == GConst.EquipConst.RESONATE_PAGE.REFINE then
		self:refreshPageRefine()
	end
end

function EquipResonateUI:refreshPageLv()
	local lv, nextLv, attrs, attrNexts = DataManager.EquipData:getResonateLevel(self.page, self.slotId)
	self:refreshUI(lv, nextLv, attrs, attrNexts, DataManager.EquipData:getResonateMaxLevel(1))
end

function EquipResonateUI:refreshPageQlt()
	local lv, nextLv, attrs, attrNexts = DataManager.EquipData:getResonateLevel(self.page, self.slotId, self.part)
	self:refreshUI(lv, nextLv, attrs, attrNexts, DataManager.EquipData:getResonateMaxLevel(2))
end

function EquipResonateUI:refreshPageRefine()
	local lv, nextLv, attrs, attrNexts = DataManager.EquipData:getResonateLevel(self.page, self.slotId)
	self:refreshUI(lv, nextLv, attrs, attrNexts, DataManager.EquipData:getResonateMaxLevel(3))
end

function EquipResonateUI:refreshUI(lv, nextLv, attrs, attrNexts, maxLevel)
	self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.LV_POINT) .. lv)
	local typeList = {}
	for k, v in pairs(attrNexts or {}) do
		table.insert(typeList, {typeName = k, id = GFunc.getAttrIdByName(k)})
	end
	if #typeList <= 0 then
		for k, v in pairs(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, 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)

			if maxLevel ~= nil and lv >= maxLevel then
				self.attrArrowImgs[i]:setActive(false)
				self.attrNextValueTxs[i]:setActive(false)
				self.tipsTx:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_2))
			else
				if nextLv == nil then
					self.tipsTx:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_2))
				else
					if self.page == GConst.EquipConst.RESONATE_PAGE.QLT then
						self.tipsTx:setText(I18N:getGlobalText("EQUIP_HERO_M_" .. self.page, I18N:getGlobalText("EQUIP_QLT_DESC_" .. nextLv[1], nextLv[2])))
					else
						self.tipsTx:setText(I18N:getGlobalText("EQUIP_HERO_M_" .. self.page, nextLv[1]))
					end
				end

				self.attrNextValueTxs[i]:setText(GFunc.getFinalAttrValue(typeName, 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)
			end
			self.attrLayouts[i]:RefreshLayout()
		else
			self.attrBgs[i]:setActive(false)
		end
	end
	if #typeList < 2 then
		self.attrBgs[1]:setAnchoredPositionY(-4)
	else
		self.attrBgs[1]:setAnchoredPositionY(26)
	end
end


return EquipResonateUI