/*
 * 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.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

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

/**
 * Draws results
 * 
 * @author chm
 */
public class DisplayScore extends JComponent {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 3256725091285872945L;

    private Grille laGrille;

    private Font laFont = new Font("Arial", Font.BOLD, 16);

    private FontMetrics fm = getFontMetrics(laFont);

    private Image offscreenImg;

    private Graphics offscreenG;

    private static final String POINTS = "POINTS";

    private static final String LINES = "LIGNES";

    private static final String LEVEL = "NIVEAU";

    private static final String NEXT = "SUIVANTE";

    public DisplayScore(Grille pGrille) {
        super();
        setBackground(Color.black);
        laGrille = pGrille;
        laGrille.ds = this;
        setPreferredSize(pGrille.getPreferredSize());
    }
    @Override
    public void update(Graphics g) {
        paint(g);
    }
    @Override
    public void paint(Graphics g) {
        if (laGrille == null)
            return;

        if (offscreenImg == null) {
            offscreenImg = createImage(this.getSize().width, this.getSize().height);
            offscreenG = offscreenImg.getGraphics();
        }

        String s;

        offscreenG.setColor(Color.black);
        offscreenG.fillRect(0, 0, this.getSize().width, this.getSize().height);

        offscreenG.setFont(laFont);

        // Affichage des Points
        offscreenG.setColor(new Color(0, 128, 224));
        offscreenG.drawString(POINTS, (this.getSize().width - fm.stringWidth(POINTS)) / 2, 15);
        s = "" + laGrille.cptPoints;
        offscreenG.drawString(s, (this.getSize().width - fm.stringWidth(s)) / 2, 30);

        // Affichage des Lignes
        offscreenG.setColor(new Color(0, 192, 0));
        offscreenG.drawString(LINES, (this.getSize().width - fm.stringWidth(LINES)) / 2, 50);
        s = "" + laGrille.cptLines;
        offscreenG.drawString(s, (this.getSize().width - fm.stringWidth(s)) / 2, 65);

        // Affichage du niveau
        offscreenG.setColor(Color.yellow);
        offscreenG.drawString(LEVEL, (this.getSize().width - fm.stringWidth(LEVEL)) / 2, 85);
        s = "" + laGrille.level;
        offscreenG.drawString(s, (this.getSize().width - fm.stringWidth(s)) / 2, 100);

        if (laGrille.gameFinished) {
            ImageIcon ii = IhmFactory.getIconFromResource(ImageResources.EDITEUR);
            offscreenG.drawImage(ii.getImage(), (getWidth() - ii.getIconWidth()) / 2, getHeight()-ii.getIconHeight()-5, ii.getIconWidth(), ii.getIconHeight(), this);
        } else {
            // Affichage de la prochaine pièce
            offscreenG.setColor(Color.white);
            offscreenG.drawString(NEXT, (this.getSize().width - fm.stringWidth(NEXT)) / 2, 140);
            // La pièce est créée et centrée
            Piece p = new Piece(laGrille.nextOne);
            p.firstRotation();
            p.reset((this.getSize().width - p.getWidth() * Piece.SQUARE_SIZE) / 2, 155);
            p.display(offscreenG);
        }

        // Transfert du buffer vers l'écran
        g.drawImage(offscreenImg, 0, 0, this);
    }
}
