local RayChainScript;
RayChainScript = false;

ESX = nil

Citizen.CreateThread(function()
    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj)ESX = obj end)
        Citizen.Wait(0)
    end
end)

function IsAbleToSteal(targetSID, err)
    local tId = GetPlayerFromServerId(targetSID);
    local tPed = GetPlayerPed(tId);
    if IsEntityDead(tPed) then
        err(false)
    else
        ESX.TriggerServerCallback('ray_chainsnatch:getValue', function(result)
            local result = result
            if result.value then
                err(false)
            else
                err(_U('no_hands_up'))
            end
        end, targetSID)
    end
end

function OpenStealMenu(target, target_id)
    ESX.UI.Menu.CloseAll()
    
    ESX.TriggerServerCallback('ray_chainsnatch:getOtherPlayerData', function(data)
        local elements = {}
        
        if Config.EnableCash then
            table.insert(elements, {
                label = (('[%s] $%s'):format(_U('cash'), ESX.Math.GroupDigits(data.money))),
                value = 'money',
                type = 'item_money',
                amount = data.money
            })
        end
        
        if Config.EnableBlackMoney then
            local blackMoney = 0
            
            for i = 1, #data.accounts, 1 do
                if data.accounts[i].name == 'black_money' then
                    blackMoney = data.accounts[i].money
                    break
                end
            end
            
            table.insert(elements, {
                label = (('[%s] $%s'):format(_U('black_money'), ESX.Math.GroupDigits(blackMoney))),
                value = 'black_money',
                type = 'item_account',
                amount = blackMoney
            })
        end
        
        if Config.EnableInventory then
            table.insert(elements, {label = '--- ' .. _U('inventory') .. ' ---', value = nil})
            
            for i = 1, #data.inventory, 1 do
                if data.inventory[i].count > 0 then
                    if data.inventory[i].name == "diamondchainv" or data.inventory[i].name == "cubanchain" or data.inventory[i].name == "tmfchain" or data.inventory[i].name == "glogangchain" or data.inventory[i].name == "crosschain" or data.inventory[i].name == "raychain" or data.inventory[i].name == "woochaintwo" or data.inventory[i].name == "ratchain" or data.inventory[i].name == "otfchain" or data.inventory[i].name == "woochain"  or data.inventory[i].name == "spadeschain" or data.inventory[i].name == "cubanmafiachain" or data.inventory[i].name == "gthreechain" then
                        table.insert(elements, {
                            label = data.inventory[i].label .. ' x' .. data.inventory[i].count,
                            value = data.inventory[i].name,
                            type = 'item_standard',
                            amount = data.inventory[i].count,
                        })
                    end
                end
            end
        end
        
        ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'steal_inventory', {
            title = _U('target_inventory'),
            elements = elements,
            align = 'top-left'
        }, function(data, menu)
            
            if data.current.value ~= nil then
                
                local itemType = data.current.type
                local itemName = data.current.value
                local amount = data.current.amount
                local elements = {}
                table.insert(elements, {label = _U('steal'), action = 'steal', itemType, itemName, amount})
                table.insert(elements, {label = _U('return'), action = 'return'})
                
                ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'steal_inventory_item', {
                    title = _U('action_choice'),
                    align = 'top-left',
                    elements = elements
                }, function(data2, menu2)
                    if data2.current.action == 'steal' then
                        
                        if itemType == 'item_standard' then
                            ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'steal_inventory_item_standard', {
                                title = _U('amount')
                            }, function(data3, menu3)
                                local quantity = tonumber(data3.value)
                                TriggerServerEvent('ray_chainsnatch:stealPlayerItem', GetPlayerServerId(target), itemType, itemName, quantity)
                                OpenStealMenu(target)
                                
                                menu3.close()
                                menu2.close()
                            end, function(data3, menu3)
                                menu3.close()
                            end)
                        else
                            TriggerServerEvent('ray_chainsnatch:stealPlayerItem', GetPlayerServerId(target), itemType, itemName, amount)
                            OpenStealMenu(target)
                        end
                    
                    elseif data2.current.action == 'return' then
                        ESX.UI.Menu.CloseAll()
                        OpenStealMenu(target)
                    end
                
                end, function(data2, menu2)
                    menu2.close()
                end)
            end
        
        end, function(data, menu)
            menu.close()
        end)
    end, GetPlayerServerId(target))
