/**
 * @author Aurélie
 *
 */

package View;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.SQLException;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import Controler.IO.DB;
import Model.IAcquisition;
import Model.IPoint;
import Model.IUser;
import Model.Model;

public class OngletApprentissage extends JPanel {

	private static final long serialVersionUID = 1L;

	public static final int numberOfAcquisitions = 5;

	private JRadioButton jr1;
	private JRadioButton jr2;
	private JLabel jl1 = new JLabel("Nom : ");
	private JLabel jl2 = new JLabel("Prénom : ");
	private JTextField jt1 = new JTextField();
	private JTextField jt2 = new JTextField();
	private ButtonGroup bg = new ButtonGroup();
	private JButton jb1;
	private JButton jb2;

	private JPanel container;
	private JPanel gtop;
	private JPanel gtoptop;
	private JPanel gtopmid;
	private JPanel gtopbot;
	private JPanel gmid;
	private JPanel gbot;

	private Model model;
	private DrawingFrame df;

	public OngletApprentissage(Model model) {
		
		this.model = model;
		
		container = new JPanel();
		gtop = new JPanel();
		gtop.setLayout(new BorderLayout());
		gtop.setPreferredSize(new Dimension(600,150));
		gtoptop = new JPanel();
		gtopmid = new JPanel();
		gtopbot = new JPanel();
		gmid = new JPanel();
		gmid.setPreferredSize(new Dimension(600,60));
		gbot = new JPanel();		
		gbot.setPreferredSize(new Dimension(600,390));

		
		container.setLayout(new BorderLayout());
		
		
		JLabel titreOnglet1 = new JLabel("Nouvel utilisateur : ");
		jr1 = new JRadioButton("Tablette");
		jr2 = new JRadioButton("DS");
		
		jr1.setSelected(true);

		jb1 = new JButton("Acquisition des signatures");
		jb2 = new JButton("Enregistrer");
		jb1.setEnabled(true);
		jb2.setEnabled(false);
		
		jt1.setPreferredSize(new Dimension(150, 25));
        jt2.setPreferredSize(new Dimension(150, 25));
        
		gtoptop.add(titreOnglet1);

		gtopmid.add(jl2);
		gtopmid.add(jt2);

		gtopmid.add(jl1);
		gtopmid.add(jt1);
		
		bg.add(jr1);
		bg.add(jr2);

		gtopbot.add(jr1);
		gtopbot.add(jr2);

		gmid.add(jb1);

		gbot.add(jb2);

		gtop.add(gtoptop, BorderLayout.NORTH);
		gtop.add(gtopmid, BorderLayout.CENTER);
		gtop.add(gtopbot, BorderLayout.SOUTH);

		container.add(gtop, BorderLayout.NORTH);
		container.add(gmid, BorderLayout.CENTER);
		container.add(gbot,BorderLayout.SOUTH);


		this.add(container);

		jb1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

				int source;

				if(jr2.isSelected()) {
					source = DrawingFrame.SOURCE_DS;
				} else {
					source = DrawingFrame.SOURCE_TABLET;
				}

				df = new DrawingFrame(OngletApprentissage.this, numberOfAcquisitions, source);

			}
		});

		jb2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addUser();
			}
		});

		jt1.addKeyListener(new KeyListener(){
			public void keyPressed(KeyEvent arg0) {}
			public void keyReleased(KeyEvent arg0) {
				checkIfItsPossibleToSave();				
			}
			public void keyTyped(KeyEvent arg0) {}
		});

		jt2.addKeyListener(new KeyListener(){
			public void keyPressed(KeyEvent arg0) {}
			public void keyReleased(KeyEvent arg0) {
				checkIfItsPossibleToSave();				
			}
			public void keyTyped(KeyEvent arg0) {}
		});

	}

	private void addUser(){
		if(!jt1.getText().trim().isEmpty() && !jt2.getText().trim().isEmpty() &&
				df != null && df.getAcquisitions().size() == numberOfAcquisitions) {
			try {
				if(DB.getInstance().findUser(jt2.getText(), jt1.getText()) != null) {
					JOptionPane.showMessageDialog(new JFrame(), "Cet utilisateur existe déjà", "Erreur",
							JOptionPane.ERROR_MESSAGE);
				} else {
					IUser u = model.addNewUser(jt2.getText(), jt1.getText());
					for(List<IPoint> listOfPoints : df.getAcquisitions()) {
						IAcquisition a = u.getUserSignature().addAcquisition(false);
						a.addPoints(listOfPoints);
					}
					df = null;
					jt1.setText("");
					jt2.setText("");
					jb1.setEnabled(true);
					checkIfItsPossibleToSave();				
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	private void checkIfItsPossibleToSave(){

		if(df != null && df.getAcquisitions().size() == numberOfAcquisitions 
				&& !jt1.getText().trim().isEmpty() && !jt2.getText().trim().isEmpty()) {
			jb2.setEnabled(true);
		} else {
			jb2.setEnabled(false);
		}
	}

	public void setAcquisitionsWorked() {
		jb1.setEnabled(false);
		checkIfItsPossibleToSave();
	}


}
