local EquipResolveUI = class("EquipResolveUI", BaseUI)


function EquipResolveUI:onClose()
end
function EquipResolveUI:isFullScreen()
	return false
end
function EquipResolveUI:ctor()
	self.isOpenPartSelect = false
	self.isOpenQltSelect = false
	
	self.selectedPart = 0
	self.selectedQlt = 0
	self.selectEquips = {}
end

function EquipResolveUI:getPrefabPath()
	return "assets/prefabs/ui/equip/equip_resolve_ui.prefab"
end

function EquipResolveUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["equip_resolve_ui.mask"]:addClickListener(function()
		self:closeUI()
	end)
	uiMap["equip_resolve_ui.bg.close_btn"]:addClickListener(function()
		self:closeUI()
	end)
	self.titleTx = uiMap["equip_resolve_ui.bg.title_tx"]
	self.bg1 = uiMap["equip_resolve_ui.bg.bg_1"]
	self.bg2 = uiMap["equip_resolve_ui.bg.bg_2"]
	self.scrollrect = uiMap["equip_resolve_ui.bg.scrollrect"]
	self.listPart = uiMap["equip_resolve_ui.bg.list_part"]
	self.listPartBtns = {}
	for i = 0, 6 do
		local partBtn = uiMap["equip_resolve_ui.bg.list_part.scrollrect.viewport.content.part_btn_" .. i]
		partBtn:addClickListener(function()
			self:selectPart(i)
		end)
		local partTx = uiMap["equip_resolve_ui.bg.list_part.scrollrect.viewport.content.part_btn_" .. i .. ".text"]
		if i == 0 then
			partTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_QLT_SELECT_ALL_2))
		else
			partTx:setText(I18N:getGlobalText(GConst.EquipConst.EQUIP_PART_NAME[i]))
		end
		table.insert(self.listPartBtns, {partBtn = partBtn, partTx = partTx})
	end
	self.partBtn = uiMap["equip_resolve_ui.bg.part_btn"]
	self.partTx = uiMap["equip_resolve_ui.bg.part_btn.text"]
	self.listQlt = uiMap["equip_resolve_ui.bg.list_qlt"]
	self.listBtnQlts = {}
	for i = 0, 5 do
		local qltBtn = uiMap["equip_resolve_ui.bg.list_qlt.scrollrect.viewport.content.qlt_btn_" .. i]
		qltBtn:addClickListener(function()
			self:selectQlt(i)
		end)
		local qltBtnTx = uiMap["equip_resolve_ui.bg.list_qlt.scrollrect.viewport.content.qlt_btn_" .. i .. ".text"]
		self.listBtnQlts[i] = {qltBtn = qltBtn, qltBtnTx = qltBtnTx}
	end
	self.qltBtn = uiMap["equip_resolve_ui.bg.qlt_btn"]
	self.qltBtnTx = uiMap["equip_resolve_ui.bg.qlt_btn.text"]
	self.cost = uiMap["equip_resolve_ui.bg.cost"]
	self.costTx = uiMap["equip_resolve_ui.bg.cost.cost_tx"]
	self.selectAllBtn = uiMap["equip_resolve_ui.bg.select_all_btn"]
	self.selectAllBtnTx = uiMap["equip_resolve_ui.bg.select_all_btn.text"]
	self.resolveBtn = uiMap["equip_resolve_ui.bg.resolve_btn"]
	self.emptyTx = uiMap["equip_resolve_ui.bg.empty_tx"]

	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_24))
	uiMap["equip_resolve_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_28))
	uiMap["equip_resolve_ui.bg.resolve_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_30))
	uiMap["equip_resolve_ui.bg.empty_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_40))

	self.partBtn:addClickListener(function()
		self:openPartSelect()
	end)
	self.qltBtn:addClickListener(function()
		self:openQltSelect()
	end)
	self.selectAllBtn:addClickListener(function()
		if table.nums(self.selectEquips) < table.nums(self.listEquip) then
			for i,v in ipairs(self.listEquip) do
				self.selectEquips[v:getUid()] = v:getUid()
			end
		else
			self.selectEquips = {}
		end
		self:refreshScrollRect()
	end)
	self.resolveBtn:addClickListener(function()
		if table.nums(self.selectEquips) == 0 then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_31))
			return
		end
		local params = {}
		params.boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL_CLOSE
		params.content = I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_48)
		params.okFunc = function()
			local listUid = {}
			for i,uid in pairs(self.selectEquips) do
				table.insert(listUid, uid)
			end
			ModuleManager.EquipManager:onEquipDecomposeReq(listUid)
		end
		GFunc.showMessageBox(params)
		
	end)
	self:bind(DataManager.EquipData, "isDirty", function()
		self:onRefresh()
	end)
	self:bind(DataManager.EquipData, "isDirtyResolve", function()
		self.selectEquips = {}
		self.selectedPart = 0
		self.selectedQlt = 0
		self:onRefresh()
	end)
