// Import libs
import clear from 'clear';

// Import shared types
import Deployment from './types/Deployment';

// Import screens
import chooseDeployment from './screens/chooseDeployment';
import showChooser from './helpers/showChooser';
import showModifyEnvVars from './screens/showModifyEnvVars';
import chooseTarget from './screens/chooseTarget';
import deployNow from './screens/deployNow';
import viewLogs from './screens/viewLogs';
import connectToDatabase from './screens/connectToDatabase';

/* eslint-disable no-console */

/**
 * Show the wizard
 * @author Gabe Abrams
 */
const showWizard = async () => {
  // The current chosen deployment
  let deployment: Deployment = chooseDeployment();

  // Endless work loop
  while (true) {
    clear();

    // Ask user to choose a menu option
    const menuOption = showChooser({
      question: 'What do you want to show/modify?',
      options: [
        {
          description: 'Environment Vars',
          tag: 'E',
        },
        {
          description: 'Target of Deployment (version or branch)',
          tag: 'T',
        },
        {
          description: 'Deploy/Redeploy',
          tag: 'D',
        },
        {
          description: 'Switch Deployment',
          tag: 'S',
        },
        {
          description: 'Log Viewer (App Logs from Today)',
          tag: 'L',
        },
        {
          description: 'Connect to database',
          tag: 'C',
        },
      ],
      title: `${deployment.name} | Main Menu`,
    });

    // Handle each case
    if (menuOption.tag === 'E') {
      await showModifyEnvVars(deployment);
    } else if (menuOption.tag === 'T') {
      await chooseTarget(deployment);
    } else if (menuOption.tag === 'D') {
      await deployNow(deployment);
    } else if (menuOption.tag === 'S') {
      deployment = await chooseDeployment();
    } else if (menuOption.tag === 'L') {
      await viewLogs(deployment);
    } else if (menuOption.tag === 'C') {
      await connectToDatabase(deployment);
    }
  }
};

// Start
showWizard();
