/*
 * responsible of inputting
 * the data from the command line
 */
package TruthTableSolver.cmdIn;

import TruthTableSolver.solve.SolutionKind;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Prompt {

private int[] values; //the truth table values
private int number_of_terms;
private List<String> termNames;
private SolutionKind sum_of_products_or_product_of_sums; // 0 for sum of products for product of sums
private boolean allPossible;

public void promptUser() throws Exception {
  opening();
  number_of_terms = promptNumberOfTerms();
  termNames = new ArrayList<>();
  values = new int[(int) Math.pow(2, number_of_terms)];
  sum_of_products_or_product_of_sums = SolutionKind.SUM_OF_PRODUCTS;
  promptTermsNames();
  promptValues();
  promptSumOfProductsOrProductOfSums();
  promptOneAllPossibleSolutionsOrOneSolution();
}

void opening() {
  System.out.println("This program solves the truth table and output all the possible minimized  \nequations.\n");
}

/*
 *promptNumberOfTerms asks user to input number of terms
 */
private int promptNumberOfTerms() throws Exception {
  int number;
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("enter the number of terms : ");
  try {
    number = Integer.parseInt(in.readLine());
  }
  catch (NumberFormatException e) {
    System.out.println("\nerror : enter a positive integer.\n");
    number = promptNumberOfTerms();
  }
  if (number <= 0) {
    System.out.println("\nerror : enter a positive integer.\n");
    number = promptNumberOfTerms();
  }
  return number;
}

private void promptTermsNames() throws Exception {
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.println();
  for (int i = 0; i < termNames.size(); i++) {
    System.out.print("enter name of term[" + (i + 1) + "] : ");
    termNames.add(in.readLine());
  }
}

/*
 *promptValues asks user to input the truth table values
 */
private void promptValues() throws Exception {
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  String temp;
  System.out.println();
  for (int i = 0; i < values.length; i++) {
    System.out.print("enter the value[" + i + "] : ");
    temp = in.readLine();
    switch (temp) {
      case "0":
        values[i] = 0;
        break;
      case "1":
        values[i] = 1;
        break;
      case "d":
        values[i] = 2;
        break;
      default:
        System.out.println("\nerror : enter 0 or 1 or d.\n");
        i--;
        break;
    }
  }
}

/*
 *promptSumOfProductsOrProductOfSums asks user if he want the answer as sum of products or product of sums
 */
private void promptSumOfProductsOrProductOfSums() throws Exception {
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  String temp;
  System.out.println();
  do {
    System.out.print("enter 1 for sum of products or 2 for product of sums : ");
    temp = in.readLine();
    switch (temp) {
      case "1":
        sum_of_products_or_product_of_sums = SolutionKind.SUM_OF_PRODUCTS;
        break;
      case "2":
        sum_of_products_or_product_of_sums = SolutionKind.PRODUCT_OF_SUMS;
        break;
      default:
        System.out.println("\nerror : enter 1 for sum of products or 2 for product of sums.\n");
        break;
    }
  } while (!temp.equals("1") && !temp.equals("2"));
}

private void promptOneAllPossibleSolutionsOrOneSolution() throws Exception {
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  String temp;
  System.out.println();
  do {
    System.out.print("enter 1 for (all possible solutions[occasionaly slow]) or \n2 (one solution only[fast]): ");
    temp = in.readLine();
    switch (temp) {
      case "1":
        allPossible = false;
        break;
      case "2":
        allPossible = true;
        break;
      default:
        System.out.println("\nerror : enter 1 for (all possible solutions[occasionaly slow]) or 2 (one solution only[fast]) .\n");
        break;
    }
  } while (!temp.equals("1") && !temp.equals("2"));
}

public int[] getValues() {
  return values;
}

public int getNumberOfTerms() {
  return number_of_terms;
}

public List<String> getTermsNames() {
  return termNames;
}

public SolutionKind getSumOfProductsOrProductOfSums() {
  return sum_of_products_or_product_of_sums;
}

public boolean getOneAllPossibleSolutionsOrOneSolution() {
  return allPossible;
}
}
