import java.awt.Dimension;
import java.awt.CardLayout;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;


public class mainStageGUI {
	public static void main(String[] args) {
		MyVideos_GUI frame = new MyVideos_GUI();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}

class MyVideos_GUI extends JFrame {
	private JPanel				cards,		// Panel general en donde se agregaran los otros
								mainPanel;	// Panel donde iran los paneles con las Grid de peliculas
	private InformationPanel	infoPanel;	// Panel de informacion
	private ArrayList<GridPanel>gridPanes;	// Arraylist con Paneles de grid de peliculas, para facilitar acceso a estos

	public MyVideos_GUI() {
		setTitle("My Movies");
		setPreferredSize(new Dimension(800,500));
		DBManager	DBman 		= new DBManager();
		MyFiles 	videoFiles 	= new MyFiles(DBman,this);
		JScrollPane scroller;	// Panel con scroll donde va el mainPanel
		//################################
		//##########   MENU   ############
		//################################
			// Creacion de menus listeners
			MenuFilesListener mfl 	= new MenuFilesListener(videoFiles, DBman);
			MenuViewListener mvl 	= new MenuViewListener(videoFiles);
			MenuSortingListener msl = new MenuSortingListener(videoFiles);
			// Creacion de Menu
			setJMenuBar(createMenuBar(mfl, mvl, msl));
			videoFiles.addMenuBar(getJMenuBar());
			//
			mfl.addGUI(this);
			msl.addGUI(this);
		//################################
		//#########   PANELES  ###########
		//################################
			gridPanes = new ArrayList<GridPanel>();
			mainPanel = new JPanel();
			mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
			scroller = new JScrollPane(mainPanel);
			scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
			infoPanel = new InformationPanel();
			cards = new JPanel(new CardLayout());
			cards.add(scroller, "mainWindow");
			cards.add(infoPanel, "Panel informacion");
			infoPanel.addCards(cards);
			add(cards);		
	}
	private JMenuBar createMenuBar(MenuFilesListener mFiles, MenuViewListener mView, MenuSortingListener mSort) {
		JMenuBar menuBar;
		JMenu menuFiles, menuView, menuSorting;
		JMenuItem menuItem;
		JRadioButtonMenuItem rbMenuItem;
		menuBar = new JMenuBar();				// Creacion de la barra de menu
		menuFiles = new JMenu("Files");		// Creacion de menu de archivos
		menuView = new JMenu("View");		// Creacion de menu de vista
		menuSorting = new JMenu("Sorting");	// Creacion de menu de sorting
		menuBar.add(menuFiles);
		menuBar.add(menuView);
		System.out.println("menuView"+menuBar.getComponentZOrder(menuView));
		menuBar.add(menuSorting);
	//#################################
		// Incorporacion de elementos al menu de archivos
		menuItem = new JMenuItem("Add Movies Folder");
		menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
		menuItem.addActionListener(mFiles);
		menuFiles.add(menuItem);

		menuItem = new JMenuItem("Add Movie File");
		menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
		menuItem.addActionListener(mFiles);
		menuFiles.add(menuItem);

		menuFiles.addSeparator();
		menuItem = new JMenuItem("Close");
		menuItem.addActionListener(mFiles);
		menuFiles.add(menuItem);
	//#################################
		// Incorporacion de elementos al menu de vista
		// Archivos a mostrar : Movies | Series | Animes | etc...
		ButtonGroup viewing = new ButtonGroup();

		rbMenuItem = new JRadioButtonMenuItem("All");
		rbMenuItem.setSelected(true);
		viewing.add(rbMenuItem);
		rbMenuItem.addActionListener(mView);
		menuView.add(rbMenuItem);

		rbMenuItem = new JRadioButtonMenuItem("Movies");
		viewing.add(rbMenuItem);
		rbMenuItem.addActionListener(mView);
		menuView.add(rbMenuItem);

		menuView.addSeparator();
	//#################################
		// Incorporacion de elementos al menu de sorting
		ButtonGroup sorting = new ButtonGroup();

		rbMenuItem = new JRadioButtonMenuItem("All");
		rbMenuItem.setSelected(true);
		sorting.add(rbMenuItem);
		rbMenuItem.addActionListener(mSort);
		menuSorting.add(rbMenuItem);

		rbMenuItem = new JRadioButtonMenuItem("Alphabetical");
		sorting.add(rbMenuItem);
		rbMenuItem.addActionListener(mSort);
		menuSorting.add(rbMenuItem);

		rbMenuItem = new JRadioButtonMenuItem("Genres");
		sorting.add(rbMenuItem);
		rbMenuItem.addActionListener(mSort);
		menuSorting.add(rbMenuItem);

		rbMenuItem = new JRadioButtonMenuItem("Year");
		sorting.add(rbMenuItem);
		rbMenuItem.addActionListener(mSort);
		menuSorting.add(rbMenuItem);

		rbMenuItem = new JRadioButtonMenuItem("Director");
		sorting.add(rbMenuItem);
		rbMenuItem.addActionListener(mSort);
		menuSorting.add(rbMenuItem);

		return menuBar;
	}
	public void addGridPanel(String s, ArrayList<Movie> movies) {
		GridPanel gp = new GridPanel(s);
		gridPanes.add(gp);		// Agrega grid al ArrayList
		mainPanel.add(gp);		// Agrega grid al Panel
		gp.addCards(cards, infoPanel);
		gp.addItems(movies);				// Agrega items a la grid
	}
	public void cleanGridPanels() {
		for (GridPanel gp:gridPanes)
			mainPanel.remove(gp);
		gridPanes.clear();
	}
	public JPanel getCards() {
		return cards;
	}
	public InformationPanel getInfoPanel() {
		return infoPanel;
	}
	public JPanel getMainPanel() {
		return mainPanel;
	}
	public ArrayList<GridPanel> getGridPanes() {
		return gridPanes;
	}

}