require 'TimedActions/ISUnequipAction.lua'
require 'TimedActions/ISWearClothing.lua'
require 'TimedActions/ISEquipWeaponAction.lua'
require 'TimedActions/ISInventoryTransferAction.lua'

Kinkocalypse = Kinkocalypse or {}
Excrementum = Excrementum or {}

Kinkocalypse.ISUnequipActionOrigPerform = ISUnequipAction.perform
Kinkocalypse.ISInventoryTransferActionOrigPerform = ISInventoryTransferAction.perform
Kinkocalypse.ISWearClothingActionOrigPerform = ISWearClothing.perform
Kinkocalypse.ISEquipWeaponActionOrigPerform = ISEquipWeaponAction.perform
Kinkocalypse.ExcrementumGetAllPantsGroinOrig = Excrementum.GetAllPantsGroin

Kinkocalypse.ClothesAboutToChangeOrig = nil
if ClothesAboutToChange then Kinkocalypse.ClothesAboutToChangeOrig = ClothesAboutToChange end

Kinkocalypse.IsHasTag = function(item, tag)
	local tagExist = false
	if item ~= nil then 	
		local tags = item:getTags()
		if tags == nil then return tagExist end
		for i = 0, tags:size() - 1 do
			if tags:get(i) == tag then
				tagExist = true
			end
		end
		if tag == "ChainedHands" and item:getModData().ChainedHands == true then tagExist = true end
		if tag == "ChainedHandsBack" and item:getModData().ChainedHandsBack == true then tagExist = true end
		if tag == "ChainedLegs" and item:getModData().ChainedLegs == true then tagExist = true end
	end
	return tagExist
end

Kinkocalypse.HasItem = function(itemName)
	if itemName == nil or itemName == "" then 
		return false
	else
		local inventory = getPlayer():getInventory()
		local items = inventory:FindAll(itemName)
		if items == nil then 
			return false
		end
		
		local count = items:size()
		if count > 0 then
			return true
		end	
	end
	return false
end

Kinkocalypse.HasKeysFromRestrains = function(item)
	if item == nil then return false
	else
		local inventory = getPlayer():getInventory()
		if inventory:contains("Kinkocalypse.RestrictionsMasterKey") then 
			return true
		end

		local items = inventory:FindAll("Kinkocalypse.RestrictionsKey")
		for i = 0, items:size() - 1 do
			local itm = items:get(i)
			if itm ~= nil then
				if itm:getModData().RestrainKeyData == item:getModData().RestrainKeyData then 
					return true
				end
			end
		end	
		return false
	end
	return false
end

-- excrementum compability
Excrementum.GetAllPantsGroin = function(player, is_male_pee, is_any)
	local pants = {}
	if ExcrementumGetAllPantsGroinOrig ~= nil then
		pants = Kinkocalypse.ExcrementumGetAllPantsGroinOrig(player, is_male_pee, is_any)
		if pants ~= nil then
			for i, v in ipairs(pants) do
				if v ~= nil then
					if Kinkocalypse.IsHasTag(v, "SteelRestriction") == true and Kinkocalypse.HasKeysFromRestrains(v) == false then
						table.remove(pants, i)
					end
				end
			end
		end
	end
	return pants
end

-- lifestyle compability
Kinkocalypse.ClothesAboutToChange = function(player, object, optiontype)
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()	
	if equipedTags["SteelRestriction"] ~= nil then return 
	elseif ClothesAboutToChangeOrig ~= nil then ClothesAboutToChangeOrig(player, object, optiontype) end
end

Kinkocalypse.IsRestrainHasKeyData = function(item)
	local keyData = item:getModData().RestrainKeyData
	return keyData ~= nil
end

Kinkocalypse.WeldLock = function(item)
	if Kinkocalypse.IsRestrainHasKeyData(item) == true then
		local wledData = item:getModData().RestrainKeyData
		if wledData ~= -999 then
			item:getModData().RestrainKeyData = -999
			--item:setName(item:getDisplayName()..getText("UI_Kincalypse_Welded"))
		end
	end
end

Kinkocalypse.SetRestrainKeyData = function(item)
	if Kinkocalypse.IsRestrainHasKeyData(item) == false then
		local keyData = ZombRand(1, 999)
		item:getModData().RestrainKeyData = keyData
		if ZombRand(100) > 50 then
			Kinkocalypse.WeldLock(item)
			return
		end
		--item:setName(item:getDisplayName().." #"..keyData)
	end
