local EquipDataOther = class("EquipDataOther", BaseData)
local EquipEntityOther = require "app/userdata/equip/equip_entity_other"

function EquipDataOther:ctor()
end

function EquipDataOther:clear()
	self.allEquips = {}
end

function EquipDataOther:init(data)
	data = data or GConst.EMPTY_TABLE
	if EDITOR_MODE then
		Logger.logHighlight("装备数据")
		Logger.printTable(data)
	end

	self.allEquips = {}
	self.equipLvs = {}
	self.equipRefineLvs = {}
	for slotId = 1, 5 do
		for part = 1, 6 do
			if data[slotId] and data[slotId][part] then
				local uid = slotId * 100 + part
				local equip = EquipEntityOther:create(data[slotId][part])
				self.allEquips[uid] = equip
				self.equipLvs[uid] = data[slotId][part].level
				self.equipRefineLvs[uid] = data[slotId][part].refine
			end
		end
	end
end

--@region 属性
function EquipDataOther:getResonateLevelValue(type, value, value2)
	local list = DataManager.EquipData:getResonateList(type)
	local lv = 0
	local nextLv = 0
	local attrs = {}
	local attrNexts = {}
	for i,v in ipairs(list) do
		if type == 1 or type == 3 then
			if v.parameter[1] <= value then
				lv = i
				for _, vv in ipairs(v.attr) do
					attrs[vv.type] = (attrs[vv.type] or 0) + vv.num
				end
			else
				nextLv = v.parameter
				for _, vv in ipairs(v.attr) do
					attrNexts[vv.type] = (attrNexts[vv.type] or 0) + vv.num
				end
				break
			end
		elseif type == 2 then
			local isTrue = false
			if v.parameter[1] < value then
				isTrue = true
			elseif v.parameter[1] == value and v.parameter[2] <= value2 then
				isTrue = true
			end
			if isTrue then
				lv = i
				for _, vv in ipairs(v.attr) do
					attrs[vv.type] = (attrs[vv.type] or 0) + vv.num
				end
			else
				nextLv = v.parameter
				for _, vv in ipairs(v.attr) do
					attrNexts[vv.type] = (attrNexts[vv.type] or 0) + vv.num
				end
				break
			end
		end
	end
	return lv, nextLv, attrs, attrNexts, table.nums(list)
end

function EquipDataOther:getResonateLevel(type, slotId)
	if type == 1 then
		local minLv = 999
		for i = 1, 6 do
			local uid = slotId * 100 + i
			local level = self.equipLvs[uid] or 0
			if level < minLv then
				minLv = level
			end
		end
		return self:getResonateLevelValue(type, minLv)
	elseif type == 2 then
		local qlt = 99
		local star = 99
		for i = 1, 6 do
			local uid = slotId * 100 + i
			local equip = self.allEquips[uid]
			if equip == nil then
				qlt = 0
				star = 0
				break
			end
			local q = equip:getQlt()
			local s = equip:getStar()
			if qlt > q then
				qlt = q
				star = s
			elseif qlt == q then
				if star > s then
					qlt = q
					star = s
				end
			end
		end
		return self:getResonateLevelValue(type, qlt, star)
	elseif type == 3 then
		local minRefine = 999
		for i = 1, 6 do
			local uid = slotId * 100 + i
			local refine = self.equipRefineLvs[uid] or 0
			if refine < minRefine then
				minRefine = refine
			end
		end
		return self:getResonateLevelValue(type, minRefine)
	end
end

function EquipDataOther:getEquipAttrBySlotId(slotId)
	local allAttr = {}
	for part = 1, 6 do
		local uid = slotId * 100 + part
		local equip = self.allEquips[uid]
		if equip then
			local attr = equip:getAllAttr()
			for k, v in pairs(attr) do
				allAttr[k] = (allAttr[k] or 0) + v
			end

			-- 升级属性
			local level = self.equipLvs[uid] or 0
			local baseType = equip:getBaseAttr().type
			allAttr[baseType] = (attr[baseType] or 0) + DataManager.EquipData:getBaseAttrLvAdd(baseType, level)

			-- 精炼属性
			local equipAttrs = equip:getExtraMap()
			local refine = self.equipRefineLvs[uid] or 0
			if refine > 0 then
				for _, extraAttr in pairs(equipAttrs) do
					local attrName = GFunc.getAttrNameById(extraAttr.id)
					local nowAttr = DataManager.EquipData:getRefineAttrAdd(refine, attrName)
					allAttr[attrName] = (allAttr[attrName] or 0) + nowAttr.num
				end
			end
		end
	end

	-- 共鸣
	for i = 1, 3 do
		local lv = self:getResonateLevel(i, slotId)
		local list = DataManager.EquipData:getResonateList(i)
		for ii, vv in ipairs(list) do
			if ii <= lv then
				for _, vvv in ipairs(vv.attr or {}) do
					allAttr[vvv.type] = (allAttr[vvv.type] or 0) + vvv.num
				end
			else
				break
			end
		end
	end

	return allAttr
end
--@endregion
return EquipDataOther