end

function EquipResolveUI:onRefresh()
	self.lowestFiveGrades, self.lowestFiveEquips = DataManager.EquipData:getLowestFiveGradeEquips()
	for i = 0, 5 do
		if i == 0 then
			self.listBtnQlts[i].qltBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_QLT_SELECT_ALL_1))
		else
			if self.lowestFiveGrades[i] then
				self.listBtnQlts[i].qltBtn:setActive(true)
				self.listBtnQlts[i].qltBtnTx:setText(I18N:getGlobalText("EQUIP_QLT_SELECT", I18N:getGlobalText("EQUIP_QLT_DESC_" .. self.lowestFiveGrades[i].qlt, self.lowestFiveGrades[i].star)))
			else
				self.listBtnQlts[i].qltBtn:setActive(false)
			end
		end
	end

	self:refreshScrollRect()
	self:refreshText()
end

function EquipResolveUI:refreshSelectEquip()
	for i,uid in pairs(self.selectEquips) do
		local shouldRemove = false
		local equip = DataManager.EquipData:getEquipByUid(uid)
		if self.selectedPart ~= 0 then
			if equip:getPart() ~= self.selectedPart then
				shouldRemove = true
			end
		end
		if not shouldRemove and self.selectedQlt ~= 0 then
			if equip:getQlt() > self.lowestFiveGrades[self.selectedQlt].qlt then
				-- 品质高于要求
				shouldRemove = true
			elseif equip:getQlt() == self.lowestFiveGrades[self.selectedQlt].qlt then
				-- 同品质时检查星级
				if equip:getStar() > self.lowestFiveGrades[self.selectedQlt].star then
					shouldRemove = true
				end
			end
		end
		if shouldRemove then
			self.selectEquips[uid] = nil
		end
	end
end

function EquipResolveUI:openPartSelect()
	if self.isOpenPartSelect then
		self.isOpenPartSelect = false
	else
		self.isOpenPartSelect = true
		self.listPart:setActive(true)
		if self.isOpenQltSelect then
			self:openQltSelect()
		end
	end
	if self.partSeq then
		self.partSeq:Kill()
		self.partSeq = nil
	end
	self.partSeq = DOTweenManager:createSeqWithIntId()
	local startSize = self.listPart:getSizeDeltaY()
	local endSize = self.isOpenPartSelect and 420 or 0
	local tween = CS.DG.Tweening.DOTween.To(
		function()
			return startSize
		end,
		function(y)
			self.listPart:setSizeDeltaY(y)
		end,
	endSize, 0.3)
	tween:SetEase(CS.DG.Tweening.Ease.InOutCirc)
	self.partSeq:Append(tween)
	self.partSeq:AppendCallback(function()
		if self.partSeq then
			self.partSeq:Kill()
			self.partSeq = nil
		end
		if not self.isOpenPartSelect then
			self.listPart:setActive(false)
		end
	end)
end

function EquipResolveUI:openQltSelect()
	if self.isOpenQltSelect then
		self.isOpenQltSelect = false
	else
		self.isOpenQltSelect = true
		self.listQlt:setActive(true)
		if self.isOpenPartSelect then
			self:openPartSelect()
		end
	end
	if self.qltSeq then
		self.qltSeq:Kill()
		self.qltSeq = nil
	end
	self.qltSeq = DOTweenManager:createSeqWithIntId()
	local startSize = self.listQlt:getSizeDeltaY()
	local endSize = self.isOpenQltSelect and 368 or 0
	local tween = CS.DG.Tweening.DOTween.To(
		function()
			return startSize
		end,
		function(y)
			self.listQlt:setSizeDeltaY(y)
		end,
	endSize, 0.3)
	tween:SetEase(CS.DG.Tweening.Ease.InOutCirc)
	self.qltSeq:Append(tween)
	self.qltSeq:AppendCallback(function()
		if self.qltSeq then
			self.qltSeq:Kill()
			self.qltSeq = nil
		end
		if not self.isOpenQltSelect then
			self.listQlt:setActive(false)
		end
	end)