end

-- has chains keys
Kinkocalypse.HasChainsKey = function(item)
	if item == nil then return false
	else
		local inventory = getPlayer():getInventory()
		if inventory:contains("Kinkocalypse.RestrictionsMasterKey") then 
			return true
		end
		
		local items = inventory:FindAll("Kinkocalypse.ChainsKey")
		for i = 0, items:size() - 1 do
			local itm = items:get(i)
			if itm ~= nil then
				local chainData = itm:getModData().ChainsKeyData
				local keyData = item:getModData().ChainsKeyData
				if chainData == keyData then 
					return true
				end
			end
		end	
		return false
	end
	return false
end

-- add chains
Kinkocalypse.AddChainsToItem = function(item)
	local keyData = ZombRand(1, 999)
	local inventory = getPlayer():getInventory()
	local keys = inventory:AddItem("Kinkocalypse.ChainsKey")
	item:getModData().ChainsKeyData = keyData
	keys:getModData().ChainsKeyData = keyData
	--keys:setName(keys:getDisplayName().." ("..item:getDisplayName()..")")
	inventory:RemoveOneOf("Base.Padlock")
end

-- remove chains
Kinkocalypse.RemoveChainsFromItem = function(item)
	local inventory = getPlayer():getInventory()
	local items = inventory:FindAll("Kinkocalypse.ChainsKey")
	for i = 0, items:size() - 1 do
		local itm = items:get(i)
		if itm ~= nil then
			if itm:getModData().ChainsKeyData == item:getModData().ChainsKeyData then 
				inventory:Remove(itm)
			end
		end
	end	
	item:getModData().ChainsKeyData = nil
	inventory:AddItem("Base.Padlock")
end

Kinkocalypse.AddChainsToItemNoKey = function(item)
	local inventory = getPlayer():getInventory()
	item:getModData().ChainsKeyData = -999
end

-- remove chains
Kinkocalypse.RemoveChainsFromItemNoKey = function(item)
	item:getModData().ChainsKeyData = nil
end

Kinkocalypse.DuplicateRestrainKeyData = function(parent, child)
	parent:getModData().RestrainKeyData = nil
	local template = ScriptManager.instance:getItem("Kinkocalypse.SteelWristCuffs_Back")
	parent:getModData().RestrainKeyData = child:getModData().RestrainKeyData	
	--parent:setName(template:getDisplayName().." #"..parent:getModData().RestrainKeyData)
end

Kinkocalypse.CheckHandBackChains = function(player, tags)
	local first = player:getPrimaryHandItem()
	local second = player:getSecondaryHandItem()
	local cuffs = player:getInventory():FindAndReturn("Kinkocalypse.SteelWristCuffs_Back")
	if tags["ChainedHandsBack"] ~= nil then
		if Kinkocalypse.HasItem("Kinkocalypse.SteelWristCuffs_Back") == false then
			cuffs = player:getInventory():AddItem("Kinkocalypse.SteelWristCuffs_Back")
		end

		if first ~= nil then
			if first:getName() ~= "Kinkocalypse.SteelWristCuffs_Back" then
				player:setPrimaryHandItem(nil)
				player:setSecondaryHandItem(nil)
				player:setPrimaryHandItem(cuffs)
				player:setSecondaryHandItem(cuffs)
			end
		else
			player:setPrimaryHandItem(cuffs)
			player:setSecondaryHandItem(cuffs)
		end
	else
		if first ~= nil then
			if first:getName() == "Kinkocalypse.SteelWristCuffs_Back" or Kinkocalypse.IsHasTag(first, "RestrictedSystemCuffs") then
				player:removeWornItem(first)
				player:setPrimaryHandItem(nil)
			end
		end		
		if second ~= nil then
			if second:getName() == "Kinkocalypse.SteelWristCuffs_Back" or Kinkocalypse.IsHasTag(second, "RestrictedSystemCuffs") then
				player:removeWornItem(second)
				player:setSecondaryHandItem(nil)
			end
		end
		local items = player:getInventory():FindAll("Kinkocalypse.SteelWristCuffs_Back")
		if items ~= nil then 
			for i = 0, items:size() - 1 do
				local itm = items:get(i)
				if itm ~= nil then player:getInventory():Remove(itm) end
			end	 
		end
		player:getInventory():RemoveAll("Kinkocalypse.SteelWristCuffs_Back")
	end
