package widget;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;



/**
 * A 3-D View (from the Model-View-Controller pattern)
 * for a Button.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ButtonView extends AbstractButtonView
{
    protected Button           button;
    protected Color            darkColor, lightColor;

    /**
     * Constructor
     *
     * @param button        The button this view belongs to
     * @param width         The width of the button (in pixels)
     * @param height        The height of the button (in pixels)
     */
    public ButtonView(Button button, int width, int height)
    {
	this(width, height);

	this.button = button;
	setMinimumSize(width, height);
	button.setSize(width, height);


	colors = ColorScheme.getButtonColors();
	button.setBackground(ColorScheme.FRAME);

	lightColor = colors[UNFOCUSED][BACKGROUND].brighter();
	darkColor  = colors[UNFOCUSED][BACKGROUND].darker();
    }


    /**
     * Constructor
     *
     * @param width         The width of the button (in pixels)
     * @param height        The height of the button (in pixels)
     */
    public ButtonView(int width, int height)
    {
	super(width, height);
    }



    /**
     * Get the Button associated with this view
     *
     * @return   The Button
     */
    public Widget getWidget()
    {
	return button;
    }




    /**
     * Paint this component
     *
     * @param g  The Graphics context to use
     */
    public void paint(Graphics g)
    {
	paintBackground(g);
	paintText(g);
	paintBorder(g);
    }




    /**
     * Paint the background
     *
     * @param g   The Graphics context to use
     */
    protected void paintBackground(Graphics g)
    {
	Color      bgColor;
	Dimension  size;
	int        h, w, x, y;



	size = button.getSize();

	// Fill the background
	g.setColor(button.getBackground());
	g.fillRect(0,0,size.width,size.height);


	// Fill the button
	if (button.hasFocus()) g.setColor(colors[FOCUSED][BACKGROUND]);
	else                   g.setColor(colors[UNFOCUSED][BACKGROUND]);

	if (shape == button.RECTANGLE) 
        {
	    g.fillRect(0,0,size.width,size.height);
	} 
        else if (shape == button.ROUNDED_RECTANGLE) 
        {
	    g.fillRoundRect(0,0,size.width,size.height,
			    arcWidth,arcHeight);
	}


	// Render the icon (if one exists)
	//
	if (enabledIcon != null) 
        {
	    h = enabledIcon.getHeight(null);
	    w = enabledIcon.getWidth(null);
	    x = (size.width  - w)/2;
	    y = (size.height - h)/2;

	    // Modify the icon location based on whether 
	    // the button is UP or DOWN
	    if (viewState == DOWN) 
            {
		x += 2;
		y += 2;
	    }
	    g.drawImage(enabledIcon, x, y, null);
	}

    }




    /**
     * Paint the border
     *
     * @param g   The Graphics context to use
     */
    protected void paintBorder(Graphics g)
    {
	Color       brColor, ulColor;
	Dimension   size;



	size = button.getSize();

	brColor = darkColor;
	ulColor = darkColor;

	// Determine the border colors
	if (viewState == UP) 
        {
	    brColor = darkColor;
	    ulColor = lightColor;
	} 
        else if (viewState == DOWN) 
        {
	    brColor = lightColor;
	    ulColor = darkColor;
	} 



	// Draw the border
	if (shape == button.RECTANGLE) 
        {
	    g.setColor(ulColor);
	    g.drawLine(0,0,size.width-1,0);
	    g.drawLine(0,0,0,size.height-1);
	    
	    g.setColor(brColor);
	    g.drawLine(0,size.height-1,size.width-1,size.height-1);
	    g.drawLine(size.width-1,0,size.width-1,size.height-1);
	} 
        else if (shape == button.ROUNDED_RECTANGLE) 
        {
	    g.setColor(ulColor);
	    g.drawLine(arcWidth/2,0,size.width-1-arcWidth/2,0);
	    g.drawLine(0,arcHeight/2,0,size.height-1-arcHeight/2);
	    // Upper left
	    g.drawArc(0,0,arcWidth,arcHeight,90,90);

	    // Upper right
	    g.drawArc(size.width-1-arcWidth,0,arcWidth,arcHeight,45,45);
	    // Lower left
	    g.drawArc(0,size.height-1-arcHeight,arcWidth,arcHeight,180,45);



	    g.setColor(brColor);
	    g.drawLine(arcWidth/2,size.height-1,
		       size.width-1-arcWidth/2,size.height-1);
	    g.drawLine(size.width-1,arcHeight/2,
		       size.width-1,size.height-1-arcHeight/2);
	    //Upper right
	    g.drawArc(size.width-1-arcWidth,0,
		      arcWidth,arcHeight,0,45);
	    // Lower left
	    g.drawArc(0,size.height-1-arcHeight,
		      arcWidth,arcHeight,225,45);
	    // Lower right
	    g.drawArc(size.width-1-arcWidth,size.height-1-arcHeight,
		      arcWidth,arcHeight,270,90);
	}
    }



    /**
     * Paint the text
     *
     * @param g   The Graphics context to use
     */
    protected void paintText(Graphics g)
    {
	Dimension   size;
	int         h, w, x, y;


	size = button.getSize();

	// Short circuit if no text
	if ((text == null) || (text.equals(""))) return;

	// Determine the size of the text
	if (textSize == null) 
        {
	    fm = g.getFontMetrics();
	    h = fm.getMaxAscent();
	    w = fm.stringWidth(text);
	    setTextSize(new Dimension(w, h));
	}

	// Determine the horizontal location
	if (horizontalTextPosition == button.LEFT) 
        {
	    x = 0;
	} 
        else if (horizontalTextPosition==button.RIGHT) 
        {
	    x = size.width - textSize.width;
	} 
        else 
        { 
	    x = (size.width - textSize.width)/2;
	}
	
	// Determine the vertical location
	if (verticalTextPosition == button.TOP) 
        {
	    y = textSize.height;
	} 
        else if (verticalTextPosition==button.BOTTOM) 
        {
	    y=size.height - fm.getMaxDescent();
	} 
        else 
        {
	    y = (size.height + textSize.height)/2;
	}

	
	// Modify the text location based on whether the button is UP or DOWN
	if (viewState == DOWN) 
        {
	    x += 2;
	    y += 2;
	}
	

	// Render the text
	g.setFont(button.getFont());
	g.setColor(colors[FOCUSED][FOREGROUND]);
	g.drawString(text, x, y);
    }





}