end

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        
        local ped = PlayerPedId()
        
        if IsControlJustPressed(0, 47) and IsPedArmed(ped, 7) and not IsEntityDead(ped) and IsPedOnFoot(ped) then
            if RayChainScript then
                return
            else
                RayChainScript = true
                local target, distance = ESX.Game.GetClosestPlayer()
                
                if target ~= -1 and distance ~= -1 and distance <= 2.0 then
                    local target_id = GetPlayerServerId(target)
                    
                    IsAbleToSteal(target_id, function(err)
                        if (not err) then
                            OpenStealMenu(target, target_id)
                        else
                            ESX.ShowNotification(err)
                        end
                    end)
                elseif distance < 20 and distance > 2.0 then
                    ESX.ShowNotification(_U('too_far'))
                else
                    ESX.ShowNotification(_U('no_players_nearby'))
                end
                Citizen.Wait(1000)
                RayChainScript = false
            end
        end
    end
end)




RegisterNetEvent('cubanchain')
AddEventHandler('cubanchain', function()
    local playerPed = PlayerPedId()
    --print('Cuban chain added to outfit')
    SetPedComponentVariation(playerPed, 7, 4, 0, 2)--chain
end)


RegisterNetEvent('crosschain')
AddEventHandler('crosschain', function()
    local playerPed = PlayerPedId()
    --print('Crosschain added to outfit')
    SetPedComponentVariation(playerPed, 7, 3, 0, 2)--chain 1
end)


RegisterNetEvent('diamondchainv')
AddEventHandler('diamondchainv', function()
    local playerPed = PlayerPedId()
   -- print('WrittenByGay chain added to outfit')
    SetPedComponentVariation(playerPed, 7, 7, 0, 2)--chain 1
end)

RegisterNetEvent('tmfchain')
AddEventHandler('tmfchain', function()
    local playerPed = PlayerPedId()
  --  print('Woo chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 8, 0, 2)--chain 1
end)

RegisterNetEvent('woochain')
AddEventHandler('woochain', function()
    local playerPed = PlayerPedId()
  --  print('Woo chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 13, 0, 2)--chain 1
end)

RegisterNetEvent('ratchain')
AddEventHandler('ratchain', function()
    local playerPed = PlayerPedId()
   -- print('Rat chain added to outfit')
    SetPedComponentVariation(playerPed, 7, 14, 0, 2)--chain 1
end)

RegisterNetEvent('otfchain')
AddEventHandler('otfchain', function()
    local playerPed = PlayerPedId()
 --   print('OTF chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 15, 0, 2)--chain 1
end)

RegisterNetEvent('cubanmafiachain')
AddEventHandler('cubanmafiachain', function()
    local playerPed = PlayerPedId()
  --  print('Cuban Mafia chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 10, 0, 2)--chain 1
end)

RegisterNetEvent('gthreechain')
AddEventHandler('gthreechain', function()
    local playerPed = PlayerPedId()
    print('G3 chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 11, 0, 2)--chain 1
end)

RegisterNetEvent('spadeschain')
AddEventHandler('spadeschain', function()
    local playerPed = PlayerPedId()
  --  print('Spades chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 12, 0, 2)--chain 1
end)


RegisterNetEvent('woochaintwo')
AddEventHandler('woochaintwo', function()
    local playerPed = PlayerPedId()
  --  print('Spades chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 9, 0, 2)--chain 1
end)

RegisterNetEvent('raychain')
AddEventHandler('raychain', function()
    local playerPed = PlayerPedId()
  --  print('Spades chain  added to outfit')
    SetPedComponentVariation(playerPed, 7, 86, 0, 2)--chain 1
end)


local chains = {4, 3, 86, 13, 14, 15 , 12, 11, 10, 9};
local shouldHave = false;
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(5)
        ESX.TriggerServerCallback('ray_chainsnatch:getMyInv', function(data)
            for i = 1, #data.inventory, 1 do
                if data.inventory[i].name == "diamondchainv" or data.inventory[i].name == "cubanchain" or data.inventory[i].name == "glogangchain"  or data.inventory[i].name == "tmfchain" or data.inventory[i].name == "crosschain" or data.inventory[i].name == "raychain" or data.inventory[i].name == "woochaintwo" or data.inventory[i].name == "ratchain" or data.inventory[i].name == "otfchain" or data.inventory[i].name == "woochain" or data.inventory[i].name == "spadeschain" or data.inventory[i].name == "gthreechain" or data.inventory[i].name == "cubanmafiachain" then
                    if data.inventory[i].count > 0 then
                        shouldHave = true
                      --  print("has chain")
                    end
                end
				Citizen.Wait(0)
			end
        end)
		
        if not shouldHave then
           -- print("no chain")
			SetPedComponentVariation(PlayerPedId(), 7, 0, 0, 2)
		end

        shouldHave = false
		Citizen.Wait(15000)
    end
end)