end

Kinkocalypse.GetPlayerEquipedRestrictionsTags = function()
	local player = getPlayer()
	local wornItems = player:getWornItems()
	local first = player:getPrimaryHandItem()
	local second = player:getSecondaryHandItem()
	
	local tags = {}
	if wornItems ~= nil then
		for i = 0, wornItems:size() - 1 do
			local item = wornItems:get(i):getItem()
			local itemTags = item:getTags()
			for j = 0, itemTags:size() - 1 do
				local tag = itemTags:get(j)
				if tags[tag] == nil then
					tags[tag] = tag
				end
			end
			if item:getModData().ChainedHands == true then tags["ChainedHands"] = "ChainedHands" end
			if item:getModData().ChainedHandsBack == true then tags["ChainedHandsBack"] = "ChainedHandsBack" end
			if item:getModData().ChainedLegs == true then tags["ChainedLegs"] = "ChainedLegs" end
        end
	end
	
	if first ~= nil then
		if first:getName() == "Kinkocalypse.SteelWristCuffs_Back" then tags["RestrictedSystemCuffs"] = "RestrictedSystemCuffs" end
	end
	if second ~= nil then
		if second:getName() == "Kinkocalypse.SteelWristCuffs_Back" then tags["RestrictedSystemCuffs"] = "RestrictedSystemCuffs" end
	end	
	return tags
end

Kinkocalypse.WetPants = function(wetness)
	local player = getPlayer()
	local wornItems = player:getWornItems()
	if wetness == nil or wetness < 1 then wetness = 1 end
	
	if wornItems ~= nil then
		for i = 0, wornItems:size() - 1 do
			local item = wornItems:get(i):getItem()
			if item ~= nil then
				if Kinkocalypse.IsBottomItemLocationIncludeSkirts(player, item:getBodyLocation()) and item:getType() == "Clothing" then	
					item:setDirtyness(item:getDirtyness() + wetness)
					item:setWetness(item:getWetness() + wetness)
				end
			end
        end
	end
	player:addDirt(BloodBodyPartType.Groin, wetness, false)
	player:addDirt(BloodBodyPartType.UpperLeg_R, wetness * 0.75, false)
	player:addDirt(BloodBodyPartType.UpperLeg_L, wetness * 0.75, false)
	player:addDirt(BloodBodyPartType.LowerLeg_R, wetness * 0.25, false)
	player:addDirt(BloodBodyPartType.LowerLeg_L, wetness * 0.25, false)
	player:getBodyDamage():setWetness(player:getBodyDamage():getWetness() + PZMath.clamp(wetness * 0.01, 0.01, 0.1));	
	if wetness > 20 and ZombRand(100) + (wetness * 0.5) > 75 then Kinkocalypse.AddCumToSprite(player) end
end

Kinkocalypse.IsBottomItemLocation = function(player, bodyLoaction)
	return bodyLoaction == "Underwear" or bodyLoaction == "UnderwearBottom" or bodyLoaction == "Torso1Legs1" or bodyLoaction == "FullSuit" or bodyLoaction == "FullSuitHead" or bodyLoaction == "Boilersuit" or bodyLoaction == "Legs1" or bodyLoaction == "Pants" or bodyLoaction == "Groin"
end

Kinkocalypse.IsBottomItemLocationIncludeSkirts = function(player, bodyLoaction)
	return bodyLoaction == "Underwear" or bodyLoaction == "UnderwearBottom" or bodyLoaction == "Torso1Legs1" or bodyLoaction == "FullSuit" or bodyLoaction == "FullSuitHead" or bodyLoaction == "Boilersuit" or bodyLoaction == "Legs1" or bodyLoaction == "Pants" or bodyLoaction == "Groin"  or bodyLoaction == "Skirt"
end

Kinkocalypse.IsTopItemLocation = function(player, bodyLoaction)
	return bodyLoaction == "UnderwearTop" or bodyLoaction == "Torso1Legs1" or bodyLoaction == "FullSuit" or bodyLoaction == "FullSuitHead" or bodyLoaction == "Boilersuit" or bodyLoaction == "TankTop" or bodyLoaction == "TorsoExtraVest" or bodyLoaction == "TorsoExtra" or bodyLoaction == "ShortSleeveShirt" or bodyLoaction == "Tshirt" or bodyLoaction == "Shirt" or bodyLoaction == "SweaterHat" or bodyLoaction == "Sweater" or bodyLoaction == "Jacket_Bulky" or bodyLoaction == "JacketHat_Bulky" or bodyLoaction == "FullTop" or bodyLoaction == "JacketSuit"
