import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.*;

import java.io.*;


public class CreationPerso extends JDialog implements ActionListener{
	
	private ParcheminFond pfond=new ParcheminFond();
	
	
	
	
	
	
	private CryptRL leJeu; 
	private PanneauJeu jeuPan;
	
	private JLabel nomLbl=new JLabel("Nom / Name");
	
	private JTextField nomTxtF=new JTextField("JACK");
	
	private JButton okBtn=new JButton("OK");
	
	private JButton tileBtn=new JButton("Tile");
	
	private JFileChooser fc = new JFileChooser();
	
	private String urlPortrait=new String();
	
	private PortraitJPanel face=new PortraitJPanel();
	
	
	private JLabel foLbl=new JLabel("Force / Strength");
	private JTextField foTxtF=new JTextField("5");
	
	private JLabel dxLbl=new JLabel("Dexterite / Dexterity");
	private JTextField dxTxtF=new JTextField("5");
	
	private JLabel coLbl=new JLabel("Constitution");
	private JTextField coTxtF=new JTextField("5");
	
	private JLabel intLbl=new JLabel("Intelligence");
	private JTextField intTxtF=new JTextField("5");
	
	private JLabel infoLbl=new JLabel("Repartissez 20 points dans les 4 caracteristiques");
	private JLabel infoLblE=new JLabel("Spend 20 points in stats");
	
	private JLabel warningLbl=new JLabel("");
	
	
	
	private JLabel infoLblSk=new JLabel("Repartissez 30 niveaux dans les skill (max rang 5)");
	private JLabel infoLblSkE=new JLabel("Spend 30 ranks in skills(max rank 5)");
	
