package vue;
import java.awt.EventQueue;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import controleur.Controle;
import controleur.GLOBAL;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;

public class ChoixJoueur extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private Controle control;
	private JLabel lblPersonnage;
	private int persoNum = 1;
	private int nbPerso = 3;
	private JTextField txtPseudo;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public ChoixJoueur(Controle ctrl) {
		setTitle("Choice");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setPreferredSize(new Dimension(400, 275));
		this.pack();
		this.setResizable(false);
		setBounds(100, 100, 412, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		String chemin = GLOBAL.cheminFonds + "fondchoix.jpg";
		
		lblPersonnage = new JLabel("");
		lblPersonnage.setBounds(141, 98, 121, 122);
		lblPersonnage.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblPersonnage);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(persoNum+1 <= nbPerso) {
					persoNum++;
					affichePerso(persoNum);
				} else {
					persoNum = 1;
					affichePerso(persoNum);
				}
				System.out.println("Suivant");
			}
			@Override
			public void mouseExited(MouseEvent e) {
				sourisNormale();
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				sourisDoigt();
			}
		});
		lblNewLabel_1.setBackground(new Color(128, 255, 0));
		lblNewLabel_1.setBounds(293, 108, 93, 81);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_1_1 = new JLabel("");
		lblNewLabel_1_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(persoNum-1 >= 1) {
					persoNum--;
					affichePerso(persoNum);
				} else {
					persoNum = 3;
					affichePerso(persoNum);
				}
				System.out.println("Précédent");
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				sourisDoigt();
			}
			@Override
			public void mouseExited(MouseEvent e) {
				sourisNormale();
			}
		});
		lblNewLabel_1_1.setBackground(new Color(128, 255, 0));
		lblNewLabel_1_1.setBounds(10, 96, 93, 81);
		contentPane.add(lblNewLabel_1_1);
		
		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				if(txtPseudo.getText().length() == 0) {
					JOptionPane.showMessageDialog(null, "La saisie du pseudo est obligatoire !");
					txtPseudo.requestFocus();
				}else {
					ctrl.evenementChoixJoueur(txtPseudo.getText(), persoNum);
				}
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				sourisDoigt();
			}
			@Override
			public void mouseExited(MouseEvent e) {
				sourisNormale();
			}
		});
		lblNewLabel_2.setBounds(293, 200, 103, 61);
		contentPane.add(lblNewLabel_2);
		
		txtPseudo = new JTextField();
		txtPseudo.setColumns(10);
		txtPseudo.setBounds(141, 235, 121, 25);
		contentPane.add(txtPseudo);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(0, 0, 396, 261);
		contentPane.add(lblNewLabel);
		
		affichePerso(persoNum);
		
		URL ressourceURL = getClass().getClassLoader().getResource(chemin);

		if (ressourceURL != null) {
		    String ressource = URLDecoder.decode(ressourceURL.getPath(), StandardCharsets.UTF_8);
		    lblNewLabel.setIcon(new ImageIcon(ressource));
		} else {
		    System.out.println("❌ Image non trouvée !");
		}
		
		control = ctrl;
		
	}
	
	public void affichePerso(int numPerso) {
		String chemin = GLOBAL.cheminPersos + "perso"+ numPerso+ "marche1d1.gif";
		URL ressourceURL = getClass().getClassLoader().getResource(chemin);

		if (ressourceURL != null) {
		    String ressource = URLDecoder.decode(ressourceURL.getPath(), StandardCharsets.UTF_8);
		    lblPersonnage.setIcon(new ImageIcon(ressource));
		} else {
		    System.out.println("❌ Image non trouvée !");
		}
	}
	
	public void sourisNormale() {
		//
		contentPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}
	public void sourisDoigt() {
		//
		contentPane.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}
}
