package card;
import game.BaseGame;
import game.BaseGame.MSG_TYPES;

import java.awt.Color;

/*
 * COLORS:
 * GREEN
 * RED
 * YELLOW
 * WHITE
 * BLUE
 * (COLORED)
 */
public class Card implements Cloneable {
	protected int color;
	protected int value;
	protected int playerId;
	protected boolean isSelected;
	protected BaseGame g;
		
	public Card(int color, int value, int playerId, BaseGame g){
		this.color = color;
		this.value = value;
		this.playerId = playerId;
		this.g = g;
		isSelected = false;
	}
	
	public Card clone(){
		Card card = new Card(color, value, playerId, g);
		return card;
	}
	
	protected void onCardClick(){
		if(isSelected){
			g.removeSelectedCards();
			isSelected = false;
			if(g.getSelectedCards() == 0){
				g.resetMoveValues();
			}
		}
		if(!g.isUseColor() && !g.isUseValue() && !g.isTrashCard() && !g.isPlaceCard()){
			g.printMessage("Please choose move type first", MSG_TYPES.ERROR);
			return;
		}
		if(playerId == g.getCurrentPlayerIndex()){
			if(g.isUseColor() || g.isUseValue()){
				g.printMessage("Please don't select your own cards!", MSG_TYPES.ERROR);
				return;
			} else if((g.isTrashCard() || g.isPlaceCard()) && g.getSelectedCards() == 1){
				g.printMessage("You already selected a card!", MSG_TYPES.ERROR);
				return;
			}
		} else if(playerId != g.getCurrentPlayerIndex() && (g.isTrashCard() || g.isPlaceCard())){
			g.printMessage("Please select your own cards", MSG_TYPES.ERROR);
			return;
		}
		if(g.getSelectedPlayer() == -1){
			g.setSelectedPlayer(playerId);
			g.setChosenColor(color);
			g.setChosenValue(value);
			g.addSelectedCards();
			isSelected = true;
			return;
		}
		if(g.getSelectedPlayer() != playerId){
			g.printMessage("Please choose a card of the selected player or unselect all cards and choose another player.", MSG_TYPES.ERROR);
			return;
		}
		if(g.getChosenColor() == -1){
			g.setChosenColor(color);
		}
		if(g.getChosenValue() == -1){
			g.setChosenValue(value);
		}
		if(!isSelected){
			if(g.isUseColor()){
				if(g.getChosenColor() == color){
					if(!isSelected){
						g.addSelectedCards();
						isSelected = true;
					}
				}
			} else if(g.isUseValue()){
				if(g.getChosenValue() == value){
					if(!isSelected){
						g.addSelectedCards();
						isSelected = true;
					}
				}
			}
		}
	}
	
	@Override
	public String toString(){
		return "Color: " + intColorToText(color) + ", Value: " + value;
	}
	
	public Color getColor(){
		Color c = null;
		switch(color){
		case 0:
			c = Color.green;
			break;
		case 1:
			c = Color.red;
			break;
		case 2:
			c = Color.yellow;
			break;
		case 3:
			c = Color.white;
			break;
		case 4:
			c = Color.blue;
			break;
		}
		return c;
	}
	
	public static String intColorToText(int id){
		String color = "";
		switch(id){
		case 0:
			color = "green";
			break;
		case 1:
			color = "red";
			break;
		case 2:
			color = "yellow";
			break;
		case 3:
			color = "white";
			break;
		case 4:
			color = "blue";
			break;
		}
		return color;
	}
	
	public boolean isSelected(){
		return isSelected;
	}
	
	public void setSelected(boolean set){
		isSelected = set;
	}
	
	public int getColorInt(){
		return color;
	}
	
	public int getValue(){
		return value;
	}
	
	public void setProps(Card c){
		this.value = c.getValue();
		this.color = c.getColorInt();
	}
}