/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.ui.ia;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import fr.gouv.finances.cp.utils.ui.IhmFactory;
import fr.gouv.finances.cp.xemelios.ui.ImageResources;

public class Plateau extends JFrame {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 3258413915443704633L;
    
    protected static Color BACK_COLOR = new Color(212, 208, 200);
    private static IhmFactory iF = IhmFactory.newInstance();

    JPanel contentPane;

    JPanel buttonPane = new JPanel();

    JButton jButtonEffacer;
    JButton pbOrdi;

    JPanel jPanelJeu = new JPanel();

    Joueur joueur = new Joueur("X",iF.getIconFromResource(ImageResources.ICON_X));
    Joueur ordi = new Joueur("O",iF.getIconFromResource(ImageResources.ICON_O));


    public Plateau() {
        super("Faire une ligne de 5 pions...");
        try {
            jbInit();
            for (int i = 0; i < 100; i++) {
                JButton jbt = new JButton();
                jbt.setBackground(BACK_COLOR);
                jbt.setFont(new java.awt.Font("Dialog", 1, 8));
                jbt.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jButton_actionPerformed(e);
                    }
                });
                jPanelJeu.add(jbt, null);
            }
            Dimension sSz = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension fSz = getSize();
            setLocation((sSz.width - fSz.width) / 2, (sSz.height - fSz.height) / 2);
            setVisible(true);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        contentPane = (JPanel)this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        jButtonEffacer = new JButton("Effacer");
        jButtonEffacer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButtonEffacer_actionPerformed(e);
            }
        });
        jPanelJeu.setLayout(new GridLayout(10,10));

        jPanelJeu.setMaximumSize(new Dimension(300, 2147483647));
        jPanelJeu.setMinimumSize(new Dimension(300, 100));
        jPanelJeu.setPreferredSize(new Dimension(300,300));
        contentPane.add(buttonPane, BorderLayout.EAST);
        buttonPane.add(jButtonEffacer, null);
        contentPane.add(jPanelJeu, BorderLayout.CENTER);
        pack();
    }

    void jButton_actionPerformed(ActionEvent e) {
        JButton source = ((JButton)e.getSource());
        if(source.getActionCommand().length()!=0) {
            JOptionPane.showMessageDialog(this,"Tricheur !");
            return;
        }
        source.setActionCommand(joueur.getMarque());
        source.setIcon(joueur.getIcon());
        joueur.modelise(jPanelJeu);
        if (joueur.victoire(jPanelJeu))
            termine(joueur.getMarque());
        else {
            // c'est a l'autre de jouer
            ordi.joue(jPanelJeu);
            if (ordi.victoire(jPanelJeu))
                termine(ordi.getMarque());
        }
    }

    void jButtonEffacer_actionPerformed(ActionEvent e) {
        for (int i = 0; i < jPanelJeu.getComponentCount(); i++) {
            ((JButton)jPanelJeu.getComponent(i)).setActionCommand("");
            ((JButton)jPanelJeu.getComponent(i)).setIcon(null);
            ((JButton)jPanelJeu.getComponent(i)).setForeground(Color.black);
            ((JButton)jPanelJeu.getComponent(i)).setBackground(BACK_COLOR);
        }
    }

    public void termine(String s) {
        String msg;
        if("X".equals(s)) msg = "Bravo, vous avez gagn�.";
        else msg = "Yeap ! J'ai gagn� !";
        JOptionPane.showMessageDialog(this,msg,"Morpion",JOptionPane.INFORMATION_MESSAGE);
    }

    void pbOrdi_actionPerformed(ActionEvent e) {
        ordi.joue(jPanelJeu);
        if (ordi.victoire(jPanelJeu))
            termine(ordi.getMarque());
    }

    void pbJoueur_actionPerformed(ActionEvent e) {
        joueur.joue(jPanelJeu);
        if (joueur.victoire(jPanelJeu))
            termine(joueur.getMarque());
    }
}
