package project;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;

public class UIFactory {
	
	
	
	public static JTabbedPane createTabbedFormPanel(Map<String,GenericForm3> forms){
		
		GenericForm3 participantForm=forms.get("Participant");
		GenericForm3 relationshipForm=forms.get("Relationship");
		GenericForm3 interestForm=forms.get("Interests");
		
		
		JTabbedPane tabbedPane=new JTabbedPane();
		tabbedPane.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
		tabbedPane.setTabPlacement(JTabbedPane.TOP);
		
		JScrollPane sPane1=new JScrollPane(participantForm);
		JScrollPane sPane2=new JScrollPane(relationshipForm);
		JScrollPane sPane3=new JScrollPane(interestForm);
		sPane1.setBorder(null);
		sPane2.setBorder(null);
		sPane3.setBorder(null);
		
		tabbedPane.add("Participant", sPane1);
		tabbedPane.add("Relatioships", sPane2);
		tabbedPane.add("Interests", sPane3);
		return tabbedPane;
		
	}
	
	public static JTabbedPane createTabbedFormPanel1(Map<String,GenericForm3> forms){
		
		JTabbedPane tabbedPane=new JTabbedPane();
		tabbedPane.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
		tabbedPane.setTabPlacement(JTabbedPane.RIGHT);
		
		for (String key : forms.keySet()){
			GenericForm3 form=forms.get(key);
			JScrollPane sPane=new JScrollPane(form);
			sPane.setBorder(null);
			tabbedPane.add(key, sPane);
		}
		
		return tabbedPane;
		
	}
	
	
	public static   JPanel createTitlePanel(){
		
		JPanel  titlePanel=new JPanel();
		titlePanel.setPreferredSize(new Dimension(200,50));
		titlePanel.setBorder(new CustomBorder(2,Color.blue));
		titlePanel.setLayout(new GridBagLayout());
		GridBagConstraints gc=new GridBagConstraints();
		
		JLabel label=new JLabel("Please Enter Search Text");
		RoundedTextField searchField=new RoundedTextField("",300);
		searchField.setPreferredSize(new Dimension(300,20));
		JButton searchButton=new JButton("Search");
		searchButton.setForeground(new Color(255,255,255));
		searchButton.setBackground(new Color(0,0,255));
		
		gc.insets.set(12, 0, 10, 10);
		gc.weightx=0.5;
		gc.weighty=0.5;
		gc.anchor=GridBagConstraints.FIRST_LINE_END;
		
		gc.gridx=0;
		gc.gridy=0;
		titlePanel.add(label, gc);
		
		gc.insets.set(10, 0, 10, 0);
		gc.anchor=GridBagConstraints.FIRST_LINE_START;
		gc.fill=GridBagConstraints.HORIZONTAL;
		gc.gridx=1;
		gc.gridy=0;
		gc.weightx=5.0;
		titlePanel.add(searchField, gc);
		
		gc.insets.set(6, 0, 10, 0);
		gc.fill=GridBagConstraints.NONE;
		gc.anchor=GridBagConstraints.FIRST_LINE_START;
		gc.weightx=0.5;
		gc.gridx=2;
		gc.gridy=0;
		titlePanel.add(searchButton, gc);
		
		return titlePanel;
	}
	
	
	public static JPanel createNavigationPanel(ActionListener actionListner){
		
		JPanel  navPanel=new JPanel();
		navPanel.setPreferredSize(new Dimension(200,200));
		//navPanel.setBorder(BorderFactory.createLineBorder(Color.black,1));
		//navPanel.setBackground(new Color(186,219,241));
		navPanel.setBackground(new Color(225,232,237));
		navPanel.setBorder(new CustomBorder(2,Color.blue));
		navPanel.setLayout(new GridBagLayout());
		GridBagConstraints gc=new GridBagConstraints();
		
		JButton homeButton=new JButton("HOME");
		homeButton.addActionListener(actionListner);
		homeButton.setActionCommand("home");
		homeButton.setForeground(new Color(255,255,255));
		homeButton.setBackground(new Color(0,0,255));
		
		
		JButton uploadButton=new JButton("Batch Upload");
		uploadButton.setForeground(new Color(255,255,255));
		uploadButton.setBackground(new Color(0,0,255));
		
		JButton viewAllButton=new JButton("Show All Pariticipants");
		viewAllButton.setActionCommand("Show All");
		viewAllButton.addActionListener(actionListner);
		viewAllButton.setForeground(new Color(255,255,255));
		viewAllButton.setBackground(new Color(0,0,255));
		
		JButton viewTop5Button=new JButton("Show Top5 Pariticipants");
		viewTop5Button.setForeground(new Color(255,255,255));
		viewTop5Button.setBackground(new Color(0,0,255));
		viewTop5Button.addActionListener(actionListner);
		viewTop5Button.setActionCommand("Top5");
		
		JButton viewTop10Button=new JButton("Show Top10 Pariticipants");
		viewTop10Button.setForeground(new Color(255,255,255));
		viewTop10Button.setBackground(new Color(0,0,255));
		viewTop10Button.addActionListener(actionListner);
		viewTop10Button.setActionCommand("Top10");
		
		JButton viewBottom5Button=new JButton("Show Bottom5 Pariticipants");
		viewBottom5Button.setForeground(new Color(255,255,255));
		viewBottom5Button.setBackground(new Color(0,0,255));
		viewBottom5Button.addActionListener(actionListner);
		viewBottom5Button.setActionCommand("Bottom5");
		
		JButton viewBottom10Button=new JButton("Show Bottom10 Pariticipants");
		viewBottom10Button.setForeground(new Color(255,255,255));
		viewBottom10Button.setBackground(new Color(0,0,255));
		viewBottom10Button.addActionListener(actionListner);
		viewBottom10Button.setActionCommand("Bottom10");
		
		JButton addParticipantButton=new JButton("ADD New Participant");
		addParticipantButton.setActionCommand("AddParticipant");
		addParticipantButton.addActionListener(actionListner);
		addParticipantButton.setForeground(new Color(255,255,255));
		addParticipantButton.setBackground(new Color(0,0,255));
		
		JButton removeParticipantButton=new JButton("Remove Participant");
		removeParticipantButton.setActionCommand("RemoveParticipant");
		removeParticipantButton.addActionListener(actionListner);
		removeParticipantButton.setForeground(new Color(255,255,255));
		removeParticipantButton.setBackground(new Color(0,0,255));
		
		gc.insets.set(5, 5, 5, 5);
		gc.weightx=0.5;
		gc.weighty=0.5;
		gc.anchor=GridBagConstraints.CENTER;
		gc.fill=GridBagConstraints.HORIZONTAL;
		
		gc.gridx=0;
		gc.gridy=0;
		navPanel.add(homeButton,gc);
		
		gc.gridx=0;
		gc.gridy=1;
		navPanel.add(uploadButton,gc);
		
		gc.gridx=0;
		gc.gridy=2;
		navPanel.add(viewAllButton,gc);
		
		gc.gridx=0;
		gc.gridy=3;
		navPanel.add(viewTop5Button,gc);
		
		gc.gridx=0;
		gc.gridy=4;
		navPanel.add(viewTop10Button,gc);
		
		gc.gridx=0;
		gc.gridy=5;
		navPanel.add(viewBottom5Button,gc);
		
		gc.gridx=0;
		gc.gridy=6;
		navPanel.add(viewBottom10Button,gc);
		
		gc.gridx=0;
		gc.gridy=7;
		navPanel.add(addParticipantButton,gc);
		
		gc.anchor=GridBagConstraints.PAGE_START;
		gc.weighty=50;
		gc.gridx=0;
		gc.gridy=8;
		navPanel.add(removeParticipantButton,gc);
	
		
		return navPanel;
	}
	