end

Kinkocalypse.IsCanEquipItemWhileChained = function(player, bodyLoaction)
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()	
	if (equipedTags["ChainedLegs"] ~= nil or equipedTags["ChainedThighs"] ~= nil) and Kinkocalypse.IsBottomItemLocation(player, bodyLoaction) == true then return false end	
	if (equipedTags["ChainedHandsBack"] ~= nil or equipedTags["ChainedHands"] ~= nil) and Kinkocalypse.IsTopItemLocation(player, bodyLoaction) == true then return false end
	return true
end

Kinkocalypse.SetEquipedRestriction = function(player, item, state)
	if player:getModData().RestrictedItems == nil then player:getModData().RestrictedItems = {} end
	if item ~= nil then
		if state == nil or state == false then
			player:getModData().RestrictedItems[item:getName()] = nil
			return
		end
		player:getModData().RestrictedItems[item:getName()] = { Item = item, State = state, }
	end
end

Kinkocalypse.GetEquipedRestriction = function(player)
	if player:getModData().RestrictedItems == nil then player:getModData().RestrictedItems = {} end
	return player:getModData().RestrictedItems
end

Kinkocalypse.IsClothingCategory = function(itm)
	-- Better Sorting mod compatibility
	if itm == nil then return false end

	local category = itm:getCategory()
	local isRemovableClothing = false
	if string.find(category, "Clothing") then isRemovableClothing = true end
	if string.find(category, "Bag") or string.find(category, "Backpack") then isRemovableClothing = false end
	return isRemovableClothing
end

Kinkocalypse.UnEquipNonRestrictionClothing = function()
	local wornItems = getPlayer():getWornItems()
	if wornItems == nil then return end

	for i = 0, wornItems:size() - 1 do
		local wItm = wornItems:get(i)
		if wItm ~= nil then
			local itm = wItm:getItem()
			if Kinkocalypse.IsHasTag(itm, "SteelRestriction") == false and Kinkocalypse.IsClothingCategory(itm) then
				getPlayer():removeWornItem(itm)
			end
		end
	end
end

-- Equip items hook
ISWearClothing.perform = function(self)
	local wornItem = self.character:getWornItem(self.item:getBodyLocation())
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()
	
	if Kinkocalypse.IsHasTag(self.item, "SteelControllCenter") == true then
		Kinkocalypse.OnControllCenterActivate(self.item)
		return ISWearClothing.stop(self)
	end
	
	if Kinkocalypse.ControllCenterIsOn() == true and Kinkocalypse.IsPunishmentActive("AlwaysNude") == true and self.item:getCategory() == "Clothing" then
		Kinkocalypse.AddPunishmentScore(10, getText("UI_Kincalypse_ViolateNudeRule"))
		return ISWearClothing.stop(self) 
	end
	
	if equipedTags["ChainedHandsBack"] ~= nil then
		Kinkocalypse.Say(getText("UI_Kincalypse_CantChainsBehind"))
		return ISWearClothing.stop(self) 
	end
	
	if self.character:isFemale() == false and Kinkocalypse.IsHasTag(wornItem, "SteelVaginalPlug") == true then
		Kinkocalypse.Say(getText("UI_Kincalypse_NeedBeFemale"))
		return ISWearClothing.stop(self)
	end
	
	if wornItem ~= nil then
		if Kinkocalypse.IsHasTag(wornItem, "SteelRestriction") == true then 
			Kinkocalypse.Say(getText("UI_Kincalypse_NeedUnlockFirst")..wornItem:getName()..getText("UI_Kincalypse_First"))
			return ISWearClothing.stop(self)
		end
	end
	if Kinkocalypse.IsCanEquipItemWhileChained(self.character, self.item:getBodyLocation()) == false and Kinkocalypse.IsHasTag(self.item, "SteelRestriction") == false then
		Kinkocalypse.Say(getText("UI_Kincalypse_ChainsNotAllowEquip")..self.item:getName().."!")
		return ISWearClothing.stop(self)
	end	
	
	if Kinkocalypse.IsHasTag(self.item, "SteelRestriction") == true then 
		self.character:Say("[Click!]")
		Kinkocalypse.SetEquipedRestriction(self.character, self.item, true)		
	end
	
	-- remove pants and other bottom things due to thighs chains...
	if Kinkocalypse.IsHasTag(self.item, "ChainedThighs") == true then 
		local wornItems = self.character:getWornItems()
		if wornItems ~= nil then
			for i = 0, wornItems:size() - 1 do
				local itm = wornItems:get(i)
				if itm ~= nil then
					local item = itm:getItem()
					if Kinkocalypse.IsBottomItemLocation(self.character, item:getBodyLocation()) then 
						self.character:removeWornItem(item)
					end
				end
			end
		end
	end	
	return Kinkocalypse.ISWearClothingActionOrigPerform(self)
