require "TimedActions/ISBaseTimedAction"
require "TimedActions/ISWearClothing"

Kinkocalypse = Kinkocalypse or {}

function Kinkocalypse.ItemsContextMenuEntry(player, context, items)
  local items = ISInventoryPane.getActualItems(items)
  local inventory = getSpecificPlayer(player):getInventory()
  
	if Kinkocalypse.ControllCenterIsOn() == false then
	  for _, item in ipairs(items) do	  
		if Kinkocalypse.HasItem("Base.Padlock") then
			-- Equip and chain
			if Kinkocalypse.IsHasTag(item, "SteelWristCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) ~= item then
			  context:addOption(getText("UI_Kincalypse_EquipAndChain_WristsCuffs"), getSpecificPlayer(player), function()
				item:getModData().ChainedHands = true
				Kinkocalypse.AddChainsToItem(item)
				ISTimedActionQueue.add(ISWearClothing:new(getSpecificPlayer(player), item, 10))
				Kinkocalypse.OnClothingUpdated()
			  end)
			end
			if Kinkocalypse.IsHasTag(item, "SteelWristCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) ~= item then
			  context:addOption(getText("UI_Kincalypse_EquipAndChain_WristsCuffs_Back"), getSpecificPlayer(player), function()
				item:getModData()ChainedHands = true
				item:getModData().ChainedHandsBack = true
				Kinkocalypse.AddChainsToItem(item)
				ISTimedActionQueue.add(ISWearClothing:new(getSpecificPlayer(player), item, 20))
				Kinkocalypse.OnClothingUpdated()
			  end)
			end		
			if Kinkocalypse.IsHasTag(item, "SteelAnkleCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) ~= item then
			  context:addOption(getText("UI_Kincalypse_EquipAndChain_Anklets"), getSpecificPlayer(player), function()
				item:getModData().ChainedLegs = true
				Kinkocalypse.AddChainsToItem(item)
				ISTimedActionQueue.add(ISWearClothing:new(getSpecificPlayer(player), item, 10))
				Kinkocalypse.OnClothingUpdated()
			  end)
			end
			
			-- Chain equiped
			if Kinkocalypse.IsHasTag(item, "SteelWristCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) == item and item:getModData().ChainedHands ~= true and item:getModData().ChainedHandsBack ~= true then
			  context:addOption(getText("UI_Kincalypse_Chain_WristsCuffs"), getSpecificPlayer(player), function()
				item:getModData().ChainedHands = true
				Kinkocalypse.AddChainsToItem(item)
				Kinkocalypse.OnClothingUpdated()
				getSpecificPlayer(player):Say("[Click!]")
			  end)
			end
			if Kinkocalypse.IsHasTag(item, "SteelWristCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) == item and item:getModData().ChainedHandsBack ~= true then
			  context:addOption(getText("UI_Kincalypse_Chain_WristsCuffs_Back"), getSpecificPlayer(player), function()
				item:getModData().ChainedHands = true
				item:getModData().ChainedHandsBack = true
				Kinkocalypse.AddChainsToItem(item)
				Kinkocalypse.OnClothingUpdated()
				getSpecificPlayer(player):Say("[Click!]")
			  end)
			end
			if Kinkocalypse.IsHasTag(item, "SteelAnkleCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) == item and item:getModData().ChainedLegs ~= true then
			  context:addOption(getText("UI_Kincalypse_Chain_Anklets"), getSpecificPlayer(player), function()
				item:getModData().ChainedLegs = true
				Kinkocalypse.AddChainsToItem(item)
				Kinkocalypse.OnClothingUpdated()
				getSpecificPlayer(player):Say("[Click!]")
			  end)
			end
		end
		
		-- UnChain Equiped
		if Kinkocalypse.HasChainsKey(item) then
			if Kinkocalypse.IsHasTag(item, "SteelWristCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) == item and item:getModData().ChainedHands == true then
			  context:addOption(getText("UI_Kincalypse_UnChain_WristsCuffs"), getSpecificPlayer(player), function()
				item:getModData().ChainedHands = false
				item:getModData().ChainedHandsBack = false
				Kinkocalypse.RemoveChainsFromItem(item)
				Kinkocalypse.OnClothingUpdated()
			  end)
			end
			if Kinkocalypse.IsHasTag(item, "SteelAnkleCuffs") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) == item and item:getModData().ChainedLegs == true then
			  context:addOption(getText("UI_Kincalypse_UnChain_Anklets"), getSpecificPlayer(player), function()
				item:getModData().ChainedLegs = false
				Kinkocalypse.RemoveChainsFromItem(item)
				Kinkocalypse.OnClothingUpdated()
			  end)
			end
		end
		
		-- Gag works
		if Kinkocalypse.IsHasTag(item, "SteelGag") == true and getSpecificPlayer(player):getWornItem(item:getBodyLocation()) == item and item:getModData().GagClosed ~= true then
			if item:getModData().GagClosed ~= true then
				context:addOption(getText("UI_Kincalypse_PlugGag"), getSpecificPlayer(player), function()
					getSpecificPlayer(player):Say(getText("UI_Kincalypse_GagPlugged"))
					item:getModData().GagClosed = true
				end)
			end
			if Kinkocalypse.HasItem("Base.Padlock") then
				context:addOption(getText("UI_Kincalypse_Plug_Gag_WithPadlock"), getSpecificPlayer(player), function()
					getSpecificPlayer(player):Say(getText("UI_Kincalypse_GagPlugged"))
					item:getModData().GagClosed = true
					item:getModData().PlugLocked = true
					Kinkocalypse.AddChainsToItem(item)
					getSpecificPlayer(player):Say("[Click!]")
				end)
			end
			
			if item:getModData().GagClosed == true then
				if item:getModData().PlugLocked == true and Kinkocalypse.HasChainsKey(item) then
					context:addOption(getText("UI_Kincalypse_UnPlugGag"), getSpecificPlayer(player), function()
						getSpecificPlayer(player):Say(getText("UI_Kincalypse_GagUnPlugged"))
						item:getModData().GagClosed = false
						item:getModData().PlugLocked = false
						Kinkocalypse.RemoveChainsFromItem(item)
					end)
				elseif item:getModData().PlugLocked ~= true then
					context:addOption(getText("UnPlug_Gag"), getSpecificPlayer(player), function()
						getSpecificPlayer(player):Say(getText("UI_Kincalypse_GagUnPlugged"))
						item:getModData().GagClosed = false
						item:getModData().PlugLocked = false
					end)
				end
			end
		end
		
		--welding
		if Kinkocalypse.HasItem("Base.BlowTorch") then
			if Kinkocalypse.IsHasTag(item, "SteelRestriction") and item:getModData().RestrainKeyData ~= -999 then
				context:addOption(getText("UI_Kincalypse_WeldPadlock"), getSpecificPlayer(player), function()
					Kinkocalypse.WeldLock(item)
					Kinkocalypse.Say(getText("UI_Kincalypse_PadlocksWelded"))
			  end)
			end
		end
	  end
	end
end

Events.OnFillInventoryObjectContextMenu.Add(Kinkocalypse.ItemsContextMenuEntry)
