import express from 'express';
import { LoginSystem } from '../../../../features/login';
import { playerSettingsSystem } from '../../../../features/settings';

export async function IdPlayer(req: express.Request, res: express.Response): Promise<void> {
    const input = req.query.input as string;

    if (!input) {
        res.sendStatus(404);
        return;
    }

    if (input[0] === '@') {
        const results = accountSearch(input);
        res.send({ message: results });
        return;
    }

    if (!isNaN(Number(input))) {
        const idQuery = Number(input);
        const fetch = mp.players.at(idQuery);

        if (fetch) {
            res.send({ message: `ID: ${fetch.id}) ${fetch.name}${fetch.character ? ` (Level: ${fetch.character.level})` : ''}` });
        } else {
            res.send({ message: `No results found for Player ID ${idQuery}.` });
        }
        return;
    }

    let players: PlayerMp[] = [];

    for (const p of mp.players.toArrayFast()) {
        if (p.name.toLowerCase() === input.toLowerCase()) {
            players = [p];
            break;
        }

        if (p.name.toLowerCase().includes(input.toLowerCase())) {
            players.push(p);
        }
    }

    if (!players.length) {
        res.send({ message: `No results found for '${input}'.` });
        return;
    }

    if (players.length > 3) {
        res.send({ message: `Too many results, try shortening your query.` });
        return;
    }

    if (players.length === 1) {
        res.send({ message: `(ID: ${players[0].id}) ${players[0].name}${players[0].character ? ` (Level: ${players[0].character?.level})` : ''}` });
    } else {
        const grouped = players
            .map((a) => {
                return `${a.name} (ID: ${a.id})`;
            })
            .join(', ');

        res.send({ message: `${grouped}` });
    }
}

function accountSearch(input: string): string {
    const fetchName = input.slice(1);

    let accounts: PlayerMp[] = [];

    const playerList = mp.players.toArrayFast().filter((p) => LoginSystem.IsLoggedIn(p));

    for (const pl of playerList) {
        if (!playerSettingsSystem.getPlayerSetting(pl as PlayerMpLoggedIn, 'privacyShowAccountName')) {
            continue;
        }

        if (pl.account!.name.toLowerCase() === fetchName.toLowerCase()) {
            accounts = [pl];
            break;
        }

        if (pl.account!.name.toLowerCase().includes(fetchName.toLowerCase())) {
            accounts.push(pl);
        }
    }

    if (!accounts.length) {
        return `No results found for '${input}`;
    }

    if (accounts.length > 3) {
        return `Too many results, try shortening your query.`;
    }

    if (accounts.length === 1) {
        return `ID: ${accounts[0].id}) ${accounts[0].name} (${accounts[0].account!.name}) (Level: ${accounts[0].character?.level})`;
    } else {
        const grouped = accounts
            .map((a) => {
                return `${a.name} ${a.account!.name} (ID: ${a.id})`;
            })
            .join(', ');

        return `${grouped}`;
    }
}
