ESX = nil
local Users = {}

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

ESX.RegisterServerCallback('ray_chainsnatch:getValue', function(source, cb, targetSID)
	if Users[targetSID] then
		cb(Users[targetSID])
	else
		cb({value = false, time = 0})
	end
end)

ESX.RegisterServerCallback('ray_chainsnatch:getOtherPlayerData', function(source, cb, target)
	local xPlayer = ESX.GetPlayerFromId(target)

	local data = {
		name = GetPlayerName(target),
		inventory = xPlayer.inventory,
		accounts = xPlayer.accounts,
		money = xPlayer.getMoney()
	}

	cb(data)
end)


ESX.RegisterServerCallback('ray_chainsnatch:getMyInv', function(source, cb)
	local xPlayer = ESX.GetPlayerFromId(source) or {["inventory"] = {}, ["accounts"] = {}}
	local data = {
		name = GetPlayerName(source),
		inventory = xPlayer.inventory,
		accounts = xPlayer.accounts,
	}
	cb(data)
end)


RegisterServerEvent('ray_chainsnatch:stealPlayerItem')
AddEventHandler('ray_chainsnatch:stealPlayerItem', function(target, itemType, itemName, amount)
	local _source = source
	local sourceXPlayer = ESX.GetPlayerFromId(_source)
	local targetXPlayer = ESX.GetPlayerFromId(target)

	if itemType == 'item_standard' then

		local label = sourceXPlayer.getInventoryItem(itemName).label
		local itemLimit = sourceXPlayer.getInventoryItem(itemName).limit
		local sourceItemCount = sourceXPlayer.getInventoryItem(itemName).count
		local targetItemCount = targetXPlayer.getInventoryItem(itemName).count

		if amount > 0 and targetItemCount >= amount then
			if itemLimit ~= -1 and (sourceItemCount + amount) > itemLimit then
				TriggerClientEvent('esx:showNotification', targetXPlayer.source, _U('ex_inv_lim_target'))
				TriggerClientEvent('esx:showNotification', sourceXPlayer.source, _U('ex_inv_lim_source'))
			else
				targetXPlayer.removeInventoryItem(itemName, amount)
				sourceXPlayer.addInventoryItem(itemName, amount)

				local _source = source
				local name = GetPlayerName(_source) --Steam Name
				local nametarget = GetPlayerName(target) --Target Steam Name
				local ip = GetPlayerEndpoint(source) --Sources IP 
				local ping = GetPlayerPing(source) --Useless
				local steamhex = GetPlayerIdentifier(source) --Sources Steam hex
				local sourceXPlayer = ESX.GetPlayerFromId(_source) --Source Server ID
				local targetXPlayer = ESX.GetPlayerFromId(target) -- Target Server ID
				local date = os.date('*t')
					if date.month < 10 then date.month = '0' .. tostring(date.month) end
							  if date.day < 10 then date.day = '0' .. tostring(date.day) end
							  if date.hour < 10 then date.hour = '0' .. tostring(date.hour) end
							  if date.min < 10 then date.min = '0' .. tostring(date.min) end
							  if date.sec < 10 then date.sec = '0' .. tostring(date.sec) end
						  
							  TriggerEvent('DiscordBot:ToDiscord', 'chains', 'Snatched Chains', '``` ID:'..source..'  '..name..' | snatched x' ..amount..' ' ..label..' | from ID:'..target..'  '..nametarget..' '
							   ..'' .. date.month .. '/' .. date.day ..'/' .. date.year .. ' | ' .. date.hour .. ':' .. date.min .. ':' .. date.sec 
							   .. '```', 'IMAGE_URL', true)




				TriggerClientEvent('esx:showNotification', sourceXPlayer.source, _U('you_stole') .. ' x' .. amount .. ' ' .. label .. ' ' .. _U('from_your_target') )
				TriggerClientEvent('esx:showNotification', targetXPlayer.source, _U('someone_stole') .. ' x'  .. amount .. ' ' .. label )
			end
		else
			TriggerClientEvent('esx:showNotification', _source, _U('invalid_quantity'))
		end

	elseif itemType == 'item_money' then

		if amount > 0 and targetXPlayer.get('money') >= amount then
			targetXPlayer.removeMoney(amount)
			sourceXPlayer.addMoney(amount)

			TriggerClientEvent('esx:showNotification', sourceXPlayer.source, _U('you_stole') .. ' $' .. amount .. ' ' .. _U('from_your_target') )
			TriggerClientEvent('esx:showNotification', targetXPlayer.source, _U('someone_stole') .. ' $'  .. amount )
		else
			TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
		end

	elseif itemType == 'item_account' then

		if amount > 0 and targetXPlayer.getAccount(itemName).money >= amount then
			targetXPlayer.removeAccountMoney(itemName, amount)
			sourceXPlayer.addAccountMoney(itemName, amount)

			TriggerClientEvent('esx:showNotification', sourceXPlayer.source, _U('you_stole') .. ' $' .. amount .. ' ' .. _U('of_black_money') .. ' ' .. _U('from_your_target') )
			TriggerClientEvent('esx:showNotification', targetXPlayer.source, _U('someone_stole') .. ' $'  .. amount .. ' ' .. _U('of_black_money') )
		else
			TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
		end

	end
end)

RegisterServerEvent('ray_chainsnatch:update')
AddEventHandler('ray_chainsnatch:update', function(bool)
	local _source = source
	Users[_source] = {value = bool, time = os.time()}
end)

RegisterServerEvent('ray_chainsnatch:getValue')
AddEventHandler('ray_chainsnatch:getValue', function(targetSID)
	local _source = source
	if Users[targetSID] then
		TriggerClientEvent('ray_chainsnatch:returnValue', _source, Users[targetSID])
	else
		TriggerClientEvent('ray_chainsnatch:returnValue', _source, Users[targetSID])
	end
end)




ESX.RegisterUsableItem('crosschain', function(source)
    TriggerClientEvent('crosschain', source)
end)


ESX.RegisterUsableItem('cubanchain', function(source)
	TriggerClientEvent('cubanchain', source)
end)

ESX.RegisterUsableItem('raychain', function(source)
	TriggerClientEvent('raychain', source)
end)

ESX.RegisterUsableItem('ratchain', function(source)
	TriggerClientEvent('ratchain', source)
end)

ESX.RegisterUsableItem('woochain', function(source)
	TriggerClientEvent('woochain', source)
end)

ESX.RegisterUsableItem('otfchain', function(source)
	TriggerClientEvent('otfchain', source)
end)

ESX.RegisterUsableItem('cubanmafiachain', function(source)
	TriggerClientEvent('cubanmafiachain', source)
end)

ESX.RegisterUsableItem('gthreechain', function(source)
	TriggerClientEvent('gthreechain', source)
end)

ESX.RegisterUsableItem('spadeschain', function(source)
	TriggerClientEvent('spadeschain', source)
end)


ESX.RegisterUsableItem('woochaintwo', function(source)
	TriggerClientEvent('woochaintwo', source)
end)


ESX.RegisterUsableItem('diamondchainv', function(source)
	TriggerClientEvent('diamondchainv', source)
end)

ESX.RegisterUsableItem('glogangchain', function(source)
	TriggerClientEvent('glogangchain', source)
end)

ESX.RegisterUsableItem('tmfchain', function(source)
	TriggerClientEvent('tmfchain', source)
end)
