/**
 * @(#)Square.java	11/03/05
 * 
 * Copyright 2005 3GP01, KCL.  All unmodified code is copyright of its respective owner.
 * 
 * This program 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.
 *
 * This program 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.
 *
 * The GNU General Public Licence should be contained within licence.txt;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.awt.Color;
import javax.swing.ImageIcon;

/**
 * This class builds and controls a Square of the Board. Icon construction is
 * handled here.
 * 
 * Square is abstract, and each type of square is supered from it.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Board
 */

public abstract class Square 
{
	protected Letter letter;

	protected int x;

	protected int y;

	//icons
	protected ImageIcon icon;

	protected ImageIcon ordinary = new ImageIcon(
			"resources/images/tiles/board/universal/ordinarysquare.png");

	protected ImageIcon ordinaryrollover = new ImageIcon(
			"resources/images/tiles/board/universal/ordinarysquarerollover.png");

	protected ImageIcon ordinarypressed = new ImageIcon(
			"resources/images/tiles/board/universal/ordinarysquarepressed.png");

	protected ImageIcon centre = new ImageIcon(
			"resources/images/tiles/board/universal/centresquare.png");

	protected ImageIcon centrerollover = new ImageIcon(
			"resources/images/tiles/board/universal/centresquarerollover.png");

	protected ImageIcon centrepressed = new ImageIcon(
			"resources/images/tiles/board/universal/centresquarepressed.png");

	Color turquoise = new Color(0, 102, 102);

	Color green = new Color(117, 192, 67);

	Color red = new Color(220, 0, 49);

	Color pink = new Color(195, 107, 171);

	Color lightBlue = new Color(72, 151, 210);

	Color blue = new Color(58, 83, 164);

	public Square() 
	{
	}

	public Square(int xx, int yy) 
	{
		x = xx;
		y = yy;
	}

	public Square(Letter l) 
	{
		letter = l;
	}

	public Scrabble getScrabble() 
	{
		return Scrabble.scrabble;
	}

	public void setLetter(Letter l) 
	{
		letter = l;
	}

	public void setX(int xx) 
	{
		x = xx;
	}

	public void setY(int yy) 
	{
		y = yy;
	}

	public ImageIcon getIcon(Color color) 
	{
		Color squareColor = getColor();
		if (squareColor == turquoise) {
			return ordinary;
		} else if (squareColor == lightBlue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doubleletterscoresquare.png");
		} else if (squareColor == blue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/tripleletterscoresquare.png");
		} else if (squareColor == pink) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doublewordscoresquare.png");
		} else if (squareColor == red) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/triplewordscoresquare.png");
		} else if (squareColor == green) {
			return centre;
		} else {
			return null;
		}
	}

	public ImageIcon getDisabledIcon(Color color) 
	{
		Color squareColor = getColor();
		if (squareColor == turquoise) {
			return ordinary;
		} else if (squareColor == lightBlue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doubleletterscoresquare.png");
		} else if (squareColor == blue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/tripleletterscoresquare.png");
		} else if (squareColor == pink) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doublewordscoresquare.png");
		} else if (squareColor == red) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/triplewordscoresquare.png");
		} else if (squareColor == green) {
			return centre;
		} else {
			return null;
		}
	}

	public ImageIcon getRolloverIcon(Color color) 
	{
		Color squareColor = getColor();
		if (squareColor == turquoise) {
			return ordinaryrollover;
		} else if (squareColor == lightBlue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doubleletterscoresquarerollover.png");
		} else if (squareColor == blue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/tripleletterscoresquarerollover.png");
		} else if (squareColor == pink) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doublewordscoresquarerollover.png");
		} else if (squareColor == red) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/triplewordscoresquarerollover.png");
		} else if (squareColor == green) {
			return icon = centrerollover;
		} else {
			return null;
		}
	}

	public ImageIcon getPressedIcon(Color color) 
	{
		Color squareColor = getColor();
		if (squareColor == turquoise) {
			return icon = ordinarypressed;
		} else if (squareColor == lightBlue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doubleletterscoresquarepressed.png");
		} else if (squareColor == blue) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/tripleletterscoresquarepressed.png");
		} else if (squareColor == pink) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/doublewordscoresquarepressed.png");
		} else if (squareColor == red) {
			return icon = new ImageIcon("resources/images/tiles/board/"
					+ getScrabble().getI18n().getLanguage() + "/"
					+ getScrabble().getI18n().getCountry()
					+ "/triplewordscoresquarepressed.png");
		} else if (squareColor == green) {
			return icon = centrepressed;
		} else {
			return null;
		}
	}

	public abstract String getDescription();

	public int getX() 
	{
		return x;
	}

	public int getY() 
	{
		return y;
	}

	public void clear() 
	{
		letter = null;
	}

	public Letter getLetter() 
	{
		return letter;
	}

	public Letter getLetterFromSymbol(char sym) 
	{
		if (sym == letter.getSymbol()) {
			return letter;
		} else
			return null;
	}

	public boolean isOccupied() 
	{
		return letter != null;
	}

	// hook method:
	public abstract int getLetterScore(Letter l);

	// template method:
	public int getLetterScore() 
	{
		if (letter == null) {
			return 0;
		}
		return getLetterScore(letter);
	}

	public abstract int getWordScore();

	// hook method:
	public abstract String squareSymbol();

	// template method:
	public String toString() 
	{
		String res = "[";
		if (letter != null) {
			res = res + letter + "]";
		} else {
			res = res + squareSymbol() + "]";
		}
		return res;
	}

	public String getText() 
	{
		if (letter != null) 
		{
			return letter + "";
		}
		return squareSymbol();
	}

	public abstract Color getColor();

	public abstract Object clone();
}

