
/*
This program demonstrates the use of JOptionPane and
JFileChooser classes.
The program reads from a file  info about
pizzas available to order and prices, asks the user to select type,
size, and payment type.
Payment is not processed by this program.
Aditya Mathur
March 27, 2019
 */
import java.io.BufferedReader;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
public class FileReadExample {
    public static String title="Welcome to 180PizzaGrab";
    public static Pizza[]pm; //Menu
    final static int maxTypes=10;
    public static int typesAvailable=0;
    public static ImageIcon img=new ImageIcon("pizza.jpg");
    public static String []size={"Small", "Medium", "Large"};
    public static void main(String[] args)
            throws Exception {
        Dimension d=new Dimension(50,75);
        Font ft=new Font("Arial", Font.BOLD, 24);
        UIManager.put("OptionPane.minimumSize", d);
        UIManager.put("OptionPane.messageFont", ft);
        UIManager.put("OptionPane.buttonFont", ft);
        String f = getFileName();
        if (f!=null) {
            pm = createMenu(f);
            Pizza pizzaSelected = selectPizza(pm);
            int size = selectSize();
            displaySelection(pizzaSelected, size);
        }
        System.exit(0);
        }// End of main()

    public static String getFileName() {
       // JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
         JFileChooser jfc = new JFileChooser();
         int returnValue = jfc.showOpenDialog(null);
        //int returnValue = jfc.showSaveDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();
            String filepath=selectedFile.getAbsolutePath();
            System.out.println("File selected: "+filepath);
            return (filepath);
        } else {
            return (null);
        }
    }// End of getFileName()

    public static Pizza [] createMenu(String filename)
            throws Exception {
        Pizza [] menu=new Pizza[maxTypes];
        // open input filename for reading pizza info.
        FileReader fr = new FileReader(filename);
        BufferedReader br = new BufferedReader(fr);
        String nextLine = null;
        while ((nextLine = br.readLine()) != null) {
            if (typesAvailable<maxTypes) {
                String[] data = nextLine.split(" ");
                String pizzaType = data[0];
                float small = Float.parseFloat(data[1]);
                float medium = Float.parseFloat(data[2]);
                float large = Float.parseFloat(data[3]);
                float[] price = {small, medium, large};
                Pizza p = new Pizza(pizzaType, price);
                menu[typesAvailable]=p;
                typesAvailable++;
            }
        }// End of while
        return menu;
    }// End of createMenu()


    public static Pizza selectPizza(Pizza[] pm){
        //Object [] text=new Object[maxTypes];
        Object [] text=new Object[maxTypes];
        Object []msg=new Object[typesAvailable];
        Object []options=new Object[typesAvailable];
        Object []optionsAvail=new Object[typesAvailable];
        for (int i=0; i<typesAvailable; i++) {
            Pizza next = pm[i];
            if(next!=null) {
                Object nextPizza = next.getPizzaType();
                float[] priceList = next.getPizzaPrice();
                String price = " S: "+Float.toString(priceList[0]) + " M: " +
                        Float.toString(priceList[1]) + " L: " +
                        Float.toString(priceList[2]);
                String displayPizza = nextPizza + " " + price;
               // System.out.println(displayPizza);
                text[i] = displayPizza;
                options[i]=nextPizza;
            }
        }// End of loop
            int msgType=JOptionPane.PLAIN_MESSAGE;
            int opType=JOptionPane.YES_NO_OPTION;
            System.arraycopy(text,0,msg,0,
                    typesAvailable);
            System.arraycopy(options,0,optionsAvail,0,
                typesAvailable);
            /*
            for (int i=0; i<typesAvailable;i++){
               msg[i]=text[i];
               optionsAvail[i]=options[i];
            }
            */
            int selection=JOptionPane.showOptionDialog(null,msg,title,
                    opType, msgType,img, optionsAvail, optionsAvail[0]);

            if (selection<0){
                return null;
            }else {
                return pm[selection];
            }
    }// End of selectPizza()
    public static int selectSize(){
        int msgType=JOptionPane.QUESTION_MESSAGE;
        int opType=JOptionPane.YES_NO_OPTION;
        String msg="Select size";
        int selection=JOptionPane.showOptionDialog(null,msg,title,
                opType, msgType,img, size, size[2]);
        return selection;
    }
    public static void displaySelection(Pizza p, int sml){
       String pizzaType=p.getPizzaType();
       String pizzaSize=size[sml];
       String price=Float.toString(p.getPizzaPrice()[sml]);
       String [] msg={"Selected: "+pizzaSize+" "+
               pizzaType+ " to pay: "+ price, "Select Payment Type"};
        int msgType=JOptionPane.QUESTION_MESSAGE;
        int opType=JOptionPane.YES_NO_OPTION;
        Object []options={"Visa", "MasterCard", "Cash"};
        int selection=JOptionPane.showOptionDialog(null,msg,title,
                opType, msgType,img, options, options[2]);
        System.out.println("Payment type: "+options[selection]);
        System.out.println("Please wait for the server.");
    }

}// End of class
