package visual.panel;

import javax.swing.*;
import org.jdesktop.swingx.*;

import control.ControlPrincipal;
import utiles.Validador;
import visual.otros.JTableTable;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Panel que contiene la tabla con los datos de la cartola del usuario, aparte
 * de componentes utilizados para añadir datos a la tabla
 */

public class PanelCartola extends JPanel {

	private JButton bAñadir;
	private JButton bEliminar;
	private JButton bModificar;
	private JComboBox<String> boxDocumento1;
	private JComboBox<String> boxDocumento2;
	private JXDatePicker dateFecha1;
	private JXDatePicker dateFecha2;
	private JTextField fDesc1;
	private JTextField fDesc2;
	private JTextField fEgr1;
	private JTextField fEgr2;
	private JTextField fFD1;
	private JTextField fFD2;
	private JTextField fIng1;
	private JTextField fIng2;
	private JTextField fNumero;
	private JScrollPane jScrollPane1;
	private JLabel lDesc1;
	private JLabel lDesc2;
	private JLabel lDocumento1;
	private JLabel lDocumento2;
	private JLabel lEgr1;
	private JLabel lEgr2;
	private JLabel lFD1;
	private JLabel lFD2;
	private JLabel lFecha1;
	private JLabel lFecha2;
	private JLabel lIng1;
	private JLabel lIng2;
	private JLabel lNumero;
	private JTableTable tabla;
	private String usuario;

	/**
	 * Inicia el panel, asignandose un usuario al panel para ser utilizado mas
	 * tarde para obtener los datos del archivo correspondiente
	 * 
	 * @param usuario
	 *            Usuario del cual se obtienen los datos.
	 */
	public PanelCartola(String usuario) {
		this.usuario = usuario;
		initComponents();
	}

	/**
	 * Inicia los componentes del panel.
	 */