	private JLabel warningLblSk=new JLabel("");
	
	
	
	
	//SKILLS LABELS
	JLabel perception=new JLabel("Perception");
	JLabel discretion=new JLabel("Discretion / Stealth");
	JLabel melee=new JLabel("Melee");
	JLabel distance=new JLabel("Distance");
	JLabel magie=new JLabel("Magie / Magic");
	JLabel attunement=new JLabel("Attunement");
	JLabel crocheter=new JLabel("Crocheter / LockPicking");
	JLabel pieges=new JLabel("Pieges / Traps");
	JLabel identification=new JLabel("Identification");
	JLabel baratin=new JLabel("Baratin / FastTalk");
	//SKILLS LEVELS
	JTextField perceptionLvl=new JTextField("3");
	JTextField discretionLvl=new JTextField("3");
	JTextField meleeLvl=new JTextField("3");
	JTextField distanceLvl=new JTextField("3");
	JTextField magieLvl=new JTextField("3");
	JTextField attunementLvl=new JTextField("3");
	JTextField crocheterLvl=new JTextField("3");
	JTextField piegesLvl=new JTextField("3");
	JTextField identificationLvl=new JTextField("3");
	JTextField baratinLvl=new JTextField("3");	
	
	
	
	
	
	
	
	
	public CreationPerso(CryptRL game,PanneauJeu gamePan){ 
		Font police=gamePan.getPolice();
		nomLbl.setFont(police);
		okBtn.setFont(police);
		tileBtn.setFont(police);
		foLbl.setFont(police);
		dxLbl.setFont(police);
		coLbl.setFont(police);
		intLbl.setFont(police);
		infoLbl.setFont(police);
		infoLblE.setFont(police);
		warningLbl.setFont(police);
		infoLblSk.setFont(police);
		infoLblSkE.setFont(police);
		warningLblSk.setFont(police);
		perception.setFont(police);
		discretion.setFont(police);
		melee.setFont(police);
		distance.setFont(police);
		magie.setFont(police);
		attunement.setFont(police);
		crocheter.setFont(police);
		pieges.setFont(police);
		identification.setFont(police);
		baratin.setFont(police);
		
		
		
		leJeu=game;
		jeuPan=gamePan;
		
		
		Container c=getContentPane();
		c.setLayout(null);
		
		
		
		
		
		nomLbl.setBounds(10,10,100,20);
		nomTxtF.setBounds(110,10,150,20);
		tileBtn.setBounds(260,84,70,20);
		face.setBounds(270,10,64,64);
		
		foLbl.setBounds(10,70,150,20);
		foTxtF.setBounds(160,70,50,20);
		
		dxLbl.setBounds(10,95,150,20);
		dxTxtF.setBounds(160,95,50,20);
		
		coLbl.setBounds(10,120,150,20);
		coTxtF.setBounds(160,120,50,20);
		
		intLbl.setBounds(10,145,150,20);
		intTxtF.setBounds(160,145,50,20);
		
		warningLbl.setBounds(10,180,200,20);
		
		infoLbl.setBounds(10,200,300,20);
		infoLblE.setBounds(10,220,300,20);
		
		
		
		
		
		c.add(nomLbl);
		c.add(nomTxtF);
		c.add(face);
		c.add(tileBtn);
		c.add(okBtn);
		c.add(foLbl);
		c.add(foTxtF);
		c.add(dxLbl);
		c.add(dxTxtF);
		c.add(coLbl);
		c.add(coTxtF);
		c.add(intLbl);
		c.add(intTxtF);
		c.add(infoLbl);
		c.add(infoLblE);
		c.add(warningLbl);
		
		tileBtn.addActionListener(this);
		okBtn.addActionListener(this);
		
		
		
		////SKILLS
		
		perception.setBounds(5,270,100,20);
		add(perception);
		perceptionLvl.setBounds(160,270,50,20);
		add(perceptionLvl);
		
		
		discretion.setBounds(5,290,150,20);
		add(discretion);
		discretionLvl.setBounds(160,290,50,20);
		add(discretionLvl);
		
		
		melee.setBounds(5,310,100,20);
		add(melee);
		meleeLvl.setBounds(160,310,50,20);
		add(meleeLvl);
		
		
		distance.setBounds(5,330,100,20);
		add(distance);
		distanceLvl.setBounds(160,330,50,20);
		add(distanceLvl);
		
		magie.setBounds(5,350,100,20);
		add(magie);
		magieLvl.setBounds(160,350,50,20);
		add(magieLvl);
		
		attunement.setBounds(5,370,100,20);
		add(attunement);
		attunementLvl.setBounds(160,370,50,20);
		add(attunementLvl);
		
		
		crocheter.setBounds(5,390,150,20);
		add(crocheter);
		crocheterLvl.setBounds(160,390,50,20);
		add(crocheterLvl);
		
		
		pieges.setBounds(5,410,100,20);
		add(pieges);
		piegesLvl.setBounds(160,410,50,20);
		add(piegesLvl);
		
		
		identification.setBounds(5,430,100,20);
		add(identification);
		identificationLvl.setBounds(160,430,50,20);
		add(identificationLvl);
		
		
		baratin.setBounds(5,450,150,20);
		add(baratin);
		baratinLvl.setBounds(160,450,50,20);
		add(baratinLvl);
		
		
		warningLblSk.setBounds(10,470,200,20);
		add(warningLblSk);
		
		infoLblSk.setBounds(10,490,330,20);
		add(infoLblSk);
		
		infoLblSkE.setBounds(10,510,330,20);
		add(infoLblSkE);
		
		okBtn.setBounds(100,550,70,20);
		
		
		
		
		
		pfond.setBounds(0,0,350,650);
		add(pfond);
		
		
		setTitle("Creation");
		setSize(350,650);
		show();
		
		
		
		//new ThreadSon("./sons/evilwelcome.wav",0).start();
		
	}
	
	
	public void actionPerformed(ActionEvent e){
		
		Object source=e.getSource();
		
		if(source==okBtn){
			
			generationPerso(nomTxtF.getText());
			
		}
		
		
		if(source==tileBtn){//POUR CHANGER DE PORTRAIT
			
			int returnVal =fc.showOpenDialog(this);
			
			if (returnVal == JFileChooser.APPROVE_OPTION) {  
            	File file = fc.getSelectedFile();
            	
            	urlPortrait=file.toString();
            	System.out.println(file.toString());
            	face.updatePortrait(urlPortrait);
            	
            	
            }
		}
		
	}
	

	
	/////////////////////////////////////////////////////////////////////
	////////////////////GENERATION PERSO/////////////////////////////////
	/////////////////////////////////////////////////////////////////////
	
