package scheduleGenerator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

import javax.swing.*;

/**
 * Creates PeopleFramework object.
 *
 * @author maurerja.
 *         Created Oct 22, 2012.
 */
public class PeopleFramework extends JFrame implements ActionListener {
	
	JButton submit;
	JPanel topPanel;
	JPanel middlePanel;
	JPanel bottomPanel;
	JLabel label;
	JTextField namebox;
	ArrayList<JComboBox<String>> yeararray;
	ArrayList<JComboBox<String>> Montharray;
	ArrayList<JComboBox<String>> Dayarray;
	ArrayList<JComboBox<String>> timearray;
	ArrayList<JCheckBox> jobhold = new ArrayList<JCheckBox>();
	JButton newdatetime;
	GridLayout gl;
	GridBagLayout gbl;
	GridBagConstraints c ;
	JPanel panel2;
	JPanel jobsP;
	ArrayList<String> days;
	
	/**
	 * Frame that allows user to add additional people.
	 *
	 */
	public PeopleFramework(){
		
		this.setBounds(100,100,300,500);
		this.gbl  = new GridBagLayout();
		this.gl= new GridLayout(0,4);
		this.topPanel = new JPanel();
		this.label = new JLabel("Please Enter Name, Jobs, And Hours");
		this.topPanel.add(this.label);
		this.topPanel.setSize(300,50);
		this.add(this.topPanel,BorderLayout.NORTH);
		this.c = new GridBagConstraints();
		this.bottomPanel = new JPanel();
		this.bottomPanel.setSize(300,50);
		this.submit = new JButton("Submit");
		this.submit.addActionListener(this);
		this.submit.setSize(50,50);
		this.bottomPanel.add(this.submit);
		this.newdatetime = new JButton("New Date/Time");
		this.newdatetime.setSize(50,50);
		this.newdatetime.addActionListener(this);
		this.bottomPanel.add(this.newdatetime);
		this.add(this.bottomPanel,BorderLayout.SOUTH);
		
		this.middlePanel= new JPanel();
		this.middlePanel.setLayout(this.gbl);
		this.namebox= new JTextField();
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.anchor= GridBagConstraints.FIRST_LINE_START;
		this.c.gridx=0;
		this.c.weightx=1;
		this.c.gridwidth = 3;
		this.c.gridy=0;
		this.middlePanel.add(this.namebox,this.c);
		this.jobsP = new JPanel();
		int i=0;
		String lineHolder;
		String jobHolder[] = null;
		boolean c = true;
		try {
			Scanner jobReader = new Scanner(new File("sampleconfig.txt"));
			while(jobReader.hasNextLine()&&c){
				lineHolder = jobReader.nextLine();
				if(!lineHolder.equals("")){
					if(lineHolder.substring(0,1).equals("%")){
						lineHolder = lineHolder.substring(1);
						jobHolder = lineHolder.split(",");
						c = false;
					}
				}
			}
			
		} catch (FileNotFoundException exception) {
			System.out.println("You need to create a configuration fill first");
		}
		this.jobsP.setLayout(new GridLayout(4,3));
		this.jobsP.add(new JLabel());
		this.jobsP.add(new JLabel());
		this.jobsP.add(new JLabel());
		for(int d = 0; d < jobHolder.length; d++){
			jobhold.add(new JCheckBox(jobHolder[d]));
			this.jobsP.add(jobhold.get(d));
		}
		this.middlePanel.add(this.jobsP, this.c);
		this.panel2 = new JPanel();
		this.panel2.setLayout(this.gl);
		this.panel2.setAutoscrolls(true);
		this.yeararray= new ArrayList<JComboBox<String>>();
		this.yeararray.add(new JComboBox<String>());
		Calendar y = new GregorianCalendar();
		Calendar.getInstance();
		String[] years = {""+y.get(Calendar.YEAR),""+(y.get(Calendar.YEAR)+1),""+(y.get(Calendar.YEAR)+2),""+(y.get(Calendar.YEAR)+3),""+(y.get(Calendar.YEAR)+4)};
		for(String s:years)this.yeararray.get(0).addItem(s);
		for(JComboBox<String> cb:this.yeararray){
		this.panel2.add(cb);
		}
		this.Montharray= new ArrayList<JComboBox<String>>();
		this.Montharray.add(new JComboBox<String>());
		String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
		for(String s:months)this.Montharray.get(0).addItem(s);
		for(JComboBox<String> cb:this.Montharray){
		this.panel2.add(cb);
		}
		this.Dayarray= new ArrayList<JComboBox<String>>();
		this.Dayarray.add(new JComboBox<String>());
		String[] day = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
				"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
		this.days = new ArrayList<String>();
		for (String s:day)this.days.add(s);
		for(String s:this.days) this.Dayarray.get(0).addItem(s);
		for(JComboBox<String> cb:this.Dayarray){
		this.panel2.add(cb);
		}
		this.timearray= new ArrayList<JComboBox<String>>();
		this.timearray.add(new JComboBox<String>());
		String[] time = {"AM","PM","AM & PM"};
		for(String s:time) this.timearray.get(0).addItem(s);
		for(JComboBox<String> cb:this.timearray){
		this.panel2.add(cb);
		}
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.weighty=1.0;
		this.c.weightx=.2;
		this.c.gridx=1;
		this.c.gridy=1;
		this.middlePanel.add(this.panel2,this.c);
		this.middlePanel.setAlignmentX((float) 10.0);
		this.add(this.middlePanel,BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.newdatetime){
			this.yeararray.add(new JComboBox<String>());
			Calendar y = new GregorianCalendar();
			Calendar.getInstance();
			String[] years = {""+y.get(Calendar.YEAR),""+(y.get(Calendar.YEAR)+1),""+(y.get(Calendar.YEAR)+2),""+(y.get(Calendar.YEAR)+3),""+(y.get(Calendar.YEAR)+4)};
			for(String s:years)this.yeararray.get(this.yeararray.size()-1).addItem(s);
			this.panel2.add(this.yeararray.get(this.yeararray.size()-1));
			this.Montharray.add(new JComboBox<String>());
			String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
			for(String s:months)this.Montharray.get(this.Montharray.size()-1).addItem(s);
			this.panel2.add(this.Montharray.get(this.Montharray.size()-1));
			this.Dayarray.add(new JComboBox<String>());
			String[] day = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
					"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
			this.days = new ArrayList<String>();
			for (String s:day)this.days.add(s);
			for(String s:this.days) this.Dayarray.get(this.Dayarray.size()-1).addItem(s);
			this.panel2.add(this.Dayarray.get(this.Dayarray.size()-1));
			this.timearray.add(new JComboBox<String>());
			String[] time = {"AM","PM","AM & PM"};
			for(String s:time) this.timearray.get(this.timearray.size()-1).addItem(s);
			this.panel2.add(this.timearray.get(this.timearray.size()-1));
			this.middlePanel.add(this.panel2,this.c);
			this.add(this.middlePanel,BorderLayout.CENTER);
			this.setVisible(true);
		}
		else if((e.getSource().equals(this.submit))){
			try {
				ArrayList<String> temp = new ArrayList<String>();
				Scanner y = new Scanner(new File("sampleInput.txt"));
				while(y.hasNextLine()){
					temp.add(y.nextLine());
				}
				PrintStream x = new PrintStream(new File("sampleInput.txt"));
				for(int k = 0; k<temp.size();k++){
					x.println(temp.get(k));
				}
				while(y.hasNextLine()){
					x.println(y.nextLine());
				}
				
				x.println(":" + this.namebox.getText());
				
				for(int j = 0; j<this.yeararray.size();j++){
					x.println("+" + ((String) this.Montharray.get(j).getSelectedItem()).substring(0,3) + " " + (this.Dayarray.get(j).getSelectedIndex()+1) + ", " + this.yeararray.get(j).getSelectedItem());
					if(this.timearray.get(j).getSelectedItem().equals("AM")){
						x.println("~am");
					}
					else if(this.timearray.get(j).getSelectedItem().equals("PM")){
						x.println("~pm");
					}
					else{
						x.println("~am");
						x.println("~pm");
					}
					for(int k = 0; k < this.jobhold.size();k++){
						if(this.jobhold.get(k).isSelected()){
							x.println("!" + this.jobhold.get(k).getText());
						}
					}
					x.print("%");
				}
				x.close();
			} catch (FileNotFoundException exception) {
				// TODO Auto-generated catch-block stub.
				exception.printStackTrace();
			}
		}
	}

}
