import { VehicleSystem } from '../../../features/vehicle';

const DRIVER_SEAT = -1; // GTA/FiveM convention: -1 = driver seat

type VehicleCommandGuardOptions = {
    driver?: boolean;
    owner?: boolean;
    engine?: 'on' | 'off';
};

export const VehicleCommandGuard = (options?: VehicleCommandGuardOptions): CommandGuard => {
    return (player: PlayerMp) => {
        if (!player.vehicle) {
            return `You aren't in a vehicle`;
        }

        if (options?.engine !== undefined) {
            if (VehicleSystem.GetVehicleClassName(player.vehicle) !== 'CYCLES') {
                if (options.engine === 'on' && !player.vehicle.engine) {
                    return `The engine must be turned on`;
                } else if (options.engine === 'off' && player.vehicle.engine) {
                    return `The engine must be turned off`;
                }
            }
        }

        if (options?.driver !== undefined) {
            if (options.driver && player.seat !== DRIVER_SEAT) {
                return `You're not driving a vehicle`;
            } else if (!options.driver && player.seat === DRIVER_SEAT) {
                return `You're not a passenger`;
            }
        }

        if (options?.owner !== undefined) {
            if (options.owner && !VehicleSystem.DoesPlayerOwnVehicle(player, player.vehicle)) {
                return `You don't own the vehicle`;
            } else if (!options.owner && VehicleSystem.DoesPlayerOwnVehicle(player, player.vehicle)) {
                return `You can't own the vehicle`;
            }
        }

        return true;
    };
};