/**
 * This class builds ordinary squares that have no special properties.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Board
 */
class OrdinarySquare extends Square 
{
	public OrdinarySquare(Letter l) 
	{
		super(l);
	}

	public OrdinarySquare() 
	{
		super();
	}

	public OrdinarySquare(int xx, int yy) 
	{
		super(xx, yy);
	}

	public Object clone() 
	{
		Square res = new OrdinarySquare(letter);
		res.setX(x);
		res.setY(y);
		return res;
	}

	public int getLetterScore(Letter l) 
	{
		return l.getScore();
	}

	public int getWordScore() 
	{
		return 1;
	}

	public String squareSymbol() 
	{
		return "-";
	}

	public Color getColor() 
	{
		return turquoise;
	}

	public String getDescription() 
	{
		return "Any letter placed on this square will be worth only its score.";
	}
}

/**
 * This class builds the centre square, which has a star, and is a double word square.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Board
 */
class CentreSquare extends Square 
{
	public CentreSquare(Letter l) 
	{
		super(l);
	}

	public CentreSquare() 
	{
		super();
	}

	public CentreSquare(int xx, int yy) 
	{
		super(xx, yy);
	}

	public Object clone() 
	{
		Square res = new CentreSquare(letter);
		res.setX(x);
		res.setY(y);
		return res;
	}

	public int getLetterScore(Letter l) 
	{
		return l.getScore();
	}

	public int getWordScore() 
	{
		return 2;
	}

	public String squareSymbol() 
	{
		return "_";
	}

	public Color getColor() 
	{
		return green;
	}

	public String getDescription() 
	{
		return "Any word containing this square will be worth double.";
	}
}

/**
 * This class builds double letter squares.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Board
 */
class DoubleLetterSquare extends Square 
{
	public DoubleLetterSquare() 
	{
		super();
	}

	public DoubleLetterSquare(int xx, int yy) 
	{
		super(xx, yy);
	}

	public DoubleLetterSquare(Letter l) 
	{
		super(l);
	}

	public Object clone() 
	{
		Square res = new DoubleLetterSquare(letter);
		res.setX(x);
		res.setY(y);
		return res;
	}

	public int getLetterScore(Letter l) 
	{
		return 2 * l.getScore();
	}

	public int getWordScore() 
	{
		return 1;
	}

	public String squareSymbol() 
	{
		return "~";
	}

	public Color getColor() 
	{
		return lightBlue;
	}

	public String getDescription() 
	{
		return "Any letter placed on this square will be worth double.";
	}
}

/**
 * This class builds triple letter squares.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Board
 */
class TripleLetterSquare extends Square 
{
	public TripleLetterSquare() 
	{
		super();
	}

	public TripleLetterSquare(Letter l) 
	{
		super(l);
	}

	public TripleLetterSquare(int xx, int yy) 
	{
		super(xx, yy);
	}

	public Object clone() 
	{
		Square res = new TripleLetterSquare(letter);
		res.setX(x);
		res.setY(y);
		return res;
	}

	public int getLetterScore(Letter l) 
	{
		return 3 * l.getScore();
	}

	public int getWordScore() 
	{
		return 1;
	}

	public String squareSymbol() 
	{
		return "=";
	}

	public Color getColor() 
	{
		return blue;
	}

	public String getDescription() 
	{
		return "Any letter placed on this square will be worth triple.";
	}
}

/**
 * This class builds double word squares.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Board
 */
class DoubleWordSquare extends Square 
{
	public DoubleWordSquare(Letter l) 
	{
		super(l);
	}

	public DoubleWordSquare() 
	{
		super();
	}

	public DoubleWordSquare(int xx, int yy) 
	{
		super(xx, yy);
	}

	public Object clone() 
	{
		return new DoubleWordSquare(letter);
	}

	public int getLetterScore(Letter l) 
	{
		return l.getScore();
	}

	public int getWordScore() 
	{
		return 2;
	}

	public String squareSymbol() 
	{
		return "*";
	}

	public Color getColor() 
	{
		return pink;
	}

	public String getDescription() 
	{
		return "Any word containing this square will be worth double.";
	}
}

/**
 * This class builds triple word squares.  Pesky blighters.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Board
 */
class TripleWordSquare extends Square 
{
	public TripleWordSquare(Letter l) 
	{
		super(l);
	}

	public TripleWordSquare() 
	{
		super();
	}

	public TripleWordSquare(int xx, int yy) 
	{
		super(xx, yy);
	}

	public Object clone() 
	{
		return new TripleWordSquare(letter);
	}

	public int getLetterScore(Letter l) 
	{
		return l.getScore();
	}

	public int getWordScore() 
	{
		return 3;
	}

	public String squareSymbol() 
	{
		return "#";
	}

	public Color getColor() 
	{
		return red;
	}

	public String getDescription() 
	{
		return "Any word containing this square will be worth triple.";
	}
}

