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.Arrays;
import java.util.Hashtable;
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 GenericForm1 extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	Participant participant=new Participant();
	JTable table;
	
	JButton saveButton;
	
	int totalFields;
	JLabel[] labelArray;
	List<JComponent> fieldArray;
	Class[] fieldClass;
	int[] fieldSize;
	
	private EventListenerList listenersList=new EventListenerList();
	
	public GenericForm1(Participant p){
		this();
		//participant=p;
		//populateForm(p);
		
	}
	public GenericForm1(){
		
		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;
		int gridSize=2;
		/* Setting the GridBagLayout as Layout Manager for the Panel */
		setLayout(new GridBagLayout());
		GridBagConstraints gc=new GridBagConstraints();
		
		
		String[] labelNames={"ID","FirstRelationship Age","Status","Longest Relation","Sexual Orientation","Parent Status","Partnership Duration","Principles","Religion","Children","Sibilings","Virgin","Health","Sex Life","Attractiveness","Happiness"};
		fieldClass=new Class[] {RoundedTextField.class,RoundedTextField.class,RoundedTextField.class,RoundedTextField.class,RoundedTextField.class,RoundedTextField.class,RoundedTextField.class,RoundedTextArea.class,
				RoundedTextField.class,RoundedTextField.class,RoundedTextField.class,RadioButtons.class,RadioButtons.class,RadioButtons.class,RadioButtons.class,RadioButtons.class};
		fieldSize=new int[] {5,5,15,15,15,15,15,20,10,5,5};
		
		Hashtable<Integer, List<String>> ht=new Hashtable<>();
		List<String> list=Arrays.asList("Yes","No");
		ht.put(11,list);
		list=Arrays.asList("1","2","3","4","5");
		ht.put(12,list);
		ht.put(13,list);
		ht.put(14,list);
		ht.put(15,list);
		
		
		
		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.FIRST_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 position;
		for(int j=0;j<gridSize;j++){
			for (int i=0;i < totalFields/gridSize;i++){
				gc.gridx=gridSize*j ;											// i row   i=0,1
				gc.gridy=i ;											// 1st column (which is index=0)

				position=i+(totalFields/gridSize)*j;
				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 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.FIRST_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
		position=0;
		for(int j=0;j<gridSize;j++){
			for (int i=0;i < totalFields/gridSize;i++){
				
				gc.gridx=gridSize*j+1 ;											// i row   i=0,1
				gc.gridy=i ;		
				if (j==gridSize-1) gc.weightx=3*gridSize;													// 2nd column (which is index=1)
				position=i+(totalFields/gridSize)*j;
				
				if (fieldClass[position]==JTextField.class){
					fieldArray.add(position,new JTextField(fieldSize[position]));
				}
				else if (fieldClass[position]==RoundedTextField.class){
					fieldArray.add(position,new RoundedTextField("",fieldSize[position]));
				}
				else if (fieldClass[position]==RoundedTextArea.class){
					fieldArray.add(position,new RoundedTextArea(3,fieldSize[position]));
				}
				else if (fieldClass[position]==RadioButtons.class){
					String[] options=(String[])(ht.get(position).toArray());
					
					//fieldArray.add(position,new RadioButtons(options));
				}
			
				add(fieldArray.get(position),gc);								//Add the Label Control in the (i,0) in the Grid
			}
		}
				
		gc.gridx=1;
		gc.gridy=totalFields;
		gc.weighty=15;
		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){
			
			
		}
		else{
			
			
		}
		
		System.out.println(p);
		
	}
	
	public void populateForm(Participant p){
		if (p.getId()!=0){
		
		}
		else{
			
		}
			
		
	}
	
	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);
	}
	
}
