package View;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import Controller.Controller;

public class GUI extends View {
	
	private static JFrame frame;
	private static Controller cont;
	private static List<DataPanel> dataPanels=new ArrayList<DataPanel>();
	private static NavigationPanel nav;
	private static SidePanel side;
	
	
	/**
	 * Constructor of the GUI which will display the GUI right away
	 * 
	 * @param controller: the Controller from which data is taken.
	 */
	public GUI(Controller controller){
		cont=controller;
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
	        public void run() {
	            createAndShowGUI();
	        }
		});
	}
	
	/**
	 * Method to create the Frame of the GUI (is more like a constructor, as it just gets called by it)
	 */
	
	private void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("Learning-Meanings");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Learning-Meanings");
        
        // create a layout manager
        frame.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        // add the menu bar
        MenuBar menu=new MenuBar(cont);
        frame.setJMenuBar(menu);
        
        //set Restrictions for the TabPane
        c.weightx = 0.8;
        c.weighty = 0.95;
        c.fill = GridBagConstraints.BOTH;
    	c.gridx = 0;
    	c.gridy = 0;
    	
        //create the data panels
        DataPanel parameters= new ParameterPanel(cont);
        dataPanels.add(parameters);
        DataPanel scenes= new ScenePanel(cont);
        dataPanels.add(scenes);
        DataPanel leWoOvTi=new WordsLearnedOverTime(cont);
        dataPanels.add(leWoOvTi);
        DataPanel woCoSc=new WordComprehensionScore(cont);
        dataPanels.add(woCoSc);
        DataPanel woTa=new WordTablePanel(cont);
        dataPanels.add(woTa);
        
        //create the TabPane and add the datapanels to it
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("Parameters",parameters);
        tabbedPane.add("Scenes",scenes);
        tabbedPane.add("Words learned over time",leWoOvTi);
        tabbedPane.add("Word comprehension score",woCoSc);
        tabbedPane.add("Word table",woTa);
        
        //add TabPane to frame
        frame.add(tabbedPane,c);
        
        //create and add the navigation panel
        nav= new NavigationPanel(cont);
        c.gridy = GridBagConstraints.RELATIVE;
        c.weighty = 0.05;
        frame.add(nav,c);
        
        //create and add the side panel
        side= new SidePanel(cont);
        c.gridx = 1;
        c.gridy = 0;
        c.weighty =1.0;
        c.weightx = 0.2;
        c.gridheight=2;
        frame.add(side,c);
        
        //Display the frame
        frame.pack();
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setVisible(true);
        frame.repaint();
    }
	
	
	/**
	 * This method is called by the Buttons in the Navigation Panel, 
	 * to load the new data into the Panels, after they set the 
	 * iterationcounter in the Controller to the new Iteration
	 */
	
	public static void updatePanels(){
		for(int i=0;i< dataPanels.size();i++){
			dataPanels.get(i).updatePanel();
		}
		side.updatePanel();
		nav.updatePanel();
		frame.repaint();
	}
	
}
