local EquipManager = class("EquipManager", BaseModule)

function EquipManager:showEquipListUI(heroEntity, part)
	UIManager:showUI("app/ui/equip/equip_list_ui", {heroEntity = heroEntity, part = part})
end

function EquipManager:showEquipGrowthUI(slotId, part)
	UIManager:showUI("app/ui/equip/equip_growth_ui", {slotId = slotId, part = part})
end

function EquipManager:showEquipInfoUI(params)
	local uiObj = UIManager:checkOpen(UIManager.UI_PATH.EQUIP_INFO_UI)
	if uiObj then
		uiObj:ctor(params)
		uiObj:onRefresh()
	else
		UIManager:showUI(UIManager.UI_PATH.EQUIP_INFO_UI, params)
	end
end

-- 共鸣
function EquipManager:showEquipResonateUI(slotId, page)
	UIManager:showUI("app/ui/equip/equip_resonate_ui", {slotId = slotId, page = page})
end

-- 分解弹窗（批量分解）
function EquipManager:showEquipResolveUI()
	UIManager:showUI("app/ui/equip/equip_resolve_ui")
end

-- 套装效果弹窗
function EquipManager:showEquipGetResonateUI(type, lvNew, attrs, attrNexts)
	local params = {
		type = type,
		lvNew = lvNew,
		attrs = attrs,
		attrNexts = attrNexts,
	}
	UIManager:showUI("app/ui/equip/equip_get_resonate_ui", params)
end

--@region 协议
-- 装备穿戴
function EquipManager:onEquipWearReq(slotId, ids)
	if table.nums(ids) <= 0 then
		return
	end
	local params = {}
	params.slot = slotId
	params.ids = ids
	self:sendMessage(ProtoMsgType.FromMsgEnum.EquipWearReq, params, self.onEquipWearRsp, BIReport.ITEM_GET_TYPE.EQUIP_WEAR)
end

function EquipManager:onEquipWearRsp(result)
	if result.err_code == GConst.ERROR_STR.SUCCESS then
		local lvOld, nextLv, attrs = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.QLT, result.reqData.slot)
		DataManager.EquipData:onWearSuccess(result.reqData.slot, result.reqData.ids)
		DataManager.HeroData:setAllHeroesDitry()
		DataManager.HeroData:setDirty()
		DataManager.HeroData:calcPower()
		local lvNew = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.QLT, result.reqData.slot)
		if lvOld < lvNew then
			local _, _, attrNexts = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.QLT, result.reqData.slot)
			self:showEquipGetResonateUI(GConst.EquipConst.RESONATE_PAGE.QLT, lvNew, attrs, attrNexts)
			-- DataManager.GiftPopData:activeEquipResonancePopGift(GConst.EquipConst.RESONATE_PAGE.QLT, lvNew)
		end
	end
end

-- 装备升级
function EquipManager:onEquipUpgradeReq(slotId, part)
	local lv = DataManager.EquipData:getPartLv(slotId, part)
	local itemCost = DataManager.EquipData:getLevelCost(lv + 1)
	if not GFunc.checkCost(itemCost.id, itemCost.num, true) then
		return
	end
	local params = {}
	params.slot = slotId
	params.part = part
	self:sendMessage(ProtoMsgType.FromMsgEnum.EquipUpgradeReq, params, self.onEquipUpgradeRsp, BIReport.ITEM_GET_TYPE.EQUIP_LV_UP)
end

function EquipManager:onEquipUpgradeRsp(result)
	if result.err_code == GConst.ERROR_STR.SUCCESS then
		local lvOld, nextLv, attrs = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.LV_UP, result.reqData.slot)
		DataManager.EquipData:onUpgradeSuccess(result.reqData.slot, result.upgraded)
		DataManager.HeroData:setAllHeroesDitry()
		DataManager.HeroData:setDirty()
		DataManager.HeroData:calcPower()
		local lvNew = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.LV_UP, result.reqData.slot)
		if lvOld < lvNew then
			local _, _, attrNexts = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.LV_UP, result.reqData.slot)
			self:showEquipGetResonateUI(GConst.EquipConst.RESONATE_PAGE.LV_UP, lvNew, attrs, attrNexts)
			-- DataManager.GiftPopData:activeEquipResonancePopGift(GConst.EquipConst.RESONATE_PAGE.LV_UP, lvNew)
		end
	end
end

-- 装备精炼
function EquipManager:onEquipRefineReq(slotId, part)
	local refine = DataManager.EquipData:getPartRefine(slotId, part)
	local itemCost = DataManager.EquipData:getRefineCost(refine + 1)
	if not GFunc.checkCost(itemCost.id, itemCost.num, true) then
		return
	end
	local params = {}
	params.slot = slotId
	params.part = part
	self:sendMessage(ProtoMsgType.FromMsgEnum.EquipRefineReq, params, self.onEquipRefineRsp, BIReport.ITEM_GET_TYPE.EQUIP_REFINE)
end

function EquipManager:onEquipRefineRsp(result)
	if result.err_code == GConst.ERROR_STR.SUCCESS then
		local lvOld, nextLv, attrs = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.REFINE, result.reqData.slot)
		DataManager.EquipData:onRefineSuccess(result.reqData.slot, result.reqData.part, result.part)
		DataManager.HeroData:setAllHeroesDitry()
		DataManager.HeroData:setDirty()
		DataManager.HeroData:calcPower()
		local lvNew = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.REFINE, result.reqData.slot)
		if lvOld < lvNew then
			local _, _, attrNexts = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.REFINE, result.reqData.slot)
			self:showEquipGetResonateUI(GConst.EquipConst.RESONATE_PAGE.REFINE, lvNew, attrs, attrNexts)
			-- DataManager.GiftPopData:activeEquipResonancePopGift(GConst.EquipConst.RESONATE_PAGE.REFINE, lvNew)
		end
	end
end

-- 装备分解
function EquipManager:onEquipDecomposeReq(ids)
	self:sendMessage(ProtoMsgType.FromMsgEnum.EquipDecomposeReq, {ids = ids}, self.onEquipDecomposeRsp, BIReport.ITEM_GET_TYPE.EQUIP_RESOLVE)
end

function EquipManager:onEquipDecomposeRsp(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	GFunc.showRewardBox(result.rewards, {customTitleStr = I18N:getGlobalText(I18N.GlobalConst.REBACK_MATERIAL_DESC)})

	DataManager.EquipData:onResolveSuccess(result.reqData.ids)

	-- ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.EQUIP_RESOLVE_NUM, #result.reqData.ids)

	BIReport:postEquipResolveOpt(BIReport.EQUIP_OP_TYPE.RESOLVE, result.reqData.ids, result.rewards)
end
--@endregion
return EquipManager