/*
 * 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.Graphics;
import java.util.Arrays;

/**
 * Describes a piece on the board
 * @author chm
 */
public class Piece {
    //  contains a representation of the 4 rotations
    private int[] images = new int[4]; 
    //  contains the height 4 rotations
    private int[] heights = new int[4]; 
    //  contains the width des 4 rotations
    private int[] widths = new int[4]; 
    //  Position
    private int x, y; 
    //  last state
    private int lastX, lastY, lastRotation; 
    private Color color; 
    //  current rotation
    private int idRotation; 
    //  unitary size of a square
    public final static int SQUARE_SIZE = 15; 

    // Constructors
    public Piece(int r1, int r2, int r3, int r4, Color color) {
        images[0] = r1;
        images[1] = r2;
        images[2] = r3;
        images[3] = r4;
        this.color = color;
        x = y = idRotation = 0;
        lastX = lastY = -1;
        computesWH();
    }

    public Piece(int r1, int r2, int r3, int r4, Color color, int p_x, int p_y) {
        this(r1, r2, r3, r4, color);
        x = p_x;
        y = p_y;
    }

    public Piece(int[] r, Color color) {
        System.arraycopy(r,0,images,0,4);
        x = y = idRotation = 0;
        lastX = lastY = -1;
        this.color = color;
        computesWH();
    }

    public Piece(int[] r, Color color, int p_x, int p_y) {
        this(r, color);
        x = p_x;
        y = p_y;
    }

    public Piece(Piece src) {
        System.arraycopy(src.images,0,images,0,4);
        System.arraycopy(src.widths,0,widths,0,4);
        System.arraycopy(src.heights,0,heights,0,4);
        idRotation = src.idRotation;
        lastX = src.lastX;
        lastY = src.lastY;
        lastRotation = src.lastRotation;
        x = src.x;
        y = src.y;
        color = src.color;
    }

    public void reset() {
        reset(0, 0);
    }

    public void reset(int p_x, int p_y) {
        x = p_x;
        y = p_y;
        idRotation = 0;
        lastX = lastY = -1;
    }

    public void firstRotation() {
        idRotation = 0;
    }

    private void computesWH() {
        Arrays.fill(widths,0);
        Arrays.fill(heights,0);

        for (int cptRotations = 0; cptRotations < heights.length; cptRotations++) {
            int mask = 0x8000;
            for (int i = 0; i < 4; i++) {
                boolean lineOK = false;
                for (int j = 0; j < 4; j++, mask >>= 1)
                    if ((mask & images[cptRotations]) != 0) {
                        if (!lineOK) {
                            lineOK = true;
                            heights[cptRotations]++;
                        }
                        if (widths[cptRotations] < j + 1)
                            widths[cptRotations] = j + 1;
                    }
            }
        }

    }

    public void rotate() {
//        if (idRotation >= 3) idRotation = 0;
//        else idRotation++;
        idRotation = ((idRotation+1) % 4);
    }
    

    public void goDown() {
        y += SQUARE_SIZE;
    }

    public void goDown(int lines) {
        y += (SQUARE_SIZE * lines);
    }

    public void goLeft() {
        x -= SQUARE_SIZE;
    }

    public void goRight() {
        x += SQUARE_SIZE;
    }

    public void goUp() {
        y -= SQUARE_SIZE;
    }

    public void goUp(int lines) {
        y -= (SQUARE_SIZE * lines);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int paintx() {
        return lastX;
    }

    public int painty() {
        return lastY;
    }

    public Color getColor() {
        return color;
    }

    public int getHeight() {
        return heights[idRotation];
    }

    public int getHeight(int id) {
        return id >= 0 && id < 4 ? heights[id] : 0;
    }

    public int nextHeight() {
        return heights[idRotation >= 3 ? 0 : idRotation + 1];
    }

    public int getWidth() {
        return widths[idRotation];
    }

    public int getWidth(int id) {
        return id >= 0 && id < 4 ? widths[id] : 0;
    }

    public int nextWidth() {
        return widths[idRotation >= 3 ? 0 : idRotation + 1];
    }

    public int getVal() {
        return images[idRotation];
    }

    public int getVal(int id) {
        return id >= 0 && id < 4 ? images[id] : 0;
    }

    public int getNextVal() {
        return images[idRotation >= 3 ? 0 : idRotation + 1];
    }

    public Piece getNextPiece() {
        Piece np = new Piece(this);
        np.rotate();
        return np;
    }

    public synchronized void display(Graphics g) {
        int mask = 0x8000, i, j;

        lastX = x;
        lastY = y;
        lastRotation = idRotation;
        g.setColor(color);
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++, mask >>= 1)
                if ((mask & images[idRotation]) != 0)
                    g.fill3DRect(x + (j * SQUARE_SIZE), y + (i * SQUARE_SIZE), SQUARE_SIZE, SQUARE_SIZE, true);
    }

    public synchronized void erase(Graphics g, Color fond) {
        int mask = 0x8000, i, j;

        if (lastX == -1 || lastY == -1)
            return;

        g.setColor(fond);
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++, mask >>= 1)
                if ((mask & images[lastRotation]) != 0)
                    g.fill3DRect(lastX + (j * SQUARE_SIZE), lastY + (i * SQUARE_SIZE), SQUARE_SIZE, SQUARE_SIZE, true);
    }
}
