package WheelOfFortune.storage;

import java.io.*;
import java.util.*;
import javax.swing.table.*;

//A class for utility methods
public class Utilities {
 public static boolean isVowel(char c) {
  if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') return true;
  return false;
 }
 
 public static boolean isConsonant(char c) {
  return !isVowel(c);
 }

 public static String getAlphabetString() {
  return "abcdefghijklmnopqrstuvwxyz".toUpperCase();
 }

 //return all letters that are not in the given string
 public static String lettersNotIn(String s) {
  String alphabet = getAlphabetString();
  String result = "";
  for(int i = 0;i < alphabet.length();++i) {
   if(s.indexOf(alphabet.charAt(i)) == -1) result += alphabet.charAt(i);
  }
  return result;
 }

 public static String fileToString(String fileName) throws IOException, FileNotFoundException {
  FileReader f = null;
  f = new FileReader(fileName);

  BufferedReader br = new BufferedReader(f);

  int temp = -1;
  temp = br.read();
  StringBuffer complete = new StringBuffer();
  while(temp != -1) {
   complete.append((char)temp);
   temp = br.read();
  }
  return complete.toString();
 }

 public static Vector fileToStringVector(String fileName) throws IOException, FileNotFoundException {
  FileReader f = null;
  f = new FileReader(fileName);

  BufferedReader br = new BufferedReader(f);

  Vector v = new Vector();

  String temp = br.readLine();
  while(temp != null) {
   v.add(temp);
   temp = br.readLine();
  }
  return v;
 }


 public static String[] fileToStrings(String fileName) throws IOException, FileNotFoundException {
  FileReader f = null;
  f = new FileReader(fileName);

  BufferedReader br = new BufferedReader(f);

  Vector v = new Vector();

  String temp = br.readLine();
  while(temp != null) {
   v.add(temp);
   temp = br.readLine();
  }
  return (String[])v.toArray(new String[0]);
 }

 public static TableModel fileToTableModel(String fileName) throws IOException, FileNotFoundException {
  FileReader f = null;
  f = new FileReader(fileName);

  BufferedReader br = new BufferedReader(f);

  Vector v = new Vector();

  String temp = br.readLine();
  while(temp != null) {
   v.add(temp);
   temp = br.readLine();
  }
  return null;
 }



 public static Vector getPuzzleVector(String filename)
throws IOException, FileNotFoundException {
  FileReader f = null;
  f = new FileReader(filename);

  BufferedReader br = new BufferedReader(f);

  Vector v = new Vector();
Vector vtemp;
StringTokenizer st;


  //Skip over header line
  String temp = br.readLine();

  temp = br.readLine();
  while(temp != null) {
   vtemp = new Vector();
   st = new StringTokenizer(temp, "\\",false);
   try {   vtemp.add(st.nextToken()); }
   catch(NoSuchElementException nsee) { vtemp.add(""); }
   try {   vtemp.add(st.nextToken()); }
   catch(NoSuchElementException nsee) { vtemp.add(""); }
   v.add(vtemp);
   temp = br.readLine();
  }
  return v;
 }

 public static void storePuzzles(Vector v, String filename) throws IOException {
  int numPuzzles = v.size();
  FileWriter fw = new FileWriter(filename);
  Vector temp;
  String solution;
  String category;
  String s;
  
  String numString = "NumPuzzles: " + numPuzzles + "\n";
  fw.write(numString,0,numString.length());
  
  for(int i = 0;i < v.size();++i) {
   temp = (Vector)v.elementAt(i);
   solution = (String)temp.elementAt(0);
   category = (String)temp.elementAt(1);
   s = solution + "\\" + category + "\n";
   fw.write(s,0,s.length());
  }
  fw.close();
 }
}