package project;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.event.EventListenerList;
import javax.swing.table.AbstractTableModel;

public class GenericForm extends JPanel implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Participant participant=new Participant();
	JTextField fieldID;
	JTextField fieldAge;	
	RadioButtons fieldGender;
	JTextField fieldEthinicity;
	JTextField fieldOccupation;
	JTextField fieldCountry;
	JTextField fieldOrigin;
	JTextField fieldEducation;
	JTable table; 
	
	JButton saveButton;
	JButton cancelButton;
	
	int totalFields;
	
	JLabel[] labelArray;
	List<JComponent> fieldArray;
	Class[] fieldClass;
	int[] fieldSize;
	
	private EventListenerList listenersList=new EventListenerList();
	
	public GenericForm(Participant p){
		this();
		//participant=p;
		//populateForm(p);
		
	}
	public GenericForm(){
		
		fieldArray=new ArrayList<>();
		
		Dimension dimension=getPreferredSize();
		dimension.width=500;
		dimension.height=400;
		
		/* setting the border */
		//setBorder(compoundBorder);
		setBackground(new Color(186	,219,241));
		
		
		totalFields=16;
		/* Setting the GridBagLayout as Layout Manager for the Panel */
		setLayout(new GridBagLayout());
		GridBagConstraints gc=new GridBagConstraints();
		
		
		String[] labelNames={"ID","Status","FirstRelation Age","Longest Relation","Sexual Orientation","Parent Status","Partnership Duration","Principles","Religion","Children","Sibilings","Virgin","Health","Sex Life","Attractiveness","Happiness"};
		fieldClass=new Class[] {JTextField.class,JTextField.class,JTextField.class,JTextField.class,JTextField.class,JTextField.class,JTextField.class,JTextField.class,
								JTextField.class,JTextField.class,JTextField.class,RadioButtons.class,RadioButtons.class,RadioButtons.class,RadioButtons.class,RadioButtons.class};
		fieldSize=new int[] {5,15,5,10,10,10,10,15,10,5,5};
		
		labelArray=new JLabel[totalFields];
		//fieldArray=new JTextField[totalFields];
		
		//Laying out all the Label fields
		// define the GridBagContraints for the first column which is column=1 row=<?>(all rows)
		//The aim is to create n * 4 (row*columns)  Grid with the flexibility of varying width of the columns

		gc.anchor=GridBagConstraints.LINE_END;  //Right Align
		gc.insets=new Insets(5,5,5,5);   		//Margin 5 pixels from all the four sides(Top,Left,Bottom,Right)
		gc.weightx=0.5;							// Weightx determine the relative width from the other columns
		gc.weighty=0.5;							// Weighty determine the relative width from the other rows
		
		//Laying the 1st column Label Fields
		int column=0;
		int position;
		for (int i=0;i < totalFields/2;i++){
			gc.gridx=0 ;											// i row   i=0,1
			gc.gridy=i ;											// 1st column (which is index=0)

			position=i+(totalFields/2)*column;
			labelArray[position]=new JLabel(labelNames[position]);	//Create the i  Label Control
			add(labelArray[position],gc);								//Add the Label Control in the (i,0) in the Grid
		}
		
		//Laying the 3rd column Label Fields
		
		column=1;
		position=0;
		for (int i=0;i < totalFields/2;i++){
			gc.gridx=2 ;						// i row   i=0,1
			gc.gridy=i ;						// 3nd column (which is index=2)

			position=i+(totalFields/2)*column;
			labelArray[position]=new JLabel(labelNames[position]);
			add(labelArray[position],gc);
		}
		
		
		//Laying out all the JTextField fields
		// define the GridBagContraints for the first column which is column=1 row=<?>(all rows)
		//The aim is to create n * 4 (row*columns)  Grid with the flexibility of varying width of the columns
				
		gc.anchor=GridBagConstraints.LINE_START;  //Right Align
		gc.insets=new Insets(5,5,5,5);   		//Margin 5 pixels from all the four sides(Top,Left,Bottom,Right)
		gc.weightx=0.5;							// Weightx determine the relative width from the other columns
		gc.weighty=0.5;							// Weighty determine the relative width from the other rows
		
		//Laying the 2nd column Label Fields
		column=0;
		position=0;
		for (int i=0;i < totalFields/2;i++){
			gc.gridx=1 ;											// i row   i=0,1
			gc.gridy=i ;											// 2nd column (which is index=1)

					
			position=i+(totalFields/2)*column;
			if (fieldClass[position]==JTextField.class){
				//fieldArray[position]=new JTextField(fieldSize[position]);				//Create the i  JTextField Control
				fieldArray.add(position,new JTextField(fieldSize[position]));
			}
			else if (fieldClass[position]==RadioButtons.class){
				//fieldArray[position]=new RadioButtons(new String[] {"Yes","No"});
				fieldArray.add(position,new RadioButtons(new String[] {"Yes","No"},1));
				
			}
			
			add(fieldArray.get(position),gc);								//Add the Label Control in the (i,0) in the Grid
		}
				
				
		//Laying the 4th column JTextField Fields
		column=1;
		position=0;
		gc.weightx=2;
		for (int i=0;i < totalFields/2;i++){
			gc.gridx=3 ;						// i row   i=0,1
			gc.gridy=i ;						// 4th column (which is index=3)

						
			position=i+(totalFields/2)*column;
			if (fieldClass[position]==JTextField.class){
				//fieldArray[position]=new JTextField(fieldSize[position]);				//Create the i  JTextField Control
				fieldArray.add(position,new JTextField(fieldSize[position]));
			}
			else if (fieldClass[position]==RadioButtons.class){
			//	fieldArray[position]=new RadioButtons(new String[] {"Yes","No"});
				//fieldArray.add(position,new RadioButtons(new String[] {"1","2","3","4","5"}));
			}
			 add(fieldArray.get(position),gc);							//Add the Label Control in the (i,0) in the Grid
		}
		
		gc.gridx=1;
		gc.gridy=8;
		gc.weighty=20;
		gc.weightx=0.5;
		//gc.gridwidth=2;
		gc.anchor=GridBagConstraints.FIRST_LINE_START;
		saveButton=new JButton("Save");
		add(saveButton,gc);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("save")){
			bindFormFields(participant);
			this.fireFormSaveEvent(new FormSaveEvent(this, "formDeatils"));
		}
		else if (e.getActionCommand().equals("view"))
			populateForm(participant);
		int row=table.getSelectedRow();
		int col=table.getSelectedColumn();
		
		for (int i=0 ;i<8;i++){
			((AbstractTableModel)table.getModel()).fireTableCellUpdated(row,i);
		}
		
		if (participant.getId()==0){
			((MyModel)table.getModel()).addRecord(participant);
		}
		
		
	}
	
	public void bindFormFields(Participant p)
	{
		if (p.id==0){
			p.setAge(Integer.parseInt(fieldAge.getText()));
			p.setGender(fieldGender.getSelectedValue());
			p.setCountry(fieldCountry.getText());
			p.setEducation(fieldEducation.getText());
			p.setEthnicity(fieldEthinicity.getText());
			p.setOccupation(fieldOccupation.getText());
			p.setOrigin(fieldOrigin.getText());	
			
		}
		else{
			
			p.setAge(Integer.parseInt(fieldAge.getText()));
			p.setGender(fieldGender.getSelectedValue());
			p.setCountry(fieldCountry.getText());
			p.setEducation(fieldEducation.getText());
			p.setEthnicity(fieldEthinicity.getText());
			p.setOccupation(fieldOccupation.getText());
			p.setOrigin(fieldOrigin.getText());	
		}
		
		System.out.println(p);
		
	}
	
	public void populateForm(Participant p){
		if (p.getId()!=0){
			participant=p;
			fieldID.setText(Integer.toString(p.getId()));
			fieldAge.setText(Integer.toString(p.getAge()));
			fieldEthinicity.setText(p.getEthnicity());
			fieldOccupation.setText(p.getOccupation());
			fieldCountry.setText(p.getCountry());
			fieldOrigin.setText(p.getOrigin());
			fieldEducation.setText(p.getEducation());
		}
		else{
			participant=p;
			fieldID.setText("");
			fieldAge.setText("");
			fieldEthinicity.setText("");
			fieldOccupation.setText("");
			fieldCountry.setText("");
			fieldOrigin.setText("");
			fieldEducation.setText("");

		}
			
		
	}
	
	public void setTable(JTable table){
		this.table=table;
	}
	
	public void fireFormSaveEvent(FormSaveEvent event){
		Object[] listeners=listenersList.getListenerList();
		System.out.println("Listeners count= "+listenersList.getListenerCount());
		System.out.println("Listeners count= "+listeners.length);
		for (int i=0;i<listeners.length;i +=2){
			if (listeners[i]==FormSaveListener.class){
				((FormSaveListener)listeners[i+1]).FormSaveOccured(event);
			}
			
		}
	}
	
	public void addFormSaveListener(FormSaveListener listener){
		listenersList.add(FormSaveListener.class, listener);
		System.out.println("Listeners count= "+listenersList.getListenerCount());
	}
	
	public void removeFormSaveListener(FormSaveListener listener){
		listenersList.remove(FormSaveListener.class, listener);
	}
	
}