end

Kinkocalypse.StopTimedAction = function()
	local player = getPlayer()
	ISTimedActionQueue.clear(player)
end


ISInventoryTransferAction.perform = function(self)
	-- prevent player from getting around unequip restrictions by transferring restricted gear to a container
	if self.item == nil then Kinkocalypse.StopTimedAction() return end
	local canUnequip = true
	local isRestrictedGear = false
	local hasKeys = Kinkocalypse.HasKeysFromRestrains(self.item)
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()
	if Kinkocalypse.IsHasTag(self.item, "SteelRestriction") == true then isRestrictedGear = true end

	if equipedTags["ChainedHandsBack"] ~= nil then Kinkocalypse.StopTimedAction() return end

 	if isRestrictedGear == true then
		if self.item:getModData().RestrainKeyData == -999 then
			Kinkocalypse.Say(self.item:getName()..getText("UI_Kincalypse_IsWelded"))
			canUnequip = false
		end
		if hasKeys ~= true then
			Kinkocalypse.Say(getText("UI_Kincalypse_NeedKey")..self.item:getName())
			canUnequip = false
		end
		-- punish for trying to unequip restricted item
		if Kinkocalypse.ControllCenterIsOn() == true then
			Kinkocalypse.ControllUnEquipRestriction()
			canUnequip = false
		elseif hasKeys == true then canUnequip = true end --unequip if key and if no control center
	end

	if canUnequip == true then
		if isRestrictedGear == true then Kinkocalypse.SetEquipedRestriction(self.character, self.item, nil) end
		return Kinkocalypse.ISInventoryTransferActionOrigPerform(self)
	else
		Kinkocalypse.StopTimedAction()
		return
	end
end

-- UnEquip items hook
ISUnequipAction.perform = function(self)
	if self.item == nil then return ISUnequipAction.stop(self) end

	local isRestrictedGear = false
	local hasKeys = Kinkocalypse.HasKeysFromRestrains(self.item)
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()
	if Kinkocalypse.IsHasTag(self.item, "SteelRestriction") == true then isRestrictedGear = true end
	
	if isRestrictedGear == true then
		if Kinkocalypse.ControllCenterIsOn() == true then
			Kinkocalypse.ControllUnEquipRestriction()
		end
		
		if self.item:getModData().RestrainKeyData == -999 then
			Kinkocalypse.Say(self.item:getName()..getText("UI_Kincalypse_IsWelded"))
			Kinkocalypse.StopTimedAction()
			return
		end
		
		if hasKeys == true then
			return Kinkocalypse.ISUnequipActionOrigPerform(self)
		else
			Kinkocalypse.Say(getText("UI_Kincalypse_NeedKey")..self.item:getName())
			Kinkocalypse.ControllUnEquipRestriction()
			Kinkocalypse.StopTimedAction()
			return
		end
	elseif equipedTags["ChainedHandsBack"] ~= nil then
		Kinkocalypse.StopTimedAction()
		return
	end
		
	if Kinkocalypse.IsCanEquipItemWhileChained(self.character, self.item:getBodyLocation()) == false then
		Kinkocalypse.Say(getText("UI_Kincalypse_StuckWith")..self.item:getName()..getText("UI_Kincalypse_DamnChains"))
		Kinkocalypse.StopTimedAction()
		return
	end
	
	if isRestrictedGear == true then
		Kinkocalypse.SetEquipedRestriction(self.character, self.item, nil)
	end
	return Kinkocalypse.ISUnequipActionOrigPerform(self)
end

