package Darts;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class DartScorePane extends JPanel
	{
	//
	// Member Data
	//
	private DisplayScore 	score = null;
	private int fontType;

	//
	// Constructor
	//
	DartScorePane( String title, Color bgColor, boolean boldName )
		{
		setBorder(BorderFactory.createTitledBorder( title ));
		setBackground( bgColor );
		fontType = boldName ? Font.BOLD : Font.PLAIN;
		}
	
	public void setScore( DisplayScore newScore )
		{
		score = newScore;
		repaint();
		}
	
	@Override
  protected void paintComponent(Graphics g)
		{
		int  width = getWidth() - 5;
		int  height = getHeight() - 1;
				
		// Paint background if we're opaque.
		if( isOpaque() )
			{
			g.setColor( getBackground() );
			g.fillRect(0, 0, getWidth(), getHeight());
			}

		if( score == null )
			return; // <----------- Exit if there is nothing to paint
		
		
//		g.setColor(Color.GREEN);
//		g.drawLine( 0, height/2, width/2, 0 );
//		g.drawLine( width/2, 0, width, height/2 );
//		g.drawLine( width, height/2, width/2, height );
//		g.drawLine( width/2, height, 0, height/2 );
		
		// Builds and their metrics
		int fontSize = ( height * 18 ) / 100;
		Font  				nameFont = new Font( "Serif", fontType, (fontSize/3)*4 );
		FontMetrics  	nameFontMetrics = new FontMetrics( nameFont ){};
		Font 					numFont = new Font( "SansSerif", Font.BOLD, fontSize );
		FontMetrics 	numFontMetrics = new FontMetrics( numFont ){};
		Font 					smallFont = new Font( "SansSerif", Font.BOLD, (fontSize/4)*3 );
		// 	smallFontMetrics = new FontMetrics( smallFont ){};
		Rectangle2D  rect;
		int x, y;
		// Calc Text Possitions
		if( score.name != null )
			{
			g.setColor( getForeground() );
			g.setFont( nameFont );
			rect = nameFontMetrics.getStringBounds( score.name, g );
			x = ( width - (int)rect.getWidth() ) / 2;
			y = ( height * 24 ) / 100;
			g.drawString( score.name, x, y );
			}
		else
			y = 5;
		
		g.setFont( numFont );
		rect = numFontMetrics.getStringBounds( "100", g );
		int tabStop1 = (int)rect.getWidth();
		int tabStop2 = tabStop1 + ( width * 2 ) / 100;
		int tabStop3 = ( width * 98 ) / 100 - (int)rect.getWidth();
		int tabStop4 = ( width * 98 ) / 100;
		for (int i = 0; i < 3; i++)
			{
			if( score.thrown[i] > 0 )
				{				
				// Draw what was thrown.
				rect = numFontMetrics.getStringBounds( ""+score.value[i], g );
				int startX = tabStop1 - (int)rect.getWidth();
				y += ( height * 18 ) / 100;
				g.drawString( ""+score.value[i], startX, y );
				
				// Draw any multipliers
				String  multiplier = "";
				if( score.thrown[i] == 2 )
					multiplier += "double ";
				else if( score.thrown[i] == 3 )
					multiplier += "triple ";
				if( score.stolen[i] > 0 )
					multiplier += "+ " + score.stolen[i] + " stolen";
				g.setFont( smallFont );
				g.drawString( multiplier, tabStop2, y );
				
				// Draw the dart's total point score (with box around it)
				
				g.setColor( Color.WHITE );
				g.fillRect( tabStop4-tabStop1, y-numFontMetrics.getMaxAscent()+2, width-(tabStop4-tabStop1), (height*18)/100);
				g.setColor( Color.BLUE );
				g.drawRect( tabStop4-tabStop1, y-numFontMetrics.getMaxAscent()+2, width-(tabStop4-tabStop1), (height*18)/100 );
				g.setColor( getForeground() );
				
				g.setFont( numFont );
				rect = numFontMetrics.getStringBounds( ""+score.dartScore[i], g );
				g.drawString( ""+score.dartScore[i], tabStop4-(int)rect.getWidth(), y );
				
				// If stolen scratch out
				if( score.gone[i] )
					{
					g.setColor( Color.RED );
					g.fillRect( startX, y-(height*7)/100, tabStop4-startX, (height*2)/100 );
					g.setColor( Color.BLACK );
					}
				}
			}
		
		// Draw sum line
		y += ( height * 3 ) / 100;
		g.fillRect( tabStop3, y, width-tabStop3-1, (height*3)/100 ); // width=tabStop1
		
		// Draw the dart's total point score
		rect = numFontMetrics.getStringBounds( ""+score.finalScore, g );
		y = ( height * 98 ) / 100;
		g.drawString( ""+score.finalScore, tabStop4-(int)rect.getWidth(), y );

		}

	} // end of class DartScorePane
