local Commands = {};
Commands.TurretSendAll = {};

Commands.TurretSendAll.RequestSpawnVehicle = function(player, args)
    local sq = getCell():getGridSquare(player:getX(), player:getY(), player:getZ())
    local car = addVehicleDebug(args.type, IsoDirections.S, -1, sq)
    local UserName = player:getUsername()
    local UserList = {}
    UserList[UserName] = {
        User = UserName,
        Access = "Owner"
    }
    local vehicleId = car:getId()
    sendServerCommand("TurretSendAll", "SetNewAccess", {
        vehicleId = vehicleId,
        User = UserList
    })
    car:getModData().User = UserList
end

Commands.TurretSendAll.SetUpANewAngle = function(player, args)
    local vehicle = getVehicleById(args.vehicleId)
    sendServerCommand("TurretSendAll", "SetUpANewAngle", {
        vehicleId = args.vehicleId,
        Angle = args.Angle,
        TurretType = args.TurretType
    })
end

Commands.TurretSendAll.SetAdmin = function(player, args)
    local vehicle = getVehicleById(args.vehicleId)
    local NewPlayerUser = args.NewPlayerUser
    local ModData = vehicle:getModData()
    local UserList = ModData.User
    if UserList[NewPlayerUser] then
        UserList[NewPlayerUser].Access = "Admin"
    else
        UserList[NewPlayerUser] = {
            User = NewPlayerUser,
            Access = "Admin"
        }
    end
    sendServerCommand("TurretSendAll", "SetNewAccess", {
        vehicleId = args.vehicleId,
        User = UserList
    })
end

Commands.TurretSendAll.NewUser = function(player, args)
    local vehicle = getVehicleById(args.vehicleId)
    local NewPlayerUser = args.NewPlayerUser
    local UserList = vehicle:getModData().User
    if not UserList then
        UserList = {}
    end
    UserList[NewPlayerUser] = {
        User = NewPlayerUser,
        Access = "User"
    }
    sendServerCommand("TurretSendAll", "SetNewAccess", {
        vehicleId = args.vehicleId,
        User = UserList
    })
end

local onClientCommand = function(module, command, player, args)
    if Commands[module] and Commands[module][command] then
        Commands[module][command](player, args);
    end
end

Events.OnClientCommand.Add(onClientCommand);
