local EquipInfoUI = class("EquipInfoUI", BaseUI)

function EquipInfoUI:isFullScreen()
	return false
end

function EquipInfoUI:getPrefabPath()
	return "assets/prefabs/ui/equip/equip_info_ui.prefab"
end

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

function EquipInfoUI:ctor(params)
	self.showType = GConst.EquipConst.INFO_SHOW_TYPE.BASE_ATTR
	if params.showType then
		self.showType = params.showType
	end
	params = params or {}
	self.heroEntity = params.heroEntity
	self.id = params.id                  --配置id
	self.uid = params.uid                --唯一id
	self.slotId = params.slotId          --上阵槽位
end

function EquipInfoUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["equip_info_ui.mask"]:addClickListener(function ()
		self:closeUI()
	end)
	uiMap["equip_info_ui.bg.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self.equipCell = uiMap["equip_info_ui.bg.equip_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.EQUIP_CELL)
	self.bg1 = uiMap["equip_info_ui.bg.bg1"]
	self.nameTx = uiMap["equip_info_ui.bg.name_tx"]
	self.partTx = uiMap["equip_info_ui.bg.part_desc_tx.part_tx"]
	self.powerTx = uiMap["equip_info_ui.bg.power_desc_tx.power_tx"]
	self.attrNameTx = uiMap["equip_info_ui.bg.attr_base.attr_name_tx"]
	self.attrValueTx = uiMap["equip_info_ui.bg.attr_base.attr_value_tx"]
	self.attrValueAddTx = uiMap["equip_info_ui.bg.attr_base.attr_value_add_tx"]
	self.attrExtraCells = {}
	for i = 1, 4 do
		local attrExtraCell = uiMap["equip_info_ui.bg.attr_extra.list.attr_extra_cell_"..i]
		local point = uiMap["equip_info_ui.bg.attr_extra.list.attr_extra_cell_"..i..".point"]
		local attrNameTx = uiMap["equip_info_ui.bg.attr_extra.list.attr_extra_cell_"..i..".attr_name_tx"]
		local attrValueTx = uiMap["equip_info_ui.bg.attr_extra.list.attr_extra_cell_"..i..".attr_value_tx"]
		local attrValueAddTx = uiMap["equip_info_ui.bg.attr_extra.list.attr_extra_cell_"..i..".attr_value_add_tx"]
		table.insert(self.attrExtraCells, {attrExtraCell = attrExtraCell, point = point, attrNameTx = attrNameTx, attrValueTx = attrValueTx, attrValueAddTx = attrValueAddTx})
	end

	self.replaceBtn = uiMap["equip_info_ui.bg.replace_btn"]
	self.upBtn = uiMap["equip_info_ui.bg.up_btn"]

	uiMap["equip_info_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_8))
	uiMap["equip_info_ui.bg.part_desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_9))
	uiMap["equip_info_ui.bg.power_desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_10))
	uiMap["equip_info_ui.bg.replace_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_13))
	uiMap["equip_info_ui.bg.up_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_14))
	uiMap["equip_info_ui.bg.attr_base.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_11))
	uiMap["equip_info_ui.bg.attr_extra.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_12))

	self.replaceBtn:addClickListener(function()
		local list = DataManager.EquipData:getAllEquipsSort(self.part)
		if table.nums(list) == 0 then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_17))
			return
		end
		ModuleManager.EquipManager:showEquipListUI(self.heroEntity, self.part)
	end)
	self.upBtn:addClickListener(function()
		ModuleManager.EquipManager:showEquipGrowthUI(self.slotId, self.part)
	end)
	self:bind(DataManager.EquipData, "isDirty", function()
		self:closeUI()
	end)
end

function EquipInfoUI:onRefresh()
	if self.showType == GConst.EquipConst.INFO_SHOW_TYPE.PREVIEW then
		self:refreshPreview()
	elseif self.showType == GConst.EquipConst.INFO_SHOW_TYPE.BASE_ATTR then
		self:refreshBaseAttr()
	elseif self.showType == GConst.EquipConst.INFO_SHOW_TYPE.ALL_ATTR then
		self:refreshAllAttr()
	end
	self:refreshInfo()
end

function EquipInfoUI:refreshInfo()
	self.nameTx:setText(self.entity:getNameQltColor())
	self.partTx:setText(self.entity:getPartName())
	self.powerTx:setText(self.entity:getPower())
	local attr = self.entity:getBaseAttr()
	self.attrNameTx:setText(GFunc.getAttrNameByType(attr.type))
	self.attrValueTx:setText(GFunc.getAttrDesc(attr.type, attr.num))

	local meshProCompNext = self.attrValueAddTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
	local nowTipsNextX = meshProCompNext.preferredWidth
	self.attrValueAddTx:setSizeDeltaX(nowTipsNextX)
	local anchorX = - nowTipsNextX - 40
	self.attrValueTx:setAnchoredPositionX(anchorX)
end

function EquipInfoUI:refreshPreview()
	if self.id == nil then
		self:closeUI()
		return
	end
	self.entity = DataManager.EquipData:createEquipEntity({cfg_id = self.id})
	self.equipCell:refreshByCfg(self.id)
	self.attrValueAddTx:setText(GConst.EMPTY_STRING)
	for i,cell in ipairs(self.attrExtraCells) do
		cell.attrExtraCell:setActive(true)
		local extraAttr = DataManager.EquipData:getExtraAttr(self.id, i)
		cell.attrNameTx:setText(GFunc.getAttrDesc(extraAttr.type, extraAttr.minnum))
		cell.attrValueTx:setText(GConst.EMPTY_STRING)
		cell.attrValueAddTx:setActive(true)
		cell.attrValueAddTx:setText("<color=#2D3449>".. GFunc.getFinalAttrValue(extraAttr.type, extraAttr.minnum, 2) .. "-".. GFunc.getFinalAttrValue(extraAttr.type, extraAttr.maxnum, 2) .."</color>")
	end
	self.replaceBtn:setActive(false)
	self.upBtn:setActive(false)
	self.bg1:setSizeDeltaY(346)
end

function EquipInfoUI:refreshBaseAttr()
	self.entity = DataManager.EquipData:getEquipByUid(self.uid)
	self.equipCell:refreshByCfg(self.entity:getId())
	local attrs = self.entity:getExtraList()
	local count = #attrs
	for i, cell in ipairs(self.attrExtraCells) do
		if count >= i then
			cell.attrExtraCell:setActive(true)
			local extraAttr = attrs[i]
			cell.attrNameTx:setText(GFunc.getAttrDesc(extraAttr.type, extraAttr.num))
			cell.attrValueTx:setText(GFunc.getFinalAttrValue(extraAttr.type, extraAttr.num, 2))
			cell.attrValueTx:setAnchoredPositionX(-10)
			cell.attrValueAddTx:setActive(false)
		else
			cell.attrExtraCell:setActive(false)
		end
	end
	self.attrValueAddTx:setText(GConst.EMPTY_STRING)
	self.replaceBtn:setActive(false)
	self.upBtn:setActive(false)
	self.bg1:setSizeDeltaY(326)
end

function EquipInfoUI:refreshAllAttr()
	if self.uid == 0 then
		self:closeUI()
		return
	end
	self.entity = DataManager.EquipData:getEquipByUid(self.uid)
	if self.entity == nil then
		return
	end
	self.part = self.entity:getPart()
	self.equipCell:refresh(self.entity, self.slotId)
	self.level = DataManager.EquipData:getPartLv(self.slotId, self.part)
	self.refine = DataManager.EquipData:getPartRefine(self.slotId, self.part)
	local typeName = self.entity:getBaseAttr().type
	local resonateLv, resonateNextLv, resonateAttrs, resonateAttrNexts = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.LV_UP, self.slotId)
	local baseValueAdd = DataManager.EquipData:getBaseAttrLvAdd(typeName, self.level)-- + (resonateAttrs[typeName] or 0)
	if baseValueAdd <= 0 then
		self.attrValueAddTx:setText(GConst.EMPTY_STRING)
	else
		self.attrValueAddTx:setText("<color=#049500>(+".. GFunc.getFinalAttrValue(typeName, baseValueAdd) ..")</color>")
	end

	local attrs = self.entity:getExtraList()
	local count = #attrs
	for i, cell in ipairs(self.attrExtraCells) do
		if count >= i then
			cell.attrExtraCell:setActive(true)
			local extraAttr = attrs[i]
			-- local extraAttrId = GFunc.getAttrIdByName(extraAttr.type)
			cell.attrNameTx:setText(GFunc.getAttrNameByType(extraAttr.type))
			cell.attrValueTx:setText(GFunc.getFinalAttrValue(extraAttr.type, extraAttr.num, 2))
			local attrAdd = DataManager.EquipData:getRefineAttrAdd(self.refine, extraAttr.type)
			local anchorX = -40
			if attrAdd.num <= 0 then
				cell.attrValueAddTx:setActive(false)
			else
				cell.attrValueAddTx:setActive(true)
				cell.attrValueAddTx:setText("<color=#049500>(+".. GFunc.getFinalAttrValue(attrAdd.type, attrAdd.num, 2) ..")</color>")
				local meshProCompNext = cell.attrValueAddTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
				local nowTipsNextX = meshProCompNext.preferredWidth
				cell.attrValueAddTx:setSizeDeltaX(nowTipsNextX)
				anchorX = anchorX - nowTipsNextX - 15
			end
			cell.attrValueTx:setAnchoredPositionX(anchorX)
		else
			cell.attrExtraCell:setActive(false)
		end
	end
	self.replaceBtn:setActive(true)
	self.upBtn:setActive(true)
	self.bg1:setSizeDeltaY(326)
end

return EquipInfoUI