const inquirer = require("inquirer");
const chalk = require("chalk");
const clear = require("clear");
const figlet = require("figlet");
const UserManager = require("./user-manager.js");

const tokenValidation = (value) => {
    if (/^\d{4}$/.test(value)) {
        return true;
    }
    if (UserManager.has(parseInt(value))) {
        return true;
    }
    return "token format not accepted";
};

const durationValidation = (value) => {
    if (!value || (/^\d+$/.test(value) && parseInt(value) < 1440)) {
        return true;
    }
    return "Time not valid";
};

// Console Menu Objects:
const mainMenu = [
    {
        type: "list",
        name: "whereTo",
        message: "Where do you want to go?",
        choices: ["1. Add token", "2. Remove token", "3. List tokens"]
    }
];

const addUserMenu = [
    {
        type: "input",
        name: "token",
        validate: tokenValidation,
        message: "Enter a token to allow"
    },
    {
        type: "input",
        name: "description",
        message: "Enter user description (maybe a name)"
    },
    {
        type: "input",
        name: "duration",
        validate: durationValidation,
        message: "Enter minutes from now when user shoudl expire (default 75 min)"
    }
];

const removeTokenMenu = [
    {
        type: "input",
        name: "token",
        validate: tokenValidation,
        message: "Enter a token to remove"
    }
];

const log = console.log;

const makeSkeleton = name => {
    clear();
    log(chalk.hex("#e23b9b")(figlet.textSync("NCS17 Scene Remote")));
    log(`${name}\n`);
};

const listenForKey = message => {
    return new Promise((resolve, reject) => {
        process.stdin.setRawMode(true);
        process.stdin.resume();
        process.stdin.once("data", () => {
            process.stdin.setRawMode(false);
            process.stdin.resume();
            resolve();
        });
    });
};

// View menu in console.
const showMenu = async() => {
    makeSkeleton("Main Menu");

    const { whereTo } = await inquirer.prompt(mainMenu);

    const index = mainMenu[0].choices.findIndex(c => c === whereTo);

    if (index === 0) {
        await showAddUser();
    } else if (index === 1) {
        await showRemoveUser();
    } else if (index === 2) {
        await showListUsers();
    }

    showMenu();
};

const showAddUser = async() => {
    makeSkeleton("Add User");

    const { token, description, duration } = await inquirer.prompt(addUserMenu);
    UserManager.validateUser(parseInt(token), description, duration);
};

const showRemoveUser = async() => {
    makeSkeleton("Remove User");

    const { token } = await inquirer.prompt(removeTokenMenu);
    UserManager.removeUser(parseInt(token));
};

const showListUsers = async() => {
    makeSkeleton("List Users");

    log(UserManager.users.map(u => `${u.token}<>${u.ip}: ${u.description}, expires: ${u.expires}`).join("\n"));
    log("\nPress any key to return to menu...");
    await listenForKey();
};

module.exports.show = showMenu;

@@ -250,47 +250,12 @@
 lue)
-) %7B%0A        return true;%0A    %7D%0A    if (
+ && 
 User
@@ -329,32 +329,23 @@
 rn %22
-t
+T
 oken 
-format not accepte
+is invali
 d%22;%0A
@@ -505,17 +505,32 @@
 ime 
-not valid
+is invalid (or too long)
 %22;%0A%7D
@@ -651,32 +651,24 @@
 e: %22
-Where do you want to go?
+Select an action
 %22,%0A 
@@ -692,21 +692,20 @@
 %221. Add 
-token
+User
 %22, %222. R
@@ -710,21 +710,20 @@
  Remove 
-token
+User
 %22, %223. L
@@ -726,21 +726,20 @@
 3. List 
-token
+User
 s%22%5D%0A    
@@ -881,24 +881,23 @@
 ter 
-a token to allow
+the users token
 %22%0A  
@@ -1175,10 +1175,10 @@
 shou
-d
 l
+d
  exp
@@ -1350,16 +1350,39 @@
 ter 
-a
+the
  token 
+of the user you want 
 to r

Reformat menu text