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

package model;
import imagedata.Image;

import java.awt.Color;
import java.util.Objects;

/**
 * This abstract class contains methods and code needed for a card to function correctly.
 * 
 * @author Brian Dennis
 * @version 3-22-2014
 */
abstract class AbstractCard implements Card {
    
    /** the String representing the value of the card. */
    private final String myValue;
    
    /** the String representing the suite of the card. */
    private final String mySuite;
    
    /** The Image shown on the front of the card. */
    private final Image myFrontImage;
    
    /** The Image shown on the back of the card. */
    private final Image myBackImage;
    
    /**
     * This constructor initiates all of the variables taking in parameters from 
     * child classes. "myBackImage" is the only variable in this parent class that remains 
     * constant for all child classes.
     * 
     * @param theValue the value of the card being made
     * @param theSuite the suite of the card being made
     * @param theFrontImage the front image of the card being made
     */
    protected AbstractCard(final String theValue, final String theSuite) {
        
        myValue = theValue;
        mySuite = theSuite;
        myFrontImage = new Image(Color.WHITE, mySuite, myValue);
        myBackImage = new Image(Color.BLACK);
    }

    /** {@inheritDoc} */
    public String getValue() {
        
        return myValue;
    }
    
    /** {@inheritDoc} */
    public String getSuite() {
        
        return mySuite;
    }
    
    /** {@inheritDoc} */
    public Image getFrontImage() {
        
        return myFrontImage;
    }
    
    /** {@inheritDoc} */
    public Image getBackImage() {
        
        return myBackImage;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object theOther) {
        
        boolean result = false;
        
        if (this == theOther) {
            result = true;
        } else if (theOther == null) {
            result = false;
        } else if (!(theOther instanceof AbstractCard)) {
            result = false;
        }
        
        final Card other = (Card) theOther;
        if (this != null && other != null) { 
            result = Objects.equals(getValue(), other.getValue()) 
                            && Objects.equals(getSuite(), other.getSuite());
        }  
        return result;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {

        return Objects.hash(myValue);
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        
        final StringBuilder sb = new StringBuilder(50);
        
        sb.append(getValue());
        sb.append(" of ");
        sb.append(getSuite());
        sb.append("s -- FrontImage: ");
        sb.append(getFrontImage().toString());
        sb.append(" -- BackImage: ");
        sb.append(getBackImage().toString());
        
        return sb.toString();
    }
}