otstd.equip_item = {}

otstd.equip_item.items = {

		-- Helmet
		[5461] = {slot = "head"},
		[2474] = {slot = "head"},
		[2343] = {slot = "head"},
		[2323] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7900] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7901] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7902] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7903] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[8820] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[9778] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[10016] = {slot = "head", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		
		--Amulet
		[2161] = {slot = "necklace"},
		[2170] = {slot = "necklace"},
		[2172] = {slot = "necklace"},
		[2197] = {slot = "necklace"},
		[2198] = {slot = "necklace"},
		[2199] = {slot = "necklace"},
		[2200] = {slot = "necklace"},
		[2201] = {slot = "necklace"},
		[2173] = {slot = "necklace"},
		[7887] = {slot = "necklace", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7888] = {slot = "necklace", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7889] = {slot = "necklace", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7890] = {slot = "necklace", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },

		--Feet
		[2195] = {slot = "feet"},
		[6132] = {slot = "feet"},
		[2640] = {slot = "feet"},
		[7886] = {slot = "feet", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7892] = {slot = "feet", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7891] = {slot = "feet", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[7893] = {slot = "feet", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[9932] = {slot = "feet", level = 130},
		[9933] = {slot = "feet", level = 130},

		-- Ring
		[2164] = {slot = "ring"},
		[2165] = {slot = "ring"},
		[2202] = {slot = "ring"},
		[2166] = {slot = "ring"},
		[2203] = {slot = "ring"},
		[2167] = {slot = "ring"},
		[2204] = {slot = "ring"},
		[2168] = {slot = "ring"},
		[2205] = {slot = "ring"},
		[2169] = {slot = "ring"},
		[2206] = {slot = "ring"},
		[2207] = {slot = "ring"},
		[2210] = {slot = "ring"},
		[2208] = {slot = "ring"},
		[2211] = {slot = "ring"},
		[2209] = {slot = "ring"},
		[2212] = {slot = "ring"},
		[2213] = {slot = "ring"},
		[2215] = {slot = "ring"},
		[2214] = {slot = "ring"},
		[2216] = {slot = "ring"},
		[6300] = {slot = "ring"},
		[6301] = {slot = "ring"},

		-- Armor
		[2472] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"} },
		[2487] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"} },
		[2492] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"} },
		[2466] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"} },
		[2476] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"} },
		[2500] = {slot = "armor", vocations = {"Paladin", "Royal Paladin"}, level = 60 },
		[7884] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 50 },
		[7897] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 50 },
		[7898] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 50 },
		[7899] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 50 },
		[8819] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[8821] = {slot = "armor", level = 50},
		[8865] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer"}, level=65 },
		[8866] = {slot = "armor", vocations = {"Druid", "Elder Druid"} },
		[8867] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer"} },
		[8868] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer"} },
		[8869] = {slot = "armor", vocations = {"Druid", "Elder Druid"}, level = 75 },
		[8870] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[8871] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}  },
		[8872] = {slot = "armor", vocations = {"Paladin", "Royal Paladin"} },
		[8877] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"}, level = 60 },
		[8878] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"}, level = 60 },
		[8879] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"}, level = 60 },
		[8880] = {slot = "armor", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"}, level = 60 },
		[8881] = {slot = "armor", vocations = {"Knight", "Elite Knight"}, level = 90 },
		[8882] = {slot = "armor", vocations = {"Knight", "Elite Knight"}, level = 90 },
		[8883] = {slot = "armor", vocations = {"Knight", "Elite Knight"}, level = 90 },
		[8884] = {slot = "armor", vocations = {"Knight", "Elite Knight"}, level = 90 },
		[8885] = {slot = "armor", vocations = {"Paladin", "Royal Paladin"}, level = 75  },
		[8886] = {slot = "armor", vocations = {"Paladin", "Royal Paladin"}, level = 75  },
		[8887] = {slot = "armor", vocations = {"Paladin", "Royal Paladin"}, level = 75  },
		[8888] = {slot = "armor", vocations = {"Paladin", "Royal Paladin"}, level = 100  },
		[8889] = {slot = "armor", vocations = {"Knight", "Elite Knight"}, level = 100 },
		[8890] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer"}, level = 100 },
		[8891] = {slot = "armor", vocations = {"Paladin", "Royal Paladin"} },
		[8892] = {slot = "armor", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"} },
		[9776] = {slot = "armor", vocations = {"Knight", "Elite Knight"}, level = 80 },

		--Leg
		[2477] = {slot = "legs", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"} },
		[2488] = {slot = "legs", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"}  },
		[2470] = {slot = "legs", vocations = {"Knight", "Elite Knight", "Paladin", "Royal Paladin"}  },
		[7885] = {slot = "legs", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 40 },
		[7894] = {slot = "legs", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 40 },
		[7895] = {slot = "legs", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 40 },
		[7896] = {slot = "legs", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 40 },
		[9777] = {slot = "legs", vocations = {"Paladin", "Royal Paladin"}, level = 80 },

		--Shield
		[8905] = {slot = "hand", vocations = {"Knight", "Elite Knight"}, level = 100 },
		[8906] = {slot = "hand", vocations = {"Knight", "Elite Knight"}, level = 100 },
		[8907] = {slot = "hand", vocations = {"Knight", "Elite Knight"}, level = 100 },
		[8908] = {slot = "hand", vocations = {"Knight", "Elite Knight"}, level = 100 },
		[8909] = {slot = "hand", vocations = {"Knight", "Elite Knight"}, level = 100 },
		[8900] = {slot = "hand", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 30 },
		[8901] = {slot = "hand", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 40 },
		[8902] = {slot = "hand", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 50 },
		[8903] = {slot = "hand", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 60 },
		[8904] = {slot = "hand", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 70 },
		[8918] = {slot = "hand", vocations = {"Sorcerer", "Master Sorcerer", "Druid", "Elder Druid"}, level = 80 },
		[9956] = {slot = "hand"}
	}

function otstd.equip_item.defaultEquipHandler(event, isEquip)
	local player = event.player
	local item = event.item
	local newid = event.newid or item:getItemID()
	item:setItemID(event.newid)
end

function otstd.equip_item.equip_handler(event, isEquip)
	return event.handler and event.handler(event, isEquip) or otstd.equip_item.defaultEquipHandler(event, isEquip)
end

function otstd.equip_item.query_equip_handler(event)
	local player = event.player
	local level = event.level
	local vocations = event.vocations
	
	if level and player:getLevel() < level then
		player:sendCancel("You do not meet the level proficiency to wear this item.")
		event:skip()
	end
	
	if vocations and not table.find(vocations, player:getVocationName())  then
		player:sendCancel("You do not meet the vocation proficiency to wear this item.")
		event:skip()
	end
	
	return true
end

function otstd.equip_item.registerHandlers()
	for id, data in pairs(otstd.equip_item.items) do

		local equipid = id
		local equiptype = Items[equipid]
		local equippedid = (equiptype.transformEquipTo > 0 and equiptype.transformEquipTo) or id
		local equippedtype = Items[equippedid]

		if data.equip_listener then
			stopListener(data.equip_listener)
		end
		
		local function lamba_equip_callback(event)
			event.newid = equippedid
			event.itemtype = equippedtype
			event.handler = data.handler
			otstd.equip_item.equip_handler(event, true)
		end

		data.equip_listener = registerOnEquipItem("itemid", id, SlotPosition(data.slot), lamba_equip_callback)

		if data.equip_query_listener then
			stopListener(data.equip_query_listener)
		end
		
		if data.vocations or data.level then
			-- Register that we want events before the item is equipped to check vocation and level requirements
			local function lamba_query_equip_callback(event)
				event.vocation = data.vocations
				event.level = data.level
				event.handler = data.handler
				otstd.equip_item.query_equip_handler(event)
			end

			data.equip_query_listener = registerOnEquipItem("before", "itemid", id, SlotPosition(data.slot), lamba_query_equip_callback)
		end

		if data.deequip_listener then
			stopListener(data.deequip_listener)
		end

		local function lamba_deequip_callback(event)
			event.newid = equipid
			event.itemtype = equiptype			
			event.handler = data.handler
			otstd.equip_item.equip_handler(event, false)
		end
		
		data.deequip_listener = registerOnDeEquipItem("itemid", equippedid, SlotPosition(data.slot), lamba_deequip_callback)
	end
end

otstd.equip_item.registerHandlers()

