/**
*Programme cree par The Clusters
*Classe Affichage du jeu "Chkoba"
* Classe qui cre la fenetre d'affichage
*=> la fenetre avec les skins
*@author The Clusters
*@version 1.0
**/

package chkoba;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
import chkoba.*;

/**
*	Classe Affichage
*/

public class Affichage extends JFrame implements ActionListener 
{
	private JPanel pDecorateur, pGeneral;
	private JButton ok, quitter;
	private JLabel lDecoN, lDecoS, lDecoO, lDecoE, lTemp, lTemp2;
	private JButton lSkin1, lSkin2, lSkin3, lSkin4, lSkin5, lSkin6, lSkin7, lSkin8, lSkin9, lSkin10;
	private JRadioButton bSkin1, bSkin2, bSkin3, bSkin4, bSkin5, bSkin6, bSkin7, bSkin8, bSkin9, bSkin10;
	private ButtonGroup bg;
	private ImageIcon iskin1 , iskin2 , iskin3 , iskin4 , iskin5 , iskin6 , iskin7 , iskin8 , iskin9 , iskin10;

	/**
	*Constructeur creant la fenetre
	*@param i l'index appelant
	*/
	public Affichage(Index i)
	{
		setTitle("Chkoba - Personalisation");
		setSize(800,600);
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Affichage.class.getResource("/icone.png")));	//permet de changer l'image dans le haut de la frame
		
		/* Creation des panels */
		
		pGeneral = new JPanel();
		pDecorateur = new JPanel();
		
		/* Changement d'etat des panels */
		
		pDecorateur.setLayout(new BorderLayout());
		pGeneral.setLayout(null);
		
		/* Ajout du Panel principal */
		
		add(pDecorateur);
		pDecorateur.add(pGeneral);
		
		/* On s'occupe de la deco */
		
		lDecoN = new JLabel(" ");
		lDecoS = new JLabel(" ");
		lDecoO = new JLabel("       ");
		lDecoE = new JLabel("       ");
		
		pDecorateur.add(lDecoN, "North");
		pDecorateur.add(lDecoS, "South");
		pDecorateur.add(lDecoO, "West");
		pDecorateur.add(lDecoE, "East");
		
		lDecoN.setSize(500,100);
		lDecoS.setSize(500,100);
		lDecoO.setSize(100,300);
		lDecoE.setSize(100,300);
		
		lDecoN.setBackground(Color.LIGHT_GRAY);
		lDecoN.setOpaque(true); 			// permet de rendre le label opaque et non plus transparent
		lDecoS.setBackground(Color.LIGHT_GRAY);
		lDecoS.setOpaque(true);
		lDecoO.setBackground(Color.LIGHT_GRAY);
		lDecoO.setOpaque(true);
		lDecoE.setBackground(Color.LIGHT_GRAY);
		lDecoE.setOpaque(true);
		
		/* On s'occupe de la premiere liste des skins */
		
		bSkin1 = new JRadioButton(); 
		bSkin1.setBounds(110,250, 30, 13);
		bSkin2 = new JRadioButton();
		bSkin2.setBounds(235,250, 30, 13);
		bSkin3 = new JRadioButton();
		bSkin3.setBounds(360,250, 30, 13);
		bSkin4 = new JRadioButton();
		bSkin4.setBounds(485,250, 30, 13);
		bSkin5 = new JRadioButton();
		bSkin5.setBounds(610,250, 30, 13);
		bg = new ButtonGroup();
		
		bSkin1.setSelected(true);
		
		iskin1 = new ImageIcon("./Skin/Skin1/Dos_carte3fini.png");
		iskin2 = new ImageIcon("./Skin/Skin2/Dos_carte3fini.png");
		iskin3 = new ImageIcon("./Skin/Skin3/Dos_carte3fini.png");
		iskin4 = new ImageIcon("./Skin/Skin4/Dos_carte3fini.png");
		iskin5 = new ImageIcon("carteAlastor.jpg");
		iskin6 = new ImageIcon("carteAlastor.jpg");
		iskin7 = new ImageIcon("carteAlastor.jpg");
		iskin8 = new ImageIcon("carteAlastor.jpg");
		iskin9 = new ImageIcon("carteAlastor.jpg");
		iskin10 = new ImageIcon("carteAlastor.jpg");
		
		lSkin1 = new JButton(iskin1);
		lSkin1.setBounds(75,75, 89, 148);
		lSkin1.addActionListener(this);
		lSkin2 = new JButton(iskin2);
		lSkin2.setBounds(200,75, 89, 148);
		lSkin2.addActionListener(this);
		lSkin3 = new JButton(iskin3);
		lSkin3.setBounds(325,75, 89, 148);
		lSkin3.addActionListener(this);
		lSkin4 = new JButton(iskin4);
		lSkin4.setBounds(450,75, 89, 148);
		lSkin4.addActionListener(this);
		lSkin5 = new JButton(iskin5);
		lSkin5.setBounds(575,75, 89, 148);
		lSkin5.addActionListener(this);
		
		
		
		pGeneral.add(lSkin1);
		pGeneral.add(lSkin2);
		pGeneral.add(lSkin3);
		pGeneral.add(lSkin4);
		pGeneral.add(lSkin5);
		
		pGeneral.add(bSkin1);
		pGeneral.add(bSkin2);
		pGeneral.add(bSkin3);
		pGeneral.add(bSkin4);
		pGeneral.add(bSkin5);
		
		bg.add(bSkin1);
		bg.add(bSkin2);
		bg.add(bSkin3);
		bg.add(bSkin4);
		bg.add(bSkin5);
		
		/* On s'occupe de la deuxieme liste des skins */
		
		bSkin6 = new JRadioButton(); 
		bSkin6.setBounds(110,475, 30, 13);
		bSkin7 = new JRadioButton();
		bSkin7.setBounds(235,475, 30, 13);
		bSkin8 = new JRadioButton();
		bSkin8.setBounds(360,475, 30, 13);
		bSkin9 = new JRadioButton();
		bSkin9.setBounds(485,475, 30, 13);
		bSkin10 = new JRadioButton();
		bSkin10.setBounds(610,475, 30, 13);
		
		lSkin6 = new JButton(iskin6);
		lSkin6.setBounds(75,300, 89, 148);
		lSkin6.addActionListener(this);
		lSkin7 = new JButton(iskin7);
		lSkin7.setBounds(200,300, 89, 148);
		lSkin7.addActionListener(this);
		lSkin8 = new JButton(iskin8);
		lSkin8.setBounds(325,300, 89, 148);
		lSkin8.addActionListener(this);
		lSkin9 = new JButton(iskin9);
		lSkin9.setBounds(450,300, 89, 148);
		lSkin9.addActionListener(this);
		lSkin10 = new JButton(iskin10);
		lSkin10.setBounds(575,300, 89, 148);
		lSkin10.addActionListener(this);
		
		pGeneral.add(lSkin6);
		pGeneral.add(lSkin7);
		pGeneral.add(lSkin8);
		pGeneral.add(lSkin9);
		pGeneral.add(lSkin10);
		
		pGeneral.add(bSkin6);
		pGeneral.add(bSkin7);
		pGeneral.add(bSkin8);
		pGeneral.add(bSkin9);
		pGeneral.add(bSkin10);
		
		bg.add(bSkin6);
		bg.add(bSkin7);
		bg.add(bSkin8);
		bg.add(bSkin9);
		bg.add(bSkin10);
		
		/* On recherche l'affichage sauvegarde */
		
		this.rechercheAffichage();
		
		/* On s'occupe des boutons du panel avec bouton */
		
		ok = new JButton("Ok");
		ok.addActionListener(this);
		ok.setBounds(500,500, 50, 20);
		pGeneral.add(ok);
		
		quitter = new JButton("Quitter");
		quitter.addActionListener(this);
		quitter.setBounds(580,500,100, 20);
		pGeneral.add(quitter); 
		
		/* On cree un Label au nord du panel principal en attendant d'autres instruction */
		
		lTemp = new JLabel("                                       CHAMP VIDE POUR DES AJOUTS D'AFFICHAGE ");
		lTemp.setBackground(Color.LIGHT_GRAY);
		lTemp2 = new JLabel("<html><br/><br/><br/>___________________________________________________________________________________________________________________________________</html>");
		lTemp2.setBackground(Color.LIGHT_GRAY);

		setVisible(true);
	}
	
	/**
	*Permet de rechercher l'affichage courant
	*/
	public void rechercheAffichage()
	{
		JOptionPane conf = new JOptionPane();
		try
		{
			
			FileReader fr = new FileReader ("affichage.txt");
			Scanner scan = new Scanner(fr);
			scan.useDelimiter(" ");
			while (scan.hasNext()) 
			{
				String skin = scan.next();
				if (skin.equals("1"))
					bSkin1.setSelected(true);
				else if (skin.equals("2"))
						bSkin2.setSelected(true);
					else if (skin.equals("3"))
							bSkin3.setSelected(true);
						else if (skin.equals("4"))
								bSkin4.setSelected(true);
							else if (skin.equals("5"))
									bSkin5.setSelected(true);
								else if (skin.equals("6"))
										bSkin6.setSelected(true);
									else if (skin.equals("7"))
											bSkin7.setSelected(true);
										else if (skin.equals("8"))
												bSkin8.setSelected(true);
											else if (skin.equals("9"))
													bSkin9.setSelected(true);
												else if (skin.equals("10"))
													bSkin10.setSelected(true);
			}
		}
		catch (Exception ex)
		{
			conf.showMessageDialog(null,"Erreur dans la lecture du fichier !","Alerte", conf.WARNING_MESSAGE);
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == lSkin1)
			bSkin1.setSelected(true);
		if (e.getSource() == lSkin2)
			bSkin2.setSelected(true);
		if (e.getSource() == lSkin3)
			bSkin3.setSelected(true);
		if (e.getSource() == lSkin4)
			bSkin4.setSelected(true);
		if (e.getSource() == lSkin5)
			bSkin5.setSelected(true);
		if (e.getSource() == lSkin6)
			bSkin6.setSelected(true);
		if (e.getSource() == lSkin7)
			bSkin7.setSelected(true);
		if (e.getSource() == lSkin8)
			bSkin8.setSelected(true);
		if (e.getSource() == lSkin9)
			bSkin9.setSelected(true);
		if (e.getSource() == lSkin10)
			bSkin10.setSelected(true);
		
		
		if (e.getSource() == quitter)
			setVisible(false);
		else if (e.getSource() == ok)
		{
			JOptionPane conf = new JOptionPane();
				try
				{
					BufferedWriter save = new BufferedWriter(new FileWriter("affichage.txt", false));
					PrintWriter pw = new PrintWriter(save);
					String s="";
						
					 
					if (bSkin1.isSelected())
						s += "1";
					else if (bSkin2.isSelected())
						s += "2";
						else if (bSkin3.isSelected())
							s += "3";
							else if (bSkin4.isSelected())
								s += "4";
								else if (bSkin5.isSelected())
									s += "5";
									else if (bSkin6.isSelected())
										s += "6";
										else if (bSkin7.isSelected())
											s += "7";
											else if (bSkin8.isSelected())
												s += "8";
												else if (bSkin9.isSelected())
													s += "9";
													else if (bSkin10.isSelected())
														s += "10";
					pw.write(s);
						
					conf.showMessageDialog(null,"Configuration sauvegardee !","", conf.WARNING_MESSAGE);
					save.close();
				}
				catch(Exception ex)
				{
					conf.showMessageDialog(null,"Erreur dans la sauvegarde !","Alerte", conf.WARNING_MESSAGE);
				}
		}	
	}
}
