import { Choice } from 'prompts';
import * as si from 'systeminformation';
import { keyMirror, promptForInput } from '../../../util';

export const Destination = keyMirror({
  YOUR_LOCAL_COMPUTER: null,
  REMOTE_DEVICE: null
});

type Destination = keyof typeof Destination;

export async function checkProcessorBrand() {
  const architecture = await si.cpu();
  const brand = architecture.brand;
  if (
    brand.toLowerCase().includes('celeron') ||
    brand.toLowerCase().includes('atom')
  ) {
    return false;
  }

  return true;
}

export async function destinationPromptComponent(props: {
  prevDestination?: Destination;
}) {
  const choices: Choice[] = [];
  if (await checkProcessorBrand()) {
    choices.push({
      title: 'Your local computer',
      value: Destination.YOUR_LOCAL_COMPUTER
    });
  } else {
    choices.push({
      title: 'Your local computer (Not available)',
      value: Destination.YOUR_LOCAL_COMPUTER,
      disabled: true
    });
  }
  choices.push({
    title: 'Remote device',
    value: Destination.REMOTE_DEVICE
  });

  const foundChoiceIndex = choices.findIndex(
    (choice) => choice.value === props.prevDestination
  );
  const initial = foundChoiceIndex > -1 ? foundChoiceIndex : 0;

  const answer = await promptForInput({
    purpose:
      'to choose whether you want to run your application here on this computer or on a remote device',
    questions: [
      {
        type: 'select',
        name: 'destination',
        message: 'What is the destination?',
        initial,
        choices,
        warn: 'edgeIQ does not support Atom/Celeron powered devices.'
      }
    ]
  });

  const destination: Destination = answer.destination;
  return destination;
}