	private void initComponents() {

		lFecha1 = new JLabel();
		lDocumento1 = new JLabel();
		boxDocumento1 = new JComboBox<>();
		lFD1 = new JLabel();
		fFD1 = new JTextField();
		fDesc1 = new JTextField();
		lDesc1 = new JLabel();
		lIng1 = new JLabel();
		fIng1 = new JTextField();
		fEgr1 = new JTextField();
		lEgr1 = new JLabel();
		jScrollPane1 = new JScrollPane();
		tabla = new JTableTable(usuario);
		bAñadir = new JButton();
		bEliminar = new JButton();
		lFD2 = new JLabel();
		fFD2 = new JTextField();
		bModificar = new JButton();
		fDesc2 = new JTextField();
		lDesc2 = new JLabel();
		lIng2 = new JLabel();
		fIng2 = new JTextField();
		fEgr2 = new JTextField();
		lFecha2 = new JLabel();
		lEgr2 = new JLabel();
		lDocumento2 = new JLabel();
		boxDocumento2 = new JComboBox<>();
		fNumero = new JTextField();
		lNumero = new JLabel();
		dateFecha1 = new JXDatePicker();
		dateFecha2 = new JXDatePicker();

		setBackground(new Color(0, 128, 128));

		lFecha1.setBackground(new Color(0, 204, 204));
		lFecha1.setFont(new Font("Candara", 1, 14)); // NOI18N
		lFecha1.setForeground(new Color(255, 255, 255));
		lFecha1.setText("Fecha:");

		lDocumento1.setBackground(new Color(0, 204, 204));
		lDocumento1.setFont(new Font("Candara", 1, 14)); // NOI18N
		lDocumento1.setForeground(new Color(255, 255, 255));
		lDocumento1.setText("Documento:");

		boxDocumento1.setForeground(new Color(21, 21, 21));
		boxDocumento1.setModel(new DefaultComboBoxModel<>(new String[] { "Credito", "Efectivo", "Transferencia" }));

		lFD1.setBackground(new Color(0, 204, 204));
		lFD1.setFont(new Font("Candara", 1, 14)); // NOI18N
		lFD1.setForeground(new Color(255, 255, 255));
		lFD1.setText("Fuente/Destino:");

		fFD1.setToolTipText("");

		lDesc1.setBackground(new Color(0, 204, 204));
		lDesc1.setFont(new Font("Candara", 1, 14)); // NOI18N
		lDesc1.setForeground(new Color(255, 255, 255));
		lDesc1.setText("Descripcion:");

		lIng1.setBackground(new Color(0, 204, 204));
		lIng1.setFont(new Font("Candara", 1, 14)); // NOI18N
		lIng1.setForeground(new Color(255, 255, 255));
		lIng1.setText("Ingreso:");

		lEgr1.setBackground(new Color(0, 204, 204));
		lEgr1.setFont(new Font("Candara", 1, 14)); // NOI18N
		lEgr1.setForeground(new Color(255, 255, 255));
		lEgr1.setText("Egreso:");

		jScrollPane1.setBackground(new Color(128, 203, 196));

		jScrollPane1.setViewportView(tabla);
		if (tabla.getColumnModel().getColumnCount() > 0) {
			tabla.getColumnModel().getColumn(4).setPreferredWidth(150);
		}

		bAñadir.setBackground(new Color(0, 188, 212));
		bAñadir.setText("Añadir");
		bAñadir.setContentAreaFilled(false);
		bAñadir.setOpaque(true);
		bAñadir.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {

				añadirActionPerformed(evt);

			}

		});

		bEliminar.setBackground(new Color(0, 188, 212));
		bEliminar.setText("Eliminar Fila Seleccionada");
		bEliminar.setContentAreaFilled(false);
		bEliminar.setOpaque(true);
		bEliminar.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {

				eliminarActionPerformed(evt);

			}

		});

		lFD2.setBackground(new Color(0, 204, 204));
		lFD2.setFont(new Font("Candara", 1, 14)); // NOI18N
		lFD2.setForeground(new Color(255, 255, 255));
		lFD2.setText("Fuente/Destino:");

		fFD2.setToolTipText("");

		bModificar.setBackground(new Color(0, 188, 212));
		bModificar.setText("Modificar Fila");
		bModificar.setContentAreaFilled(false);
		bModificar.setOpaque(true);
		bModificar.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {

				modificarActionPerformed(evt);

			}

		});

		Calendar today = Calendar.getInstance();

		dateFecha1.setDate(today.getTime());
		dateFecha2.setDate(today.getTime());

		lDesc2.setBackground(new Color(0, 204, 204));
		lDesc2.setFont(new Font("Candara", 1, 14)); // NOI18N
		lDesc2.setForeground(new Color(255, 255, 255));
		lDesc2.setText("Descripcion:");

		lIng2.setBackground(new Color(0, 204, 204));
		lIng2.setFont(new Font("Candara", 1, 14)); // NOI18N
		lIng2.setForeground(new Color(255, 255, 255));
		lIng2.setText("Ingreso:");

		lFecha2.setBackground(new Color(0, 204, 204));
		lFecha2.setFont(new Font("Candara", 1, 14)); // NOI18N
		lFecha2.setForeground(new Color(255, 255, 255));
		lFecha2.setText("Fecha:");

		lEgr2.setBackground(new Color(0, 204, 204));
		lEgr2.setFont(new Font("Candara", 1, 14)); // NOI18N
		lEgr2.setForeground(new Color(255, 255, 255));
		lEgr2.setText("Egreso:");

		lDocumento2.setBackground(new Color(0, 204, 204));
		lDocumento2.setFont(new Font("Candara", 1, 14)); // NOI18N
		lDocumento2.setForeground(new Color(255, 255, 255));
		lDocumento2.setText("Documento:");

		boxDocumento2.setForeground(new Color(21, 21, 21));
		boxDocumento2.setModel(new DefaultComboBoxModel<>(new String[] { "Credito", "Efectivo", "Transferencia" }));

		fNumero.setToolTipText("");

		lNumero.setBackground(new Color(0, 204, 204));
		lNumero.setFont(new Font("Candara", 1, 14)); // NOI18N
		lNumero.setForeground(new Color(255, 255, 255));
		lNumero.setText("Nº");

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup().addContainerGap()
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
										.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 888, Short.MAX_VALUE)
										.addGroup(
												layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
														.addComponent(bEliminar)))
								.addGap(18, 18, 18)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout
										.createSequentialGroup()
										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
												.addComponent(lFD2, GroupLayout.Alignment.TRAILING)
												.addComponent(lEgr2, GroupLayout.Alignment.TRAILING)
												.addComponent(lIng2, GroupLayout.Alignment.TRAILING)
												.addComponent(lDesc2, GroupLayout.Alignment.TRAILING)
												.addComponent(lDocumento2,
														GroupLayout.Alignment.TRAILING)
												.addComponent(lFecha2,
														GroupLayout.Alignment.TRAILING))
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
												.addGroup(layout
														.createParallelGroup(GroupLayout.Alignment.LEADING, false)
														.addComponent(boxDocumento2, 0, GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(fFD2).addComponent(fDesc2).addComponent(fIng2)
														.addComponent(fEgr2, GroupLayout.PREFERRED_SIZE, 170,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(bModificar))
												.addComponent(dateFecha2, GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
										.addGroup(GroupLayout.Alignment.TRAILING,
												layout.createSequentialGroup().addComponent(lNumero)
														.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
														.addComponent(fNumero, GroupLayout.PREFERRED_SIZE, 170,
																GroupLayout.PREFERRED_SIZE))
										.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
												.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
														.addComponent(lFD1, GroupLayout.Alignment.TRAILING)
														.addComponent(lEgr1, GroupLayout.Alignment.TRAILING)
														.addComponent(lIng1, GroupLayout.Alignment.TRAILING)
														.addComponent(lDesc1, GroupLayout.Alignment.TRAILING)
														.addComponent(lDocumento1, GroupLayout.Alignment.TRAILING)
														.addComponent(lFecha1, GroupLayout.Alignment.TRAILING))
												.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
												.addGroup(layout
														.createParallelGroup(GroupLayout.Alignment.LEADING, false)
														.addComponent(boxDocumento1, 0, GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(fFD1).addComponent(fDesc1).addComponent(fIng1)
														.addComponent(fEgr1, GroupLayout.DEFAULT_SIZE, 170,
																Short.MAX_VALUE)
														.addComponent(bAñadir, GroupLayout.PREFERRED_SIZE, 96,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(dateFecha1, GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
								.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout
				.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
						.addGroup(
								layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,
										GroupLayout.PREFERRED_SIZE, 470, GroupLayout.PREFERRED_SIZE))
						.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup().addGap(38, 38, 38)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
										.addComponent(lFecha1).addComponent(dateFecha1, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(lDocumento1).addComponent(boxDocumento1,
												GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lFD1)
										.addComponent(fFD1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(lDesc1).addComponent(fDesc1, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lIng1)
										.addComponent(fIng1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(fEgr1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(lEgr1))
								.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(bAñadir)
								.addGap(18, 18, 18)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(lNumero).addComponent(fNumero, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(lFecha2).addComponent(dateFecha2, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(lDocumento2).addComponent(boxDocumento2,
												GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lFD2)
										.addComponent(fFD2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(lDesc2).addComponent(fDesc2, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lIng2)
										.addComponent(fIng2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(fEgr2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(lEgr2))
								.addGap(11, 11, 11).addComponent(bModificar)))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(bEliminar)
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

	}

	/**
	 * Getter que entrega la tabla de la cartola
	 * 
	 * @return JTableTable
	 */

	public JTableTable getTabla() {

		return tabla;

	}

	/**
	 * Accion realizada al presionar el boton añadir.
	 * <p>
	 * Toma los datos ingresados en los fields correspondientes y los añade a el
	 * archivo del usuario a travez de la clase ControlPrincipal
	 * </p>
	 */

	private void añadirActionPerformed(ActionEvent evt) {

		ControlPrincipal control = new ControlPrincipal(usuario);
		SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yyyy");
		String fecha = formateador.format(dateFecha1.getDate());
		String documento = this.boxDocumento1.getSelectedItem().toString();

		String fD = fFD1.getText();
		fFD1.setText("");

		String desc = fDesc1.getText();
		fDesc1.setText("");

		String ing = fIng1.getText();
		if (ing.equals("")) {
			ing = "0";
		} else if (ing.matches("^0")) {

			ing.replaceFirst("0", "");

		} else if (!Validador.isNumber(ing)) {
			JOptionPane.showMessageDialog(null, "Ingreso ingresado incorrecto", "Error", JOptionPane.ERROR_MESSAGE);
		}
		fIng1.setText("0");

		String egr = fEgr1.getText();
		if (egr.equals("")) {
			egr = "0";
		} else if (ing.startsWith("0")) {

			ing.replaceFirst("0", "");

		} else if (!Validador.isNumber(egr)) {
			JOptionPane.showMessageDialog(null, "Egreso ingresado incorrecto", "Error", JOptionPane.ERROR_MESSAGE);
		}
		fEgr1.setText("0");

		control.añadir(fecha, documento, fD, desc, ing, egr);

		tabla.actTabla();

	}

	/**
	 * Elimina la fila seleccionada en la tabla al ser presionada el boton
	 * Eliminar
	 */

	private void eliminarActionPerformed(ActionEvent evt) {

		ControlPrincipal control = new ControlPrincipal(usuario);

		if (tabla.getSelectedRow() != -1) {

			control.eliminarFila((String) tabla.getValueAt(tabla.getSelectedRow(), 0));

			tabla.actTabla();

		}

	}

	/**
	 * Modifica la linea ingresada en el jtextfield Nº. Esto atravez de los
	 * metodos en ControlPrincipal
	 */

	private void modificarActionPerformed(ActionEvent evt) {

		if (!fNumero.getText().equals("")) {

			String number = fNumero.getText();

			SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yyyy");
			String fecha = formateador.format(dateFecha2.getDate());

			String documento = boxDocumento2.getSelectedItem().toString();
			String fD = fFD2.getText();
			String desc = fDesc2.getText();
			String ing = fIng2.getText();
			if (ing.equals("")) {

				ing = "0";

			} else if (ing.startsWith("0")) {

				ing.replaceFirst("0", "");

			} else if (!Validador.isNumber(ing)) {

				JOptionPane.showMessageDialog(null, "Dato ingresado incorrecto", "Error", JOptionPane.ERROR_MESSAGE);

			}
			String egr = fEgr2.getText();
			if (egr.equals("")) {

				egr = "0";

			} else if (ing.startsWith("0")) {

				ing.replaceFirst("0", "");

			} else if (!Validador.isNumber(egr)) {

				JOptionPane.showMessageDialog(null, "Dato ingresado incorrecto", "Error", JOptionPane.ERROR_MESSAGE);

			}

			ControlPrincipal control = new ControlPrincipal(usuario);

			control.modificarFila(number, fecha, documento, fD, desc, ing, egr);

			tabla.actTabla();

		} else {

			JOptionPane.showMessageDialog(null, "No se ingreso el Nº");

		}

	}
}
