/*
 * MyFirst solo Project Spring 2014
 * Matching Card Game
 */

package imagedata;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.Icon;

/**
 * This class draws an image that is used for the front and back of a playing card.
 * 
 * @author Brian Dennis
 * @version 3-22-2014
 */
public class Image implements Icon {
     
    /** the width of the icon. */
    private static final int WIDTH = 75;  //50   //92
    
    /** the height of the icon. */
    private static final int HEIGHT = 107;   //75  //136
    
    /** the color of the image. */
    private final Color myColor;
    
    /** the suite of this image. */
    private final String mySuite;
    
    /** the value of this image. */
    private final String myValue;

    /**
     * This constructor takes in a background color of black and sets it accordingly.
     * 
     * @param theColor the background color wanting to be drawn onto the back of the card
     */
    public Image(final Color theColor) {
            
        myColor = theColor;
        mySuite = null;
        myValue = null;
    }
    
    /**
     * This constructor takes in a background color of white, the suite and the value, and
     * sets them accordingly.
     * 
     * @param theColor the background color wanting to be drawn onto the front of the card
     * @param theSuite the Suite that should be drawn onto the front of the card
     * @param theValue the value that should be drawn onto the front of the card.
     */
    public Image(final Color theColor, final String theSuite, final String theValue) {
        
        myColor = theColor;
        mySuite = theSuite;
        myValue = theValue;
    }
    
    /**
     * Paints the new Icon.
     * {@inheritDoc}
     */
    public void paintIcon(final Component theComponent, final Graphics theGraphics, 
                                                             final int theX, final int theY) {
       
        final Graphics2D graphics = (Graphics2D) theGraphics;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        
        final Color oldColor = graphics.getColor();
        graphics.setColor(myColor);
        graphics.fill3DRect(theX, theY, getIconWidth(), getIconHeight(), true);
        
        if (myColor == Color.BLACK) {
            
            drawBackIcon(graphics);
        }
        
        if (myColor == Color.WHITE) {
            
            drawFrontIcon(graphics);
        }
        
        graphics.setColor(oldColor);
    }

    /**
     * This method draws the backs of the cards.
     * 
     * @param theGraphics the graphics object
     */
    private void drawBackIcon(final Graphics2D theGraphics) {
        
        //make white border line
        theGraphics.setColor(Color.WHITE);
        theGraphics.setStroke(new BasicStroke(2));
        theGraphics.drawRect(getIconWidth() - WIDTH + 3, getIconHeight() - HEIGHT + 4 
                             , WIDTH - 8, HEIGHT - 10);
        //make blue border line
        theGraphics.setColor(Color.BLUE);
        theGraphics.setStroke(new BasicStroke(2));
        theGraphics.drawRect(getIconWidth() - WIDTH + 4, getIconHeight() - HEIGHT + 5 
                             , WIDTH - 9, HEIGHT - 11);
        //draw the text of the icon
        theGraphics.setColor(Color.WHITE);
        theGraphics.drawString("MATCHING", WIDTH / 9, HEIGHT / 2);
        theGraphics.drawString("GAME", WIDTH - 54, HEIGHT - 40);
    }
    
    /**
     * This method draws the backs of the cards.
     * 
     * @param theGraphics the graphics object
     */
    private void drawFrontIcon(final Graphics2D theGraphics) {
        
        theGraphics.setColor(Color.BLACK);
        
        drawValue(theGraphics);
        drawSuite(theGraphics);
    }
    
    /**
     * This helper method draws the value of the card on the icon.
     * 
     * @param theGraphics the graphics object
     */
    private void drawValue(final Graphics2D theGraphics) {
        
        theGraphics.setFont(new Font(Font.SERIF, Font.BOLD, 65));
        
        if ("10".equals(myValue)) {
                
            theGraphics.drawString(myValue, WIDTH - 70, HEIGHT - 35);

        } else {
            
            if (myValue.length() > 2) {
                
                if ("Jack".equals(myValue)) {
                    
                    theGraphics.drawString(myValue.substring(0, 1), WIDTH - 55, HEIGHT - 35);
                    
                } else if ("Ace".equals(myValue)) {
                    
                    theGraphics.drawString(myValue.substring(0, 1), WIDTH - 60, HEIGHT - 35);
                    
                } else {
                    
                    theGraphics.drawString(myValue.substring(0, 1), WIDTH - 65, HEIGHT - 35);
                }
                
            } else {
                
                theGraphics.drawString(myValue, WIDTH - 55, HEIGHT - 32);
            }
        }
    }

    /**
     * This helper method draws the suite image onto the icon.
     * 
     * @param theGraphics the graphics object
     */
    private void drawSuite(final Graphics2D theGraphics) {
        
        theGraphics.setFont(new Font(Font.SERIF, Font.BOLD, 30));
        theGraphics.setStroke(new BasicStroke(2));
        
        final String spade = "\u2660";
        final String heart = "\u2665";
        final String club = "\u2663";
        final String diamond = "\u2666";
        
        if ("Spade".equals(mySuite)) {
           
            
            theGraphics.drawString(spade, WIDTH - 74, HEIGHT - 5);
            theGraphics.drawString(spade, WIDTH - 28, HEIGHT - 80);
           
        } else if ("Heart".equals(mySuite)) {
           
            
            theGraphics.drawString(heart, WIDTH - 74, HEIGHT - 5);
            theGraphics.drawString(heart, WIDTH - 28, HEIGHT - 80);
           
        } else if ("Club".equals(mySuite)) {
           
            theGraphics.drawString(club, WIDTH - 74, HEIGHT - 5);
            theGraphics.drawString(club, WIDTH - 28, HEIGHT - 80);
           
        } else if ("Diamond".equals(mySuite)) {
           
            theGraphics.drawString(diamond, WIDTH - 74, HEIGHT - 5);
            theGraphics.drawString(diamond, WIDTH - 28, HEIGHT - 80);
            
            /*  //My code for making a diamond.
            for (int i = 3, j = 95; i <= 11; i++, j++) {
            
            theGraphics.drawLine(i, j, i + 8, j - 8);
            
            }
        
            for (int i = 56, j = 12; i <= 64; i++, j++) {
            
            theGraphics.drawLine(i, j, i + 8, j - 8);
            
            }*/
        }
    }        

    /** the width of the icon. 
     * 
     * @return the width of icon
     */
    public int getIconWidth() {
        return WIDTH;
    }

    /** the height of the icon. 
     * 
     * @return the height of icon
     */
    public int getIconHeight() {
        return HEIGHT;
    }
    
    /**
     * returns a string variation of the main color of this image.
     * 
     * @return the color of this image
     */
    public String toString() {
        
        return myColor.toString();
    }
}