	public void generationPerso(String nomGen){
		
		leJeu.monde=new Monde(jeuPan,leJeu.cat);
		
		//une fonction de génération de personnage qui
		//determinera les stats en fonction des choix faits par l'utilisateur
		//c'est cette fonction qui créera le Personnage perso.
		
		
		//fo,dx,co,int
		if(urlPortrait.compareTo("")==0){urlPortrait="./images/pjs/alchemist.GIF";
		}
		
		int f=Integer.parseInt(foTxtF.getText());
		int d=Integer.parseInt(dxTxtF.getText());
		int c=Integer.parseInt(coTxtF.getText());
		int i=Integer.parseInt(intTxtF.getText());
		
		int s1=Integer.parseInt(perceptionLvl.getText());
		int s2=Integer.parseInt(discretionLvl.getText());
		int s3=Integer.parseInt(meleeLvl.getText());
		int s4=Integer.parseInt(distanceLvl.getText());
		int s5=Integer.parseInt(magieLvl.getText());
		int s6=Integer.parseInt(attunementLvl.getText());
		int s7=Integer.parseInt(crocheterLvl.getText());
		int s8=Integer.parseInt(piegesLvl.getText());
		int s9=Integer.parseInt(identificationLvl.getText());
		int s10=Integer.parseInt(baratinLvl.getText());
		
		if(f+d+c+i!=20){warningLbl.setText("Erreur sur le total");
		}
		
		if(s1+s2+s3+s4+s5+s6+s7+s8+s9+s10!=30){warningLblSk.setText("Erreur sur le total");
		}
		
		if(s1>5 || s2>5|| s3>5|| s4>5|| s5>5|| s6>5|| s7>5|| s8>5|| s9>5|| s10>5){
			warningLblSk.setText("Pas de rang supérieur a 5 !");
		}
		
		if(f+d+c+i==20 && s1+s2+s3+s4+s5+s6+s7+s8+s9+s10==30 && s1<=5&& s2<=5&& s3<=5&& s4<=5&& s5<=5&& s6<=5&& s7<=5&& s8<=5&& s9<=5&& s10<=5)
		{
			
			
			
			
			Personnage persoTemp=new Personnage(leJeu.monde,urlPortrait,nomGen,f,d,c,i,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10);
			
			
			//TEST 	pour le lancer d'objets					!!!!!!!
			//persoTemp.setDistanceLvl(10);
			
			
			///////////////////////////////////////////////////
			////////////////UPDATE/////////////////////////////
			///////////////////////////////////////////////////
			
			// CEST LE PERSO DE MONDE QUI IMPORTE !!!!!!!!!!!!!!!!!!!!!!!!
			leJeu.monde.perso=persoTemp;
			
			
			//Recepteur r = new Recepteur(leJeu,jeuPan);
			//jeuPan.addKeyListener(r);
			
			
			//On place les stats du perso dans resumestatsPanel
			//jeuPan.resumestatsPanel.nomTxt.setText(jeuPan.perso.nom);///!!!!!
			jeuPan.resumestatsPanel.nomTxt.setText(leJeu.monde.perso.getNom());///!!!!!
			//////////////////////////////////////////////////
			leJeu.monde.jeuPan=jeuPan;
			//////////////////////////////////////////////////
			
			
			
			
			
			leJeu.chargementPremierTerrainPERSO(nomGen);
			
			
			
			
			
			if(!leJeu.jeuInit){
				jeuPan.terrainPanel=new TerrainPanel(leJeu);
				jeuPan.terrainPanel.setBounds(172,10,640,480);
				jeuPan.add(jeuPan.terrainPanel);
				
				
				jeuPan.equipementpanel=new EquipementPanel(leJeu);
				jeuPan.equipementpanel.setBounds(817,10,195,450);
				jeuPan.add(jeuPan.equipementpanel);
				
				
			}
			
			
			
			leJeu.monde.miseAJourResumeStatsPanel();
			
			//Ancienne méthode
			//jeuPan.equipementpanel.updateEquipementList(leJeu.monde,leJeu.monde.perso.equipementVect);
			
			
			//jeuPan.terrainPanel.repaint();
			//jeuPan.repaint();
			
			
			
			leJeu.jeuInit=true;
			
			//leJeu.monde.ambiance();
			
			
			
			jeuPan.requestFocus();
			
			
			System.out.println(leJeu.monde.perso.getNom()+" est cree");//////////
			
			//leJeu.started=true;
			
			dispose();
		}
		
		
	}
	
	
	/////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////
	
}