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 GenericForm3 extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	int totalFields;
	List<JComponent> fieldList;
	FormConfiguration formConfigurator;
	JButton saveButton;
	JTable table;
	
	private EventListenerList listenersList=new EventListenerList();
	
	public GenericForm3(){
		
	}
	public GenericForm3(FormConfiguration formConfigurator){
		this.formConfigurator=formConfigurator;
		this.addFormSaveListener(formConfigurator);
		totalFields=formConfigurator.getTotalFields();
		int gridSize=formConfigurator.getGridSize();
		
		fieldList=new ArrayList<>();
		
		Dimension dimension=getPreferredSize();
		dimension.width=500;
		dimension.height=400;
		
		/* setting the border */
		//setBorder(compoundBorder);
		//setBackground(new Color(186	,219,241));
		//setBackground(new Color(225,232,237));
		setBackground(formConfigurator.formColor);
		
		
		/* Setting the GridBagLayout as Layout Manager for the Panel */
		setLayout(new GridBagLayout());
		GridBagConstraints gc=new GridBagConstraints();
		
		//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(formConfigurator.getLabel(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
		
		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=5*gridSize;													// 2nd column (which is index=1)
				position=i+(totalFields/gridSize)*j;
				
				JComponent field=formConfigurator.getField1(position);
				if ((field instanceof JTextArea)  ||(field instanceof RoundedTextArea)) {
					JScrollPane sPane=new JScrollPane(field,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					add(sPane,gc);
				}
				else add(field,gc);
				fieldList.add(position, field);
				//addFieldAttributes(position);
												//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");
		saveButton.setActionCommand("SAVE");
		saveButton.addActionListener(this);
		saveButton.setEnabled(formConfigurator.isEdit);
		add(saveButton,gc);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("SAVE")){
			this.fireFormSaveEvent(new FormSaveEvent(this, "formDeatils"));
		}
		if (table !=null) {
			int row=table.getSelectedRow();
			//int col=table.getSelectedColumn();
			for (int i=0 ;i<8;i++){
				((AbstractTableModel)table.getModel()).fireTableCellUpdated(row,i);
			}
		}
		
	}
	
	
	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);
	}
	
	public JComponent getFieldComponent(int index){
		return fieldList.get(index);
	}
	
	public void setConfigurator(FormConfiguration formConfigurator){
		this.formConfigurator=formConfigurator;
	}
	
	public FormConfiguration getFormConfigurator(){
		return formConfigurator;
	}
	
	public void bind(Object formDataObject){
		formConfigurator.bind(formDataObject, this);
	}
	
	public void setTable(JTable table){
		this.table=table;
	}
	
	public void addFieldAttributes(int position){
		if (position==0){
		((RoundedTextField)fieldList.get(position)).setEnabled(false);
		}
		int verifier=formConfigurator.getVerifiers()[position];
		if (position!=0){
			switch (verifier){
				case 1 : 	((RoundedTextField)fieldList.get(position)).setInputVerifier(new NumericVerifier());
							break;
				case 2 : 	((RoundedTextField)fieldList.get(position)).setInputVerifier(new TextVerifier());
							break;
			}
		}
		
	}
	
}