end

function EquipResolveUI:selectPart(part)
	if self.isOpenPartSelect then
		self:openPartSelect()
	end
	self.selectedPart = part
	self:refreshScrollRect()
	self:refreshText()
end

function EquipResolveUI:selectQlt(qlt)
	if self.isOpenQltSelect then
		self:openQltSelect()
	end
	self.selectedQlt = qlt
	self:refreshScrollRect()
	self:refreshText()
end

function EquipResolveUI:refreshText()
	if self.selectedPart == 0 then
		self.partTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_QLT_SELECT_ALL_2))
	else
		self.partTx:setText(I18N:getGlobalText(GConst.EquipConst.EQUIP_PART_NAME[self.selectedPart]))
	end
	if self.selectedQlt == 0 then
		self.qltBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_QLT_SELECT_ALL_1))
	else
		self.qltBtnTx:setText(I18N:getGlobalText("EQUIP_QLT_SELECT", I18N:getGlobalText("EQUIP_QLT_DESC_" .. self.lowestFiveGrades[self.selectedQlt].qlt, self.lowestFiveGrades[self.selectedQlt].star)))
	end
end

-- 新增方法：根据选中的品质+星级，获取该等级及更低的所有装备
function EquipResolveUI:getEquipsByGradeAndBelow(selectedPart, selectedQlt, selectedStar)
	-- 2. 筛选符合条件的装备
	local resultEquips = {}
	for _, group in ipairs(self.lowestFiveEquips) do
		-- 判断是否低于或等于选中等级
		if group:getQlt() < selectedQlt or (group:getQlt() == selectedQlt and group:getStar() <= selectedStar) then
			if selectedPart == 0 or (selectedPart > 0 and group:getPart() == selectedPart) then
				table.insert(resultEquips, group)
			end
		end
	end
	return resultEquips
end

function EquipResolveUI:refreshScrollRect()
	if self.selectedQlt == 0 then
		self.listEquip = DataManager.EquipData:getAllEquipsSortInverted(self.selectedPart, nil, nil, false)
	else
		self.listEquip = self:getEquipsByGradeAndBelow(self.selectedPart, self.lowestFiveGrades[self.selectedQlt].qlt, self.lowestFiveGrades[self.selectedQlt].star)
	end
	self.emptyTx:setActive(#self.listEquip <= 0)
	if self.scrollRect == nil then
		self.scrollRect = self.scrollrect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
		self.scrollRect:addInitCallback(function()
			return GConst.TYPEOF_LUA_CLASS.EQUIP_CELL
		end)
		self.scrollRect:addRefreshCallback(function(index, cell)
			local isSelect = self.selectEquips[self.listEquip[index]:getUid()] ~= nil
			cell:refresh(self.listEquip[index])
			cell:showMask(isSelect)
			cell:showCheck(isSelect)
			cell:addClickListener(function()
				if self.selectEquips[self.listEquip[index]:getUid()] then
					self.selectEquips[self.listEquip[index]:getUid()] = nil
					cell:showCheck(false)
					cell:showMask(false)
				else
					self.selectEquips[self.listEquip[index]:getUid()] = self.listEquip[index]:getUid()
					cell:showCheck(true)
					cell:showMask(true)
				end
				self:refreshReward()
			end)
		end)
	end
	if self.scrollRect:getTotalCount() == nil or self.scrollRect:getTotalCount() <= 0 then
		self.scrollRect:refillCells(#self.listEquip)
	elseif self.scrollRect:getTotalCount() ~= #self.listEquip then
		self.scrollRect:clearCells()
		self.scrollRect:refillCells(#self.listEquip)
	else
		self.scrollRect:updateAllCell()
	end
	self:refreshReward()
end

function EquipResolveUI:refreshReward()
	self:refreshSelectEquip()
	if table.nums(self.selectEquips) < table.nums(self.listEquip) then
		self.selectAllBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_29))
	else
		self.selectAllBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_21))
	end
	local count = 0
	for i,uid in pairs(self.selectEquips) do
		local equip = DataManager.EquipData:getEquipByUid(uid)
		local decompose = DataManager.EquipData:getResolveReward(equip:getId())
		count = count + decompose.num
	end
	self.costTx:setText(count)
end

return EquipResolveUI