ISEquipWeaponAction.perform = function(self)
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()
	if equipedTags["ChainedHandsBack"] ~= nil and Kinkocalypse.IsHasTag(self.item, "RestrictedSystemCuffs") == false then
		Kinkocalypse.Say(getText("UI_Kincalypse_CantChainsBehind"))
		Kinkocalypse.StopTimedAction()
		return
	end
	return Kinkocalypse.ISEquipWeaponActionOrigPerform(self)
end

-- check and prevent degradation of steel restrictions
Kinkocalypse.MaintainRestrictedItems = function()
	local player = getPlayer()
	if GameTime:isGamePaused() == true or player:isAsleep() then return end
	
	local wornItems = player:getWornItems()
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()

	if wornItems ~= nil then
		for i = 0, wornItems:size() - 1 do
			local item = wornItems:get(i):getItem()
            if Kinkocalypse.IsHasTag(item, "SteelRestriction") == true then 
				item:setCondition(99999)
				
				if item:getType() == "Clothing" then
					local dirt = item:getDirtyness()
					local wet = item:getWetness()
					local blood = item:getBloodlevel()
					
					dirt = dirt - (dirt * 0.001)
					wet = wet - (wet * 0.001)
					blood = blood - (blood * 0.001)
					
					if dirt < 0 then dirt = 0 end
					if wet < 0 then wet = 0 end
					if blood < 0 then blood = 0 end
					
					item:setDirtyness(dirt)
					item:setWetness(wet)
					item:setBloodLevel(blood)
				end
				Kinkocalypse.SetRestrainKeyData(item)
				Kinkocalypse.SetEquipedRestriction(player, item, true)	
			end
        end
	end
	
	if equipedTags["ChainedHandsBack"] == nil then
		player:getInventory():RemoveAll("Kinkocalypse.SteelWristCuffs_Back")
	end
end
Events.EveryOneMinute.Add(Kinkocalypse.MaintainRestrictedItems)

Kinkocalypse.CheckRestrictionItems = function(player, equipedTags)
	local wornItems = player:getWornItems()
	local equippedRestrictions = Kinkocalypse.GetEquipedRestriction(player)
	local restrictionsCount = 0
	
	if wornItems == nil then return end
	
	if equippedRestrictions == nil then
		for i = 0, wornItems:size() - 1 do
			local item = wornItems:get(i):getItem()
            if Kinkocalypse.IsHasTag(item, "SteelRestriction") == true then 
				Kinkocalypse.SetEquipedRestriction(player, item, true)
			end
		end
		equippedRestrictions = Kinkocalypse.GetEquipedRestriction(player)
		if equippedRestrictions == nil then return end
	end
	
	if equippedRestrictions ~= nil then
		for indx, itemData in ipairs(equippedRestrictions) do
			if itemData ~= nil and itemData.State == true then
				if Kinkocalypse.HasItem(indx) == false then
					itemData.Item = player:getInventory():AddItem(itemData.Item:getName())
					Kinkocalypse.SetEquipedRestriction(player, itemData.Item, true)
				end
				local wornItem = player:getWornItem(itemData.Item:getBodyLocation())
				if wornItem ~= itemData.Item then						
					player:setWornItem(itemData.Item:getBodyLocation(), itemData.Item)
				end
				restrictionsCount = restrictionsCount + 1
			end
		end
	end
	
	player:getModData().RestrictionItemsCount = restrictionsCount
	if equipedTags["ChainedHandsBack"] == nil then
		local items = player:getInventory():FindAll("Kinkocalypse.SteelWristCuffs_Back")
		if items ~= nil then 
			for i = 0, items:size() - 1 do
				local itm = items:get(i)
				if itm ~= nil then player:getInventory():Remove(itm) end
			end	 
		end
		player:getInventory():RemoveAll("Kinkocalypse.SteelWristCuffs_Back")
	end
end


-- check items state on clothing updated
function Kinkocalypse.OnClothingUpdated(playerOrCharacter)
	if GameTime:isGamePaused() == true then return end
	
	local player = getPlayer()
	local equipedTags = Kinkocalypse.GetPlayerEquipedRestrictionsTags()
	
	Kinkocalypse.CheckHandBackChains(player, equipedTags)
	Kinkocalypse.CheckBlindFold(player)
	Kinkocalypse.CheckRestrictionItems(player, equipedTags)
end
Events.OnClothingUpdated.Add(Kinkocalypse.OnClothingUpdated)
