/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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.Graphics;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class JTRIS extends JDialog implements Runnable, KeyListener {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 3256721801408099890L;
    private Grille laGrille;
    private DisplayScore dspScore;
    private Piece[] listePieces;
    private boolean finished = false;

    public JTRIS(JFrame parent) {
        super(parent);
        listePieces = new Piece[7];
        listePieces[0] = new Piece(0x88C0, 0x2E00, 0xC440, 0xE800, new Color(0, 192, 192)); // cyan
        listePieces[1] = new Piece(0x44C0, 0xE200, 0xC880, 0x8E00, new Color(255, 230, 0)); // jaune
        listePieces[2] = new Piece(0xCC00, 0xCC00, 0xCC00, 0xCC00, new Color(0, 192, 0)); // vert
        listePieces[3] = new Piece(0x8888, 0xF000, 0x8888, 0xF000, new Color(230, 0, 0)); // rouge
        listePieces[4] = new Piece(0x8C40, 0x6C00, 0x8C40, 0x6C00, new Color(0, 128, 224)); // bleu
        listePieces[5] = new Piece(0x4C80, 0xC600, 0x4C80, 0xC600, Color.orange);
        listePieces[6] = new Piece(0x8C80, 0x4E00, 0x4C40, 0xE400, Color.lightGray);
        init();
    }

    public void init() {
        JPanel mainPane = new JPanel(new GridLayout(1, 2, 10, 0));

        laGrille = new Grille(listePieces);
        laGrille.setBackground(Color.black);
        mainPane.add(laGrille);

        dspScore = new DisplayScore(laGrille);
        mainPane.add(dspScore);
        
        setContentPane(mainPane);
        addKeyListener(this);
        pack();
    }
    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        if(b) {
            laGrille.start();
            start();
        } else {
            stop();
            laGrille.stop();
        }
    }
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.orange);
        g.fill3DRect(0, 0, this.getSize().width, this.getSize().height, true);

        if (dspScore != null)
            dspScore.repaint();
    }

    public void start() {
        finished=false;
    }

    public void stop() {
        finished=true;
    }

    public void run() {
        while(!finished) {
            repaint();
            try {
                Thread.sleep(300);
            }
            catch (InterruptedException e) {}
        }
    }
    public void keyPressed(KeyEvent e) {
        laGrille.keyPressed(e);
    }
    public void keyReleased(KeyEvent e) {
        laGrille.keyReleased(e);
    }
    public void keyTyped(KeyEvent e) {
        laGrille.keyTyped(e);
    }
}