package visual.frame;

import javax.swing.*;

import visual.panel.PanelCartola;
import visual.panel.PanelResumen;
import visual.panel.PanelSuperior;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Frame que contiene los Paneles cartola, resumen y superior
 */

public class FramePrincipal extends JFrame {

	private JPanel panelCard;
	private PanelCartola panelCartola;
	private PanelResumen panelResumen;
	private PanelSuperior panelSuperior;
	private JPanel separador;
	private String usuario;

	/**
	 * Inicia la ventana de usuario
	 * 
	 * @param usuario
	 *            Usuario el cual indicara a quien corresponde el archivo con
	 *            sus datos.
	 */

	public FramePrincipal(String usuario) {
		this.usuario = usuario;
		initComponents();

	}

	/**
	 * Inicia los componentes del frame
	 */

	private void initComponents() {

		panelSuperior = new PanelSuperior(usuario);
		panelCard = new JPanel();
		panelCartola = new PanelCartola(usuario);
		panelResumen = new PanelResumen(usuario);
		separador = new JPanel();

		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				salir();
			}
		});

		separador.setBackground(new Color(255, 255, 255));

		panelCard.setLayout(new CardLayout());
		panelCard.add(panelCartola, "cartola");
		panelCard.add(panelResumen, "resumen");

		javax.swing.GroupLayout separadorLayout = new javax.swing.GroupLayout(separador);
		separador.setLayout(separadorLayout);
		separadorLayout.setHorizontalGroup(separadorLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 1001, Short.MAX_VALUE));
		separadorLayout.setVerticalGroup(separadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 11, Short.MAX_VALUE));

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(panelSuperior, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
						Short.MAX_VALUE)
				.addComponent(panelCard, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
						Short.MAX_VALUE)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(separador, javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addComponent(panelSuperior, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(0, 0, 0)
						.addComponent(separador, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(0, 0, 0).addComponent(panelCard, javax.swing.GroupLayout.PREFERRED_SIZE, 524,
								javax.swing.GroupLayout.PREFERRED_SIZE)));

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		pack();
		setResizable(false);
		setVisible(true);
		setLocationRelativeTo(null);
	}

	/**
	 * Accion que se realiza al presionar el boton salir de la ventana
	 */

	protected void salir() {
		Object[] opciones = { "Si", "No" };
		if (JOptionPane.showOptionDialog(null, "żEsta seguro?", "Salir", JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.INFORMATION_MESSAGE, null, opciones, "Si") == 0) {
			System.exit(0);
		}

	}

	/**
	 * Muestra el panel Cartola
	 */

	public void toCartola() {

		panelCartola.getTabla().actTabla();
		CardLayout cardLayout = (CardLayout) panelCard.getLayout();
		cardLayout.show(panelCard, "cartola");
	}

	/**
	 * Muestra el panel Resumen
	 */

	public void toResumen() {
		panelResumen.llenarResumen();
		CardLayout cardLayout = (CardLayout) panelCard.getLayout();
		cardLayout.show(panelCard, "resumen");

	}
}