	public static  JMenuBar createMenuBar(){
		JMenuBar menuBar=new JMenuBar();
		JMenu file=new JMenu("File");
		menuBar.add(file);
		JMenuItem newParticipant=new JMenuItem("New Participant");
		JMenuItem removeParticipant=new JMenuItem("Remove Participant");
		JMenuItem viewAll=new JMenuItem("Show All");
		JMenuItem upload=new JMenuItem("Batch Upload");
		JMenuItem exit=new JMenuItem("Exit");
		file.add(newParticipant);
		file.addSeparator();
		file.add(removeParticipant);
		file.addSeparator();
		file.add(viewAll);
		file.addSeparator();
		file.add(upload);
		file.addSeparator();
		file.add(exit);
		
		JMenu help=new JMenu("Help");
		menuBar.add(help);
		JMenuItem appHelp=new JMenuItem("Application Help");
		help.add(appHelp);

		return menuBar;
	}
	
	public static JPanel  createFooterPanel(ActionListener listener){
		JPanel footerPanel=new JPanel();
		footerPanel.setBackground(Color.blue);
		footerPanel.setPreferredSize(new Dimension(300,50));
		footerPanel.setBackground(new Color(225,232,237));
		footerPanel.setBorder(new CustomBorder(2,Color.blue));
		JButton next=new JButton("NEXT");
		next.addActionListener(listener);
		next.setActionCommand("next");
		JButton prev=new JButton("PREV");
		prev.setActionCommand("prev");
		prev.addActionListener(listener);
		footerPanel.add(next);
		footerPanel.add(prev);
		JButton buttonClick=new JButton("BUTTONCLICK");
		buttonClick.addActionListener(listener);
		buttonClick.setActionCommand("button");
		footerPanel.add(buttonClick);
		
		return footerPanel;
	}
	
	public static JTable createTable(ListSelectionListener Listener1,TableModelListener listener2, Repository repos){
		
		TableView view=new TableView(repos.getParticipants(), repos.getParticipantsAttributes());
		String[] headers=repos.getParticipantsAttributes();
		JTable table=view.renderTable();
		table.getModel().addTableModelListener(listener2);
		ListSelectionModel listSelectionModel=table.getSelectionModel();
		listSelectionModel.addListSelectionListener(Listener1);
		table.setFillsViewportHeight(true);
		table.setPreferredScrollableViewportSize(new Dimension(750,250));
		return table;
	}

}
