TwoNa = exports["2na_core"]:getSharedObject()
ShowingMenu = false
ParkCam = nil

local function loadAnimDict(dict)
    while (not HasAnimDictLoaded(dict)) do
        RequestAnimDict(dict)
        Wait(0)
    end
end

RegisterNetEvent("hp_carcontrol:Client:ShowMenu")
AddEventHandler("hp_carcontrol:Client:ShowMenu", function() 
    local vehicle = GetPedVehicleData(GetPlayerPed(-1))

    if vehicle then 
        TwoNa.TriggerServerCallback("hp_carcontrol:Server:GetMileage", { plate = GetVehicleNumberPlateText(vehicle.vehicle) }, function(mileage) 
            vehicle.mileage = mileage

            SetNuiFocus(true, true)

            SendNUIMessage({
                action = "show",
                vehicle = vehicle
            })

            ShowingMenu = true
        end)
    end
end)

RegisterNetEvent("hp_carcontrol:Client:HideMenu")
AddEventHandler("hp_carcontrol:Client:HideMenu", function() 
    SendNUIMessage({
        action = "hide",
    })

    SetNuiFocus(false, false)

    ShowingMenu = false
end)

RegisterNetEvent("hp_carcontrol:Client:ControlCarMedia")
AddEventHandler("hp_carcontrol:Client:ControlCarMedia", function(payload) 
    SendNUIMessage(payload)
end)

RegisterNetEvent("hp_carcontrol:Client:Trigger")
AddEventHandler("hp_carcontrol:Client:Trigger", function(veh)
    local ped = PlayerPedId()

    loadAnimDict("anim@mp_player_intmenu@key_fob@")
    TaskPlayAnim(ped, 'anim@mp_player_intmenu@key_fob@', 'fob_click', 3.0, 3.0, -1, 49, 0, false, false, false)

    TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5, "lock", 0.3)

    SetVehicleLights(veh, 2)
    Wait(250)
    SetVehicleLights(veh, 1)
    Wait(200)
    SetVehicleLights(veh, 0)
    Wait(300)
    ClearPedTasks(ped)
end)

RegisterNetEvent("hp_carcontrol:Client:ShowParkCam")
AddEventHandler("hp_carcontrol:Client:ShowParkCam", function() 
    if ParkCam then 
        DisableParkCam()
    else
        EnableParkCam()
    end

    TriggerEvent("hp_carcontrol:Client:HideMenu")
end)