package interfaz;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.SystemColor;

import javax.swing.JList;
import java.awt.Insets;
import javax.swing.JScrollPane;

import negocio.Plantilla;

import org.hibernate.Session;
import org.hibernate.Transaction;

import persistencia.UtilidadHibernate;

import java.awt.Color;
import java.util.Iterator;
import java.util.List;

public class SeleccionarMarco extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JButton jButton = null;

	private JLabel jLabel = null;

	private JPanel jPanel2 = null;

	private JScrollPane jScrollPane = null;

	private JList jList = null;

	private JPanel jPanel3 = null;

	private JLabel jLabel1 = null;

	private JPanel jPanel4 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;
	
	private Principal padrePrincipal;
	
	public DefaultListModel modelo = new DefaultListModel();
	
	boolean vieneDeLogin;

	/**
	 * @param owner
	 */
	public SeleccionarMarco(Principal owner, boolean v) {
		super(owner);
		padrePrincipal = owner;
		vieneDeLogin = v;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		//this.setSize(550, 600);
		padrePrincipal.Tamaño_ventana(this);
		this.setModal(true);
		if (padrePrincipal.idioma.equals("español"))
			this.setTitle("Selecciona un marco de trabajo");
		else
			this.setTitle("Select a workspace");
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() {   
			public void windowClosing(java.awt.event.WindowEvent e) {    
				if (vieneDeLogin)
					padrePrincipal.usuario=null;
			}   
			public void windowOpened(java.awt.event.WindowEvent e) {
				ActualizarPlantillas();
				EstadisticasNumeroPlantillas();
				try{
					if (modelo.getSize()==0){
						if (padrePrincipal.idioma.equals("español"))
							throw new Exception("Error, no existen plantillas de proyecto definidas. Contacte con el desarrollador.");
						else
							throw new Exception("Error, there aren't defined project templates. Contact developer.");
					}
				}
				catch (Exception exp) {
					// TODO: handle exception
					JOptionPane.showMessageDialog(null, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}

	void ActualizarPlantillas(){
		
		try{
			Session session=null;
			
			session = UtilidadHibernate.getSessionFactory().openSession();
			
			Transaction tx = session.beginTransaction();
			int idelemento = padrePrincipal.jList.getSelectedIndex();

			modelo.removeAllElements();
			List messages = session.createQuery("from Plantilla").list();
			//System.out.println("Número de elementos: " + messages.size());
			for ( Iterator iter = messages.iterator(); iter.hasNext(); ) {
				Plantilla plantilla = (Plantilla) iter.next();
				modelo.addElement(plantilla);
			}
			jList.setModel(modelo);
			tx.commit();
			session.close();
		}	
		catch (Exception exp) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(null, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void EstadisticasNumeroPlantillas()
	{
		try{
			Session session=null;
			
			session = UtilidadHibernate.getSessionFactory().openSession();
			
			//Número de administradores
			List messages = session.createQuery("from Plantilla order by nombre").list();
			Integer numero_plantillas= messages.size();
			
			jLabel3.setText(numero_plantillas.toString());
			
			session.close();
			
		}	
		catch (Exception exp) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(null, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.EAST);
			jContentPane.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new FlowLayout());
			jPanel.add(getJButton(), null);
			jPanel.setBackground(SystemColor.controlHighlight);
			jPanel.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.lightGray));
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 3;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 2;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.insets = new Insets(5, 5, 0, 5);
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 4;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(15, 5, 5, 5);
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			if (padrePrincipal.idioma.equals("español"))
				jLabel.setText("Lista de marcos de trabajo:");
			else
				jLabel.setText("List of frameworks:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(jLabel, gridBagConstraints);
			jPanel1.add(getJPanel2(), gridBagConstraints2);
			jPanel1.add(getJScrollPane(), gridBagConstraints1);
			jPanel1.add(getJPanel3(), gridBagConstraints4);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			if (padrePrincipal.idioma.equals("español"))
				jButton.setText("Aceptar");
			else
				jButton.setText("Accept");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					try{
						int idplantilla = jList.getSelectedIndex();
						if (idplantilla>-1){
							Plantilla plantilla = (Plantilla) modelo.getElementAt(idplantilla);
							
							padrePrincipal.marco_seleccionado = (Plantilla) plantilla;
							SeleccionarMarco.this.dispose();
							padrePrincipal.dispose();
							Principal thisClass = new Principal(true, padrePrincipal.usuario, plantilla);
							thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
							if (thisClass.usuario.getRol().equals("Implanter")){
								thisClass.jLabel3.setVisible(true);
								thisClass.jTextField1.setVisible(true);
								thisClass.jTextField1.setText(thisClass.marco_seleccionado.getNombre());
							}
							//thisClass.usuario=padrePrincipal.usuario;
							thisClass.jMenuItem9.setEnabled(false);
							thisClass.setVisible(true);
						}
						else if (modelo.getSize()==0){
							if (padrePrincipal.idioma.equals("español"))
								throw new Exception("Error, no existen plantillas de proyecto definidas. Contacte con el desarrollador.");
							else
								throw new Exception("Error, there aren't defined project templates. Contact developer.");
						}
						else{
							if (padrePrincipal.idioma.equals("español"))
								throw new Exception("Error, selecciona una plantilla de la lista.");
							else
								throw new Exception("Error, select a template of the list.");
						}
					}
					catch (Exception exp) {
						// TODO: handle exception
						JOptionPane.showMessageDialog(null, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
		}
		return jPanel2;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJList() {
		if (jList == null) {
			jList = new JList();
		}
		return jList;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = -1;
			gridBagConstraints5.insets = new Insets(0, 0, 5, 0);
			gridBagConstraints5.gridy = -1;
			jLabel1 = new JLabel();
			if (padrePrincipal.idioma.equals("español"))
				jLabel1.setText("Estadísticas:");
			else
				jLabel1.setText("Stats:");
			jPanel3 = new JPanel();
			jPanel3.setLayout(new GridBagLayout());
			jPanel3.setPreferredSize(new Dimension(38, 50));
			jPanel3.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.lightGray));
			jPanel3.add(jLabel1, gridBagConstraints5);
			jPanel3.add(getJPanel4(), gridBagConstraints6);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.gridy = 0;
			jLabel3 = new JLabel();
			jLabel3.setForeground(Color.BLUE);
			jLabel3.setText("");
			jLabel2 = new JLabel();
			jLabel2.setText("Total: ");
			jLabel2.setForeground(Color.blue);
			jPanel4 = new JPanel();
			jPanel4.setLayout(new GridBagLayout());
			jPanel4.add(jLabel2, new GridBagConstraints());
			jPanel4.add(jLabel3, gridBagConstraints7);
		}
		return jPanel4;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
