package j3deditor.bin.components;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.RenderingHints;

import javax.swing.JPanel;


/**
 * Half transparent panel with gradient background and rounded corners.
 * 
 * @author Risto Seene
 */
public class TransparentPanel extends JPanel{
	
	/** width of the border. */
	private int border = 0;
	
	/** radius of the rounded corner. */
	private int arc = 0;
	
	/** background color. */
	private Color color = Color.gray;
	
	/** transparency of the panel. */
	private float transparency = 0.5f;
	
	/** color of the top of the background. */
	private Color startColor;
	
	/** color of the bottom of the background. */
	private Color endColor;

	
	/**
	 * Creates an instance of <code>TransparentPanel</code> with the specified border with,
	 * arc radius and color.
	 * 
	 * @param border
	 * 	width of the border
	 * @param arcRadius
	 * 	radius of the rounded corner
	 * @param color
	 * 	background color
	 * @param transparency
	 * 	transparency of the panel between 0 and 1
	 */
	public TransparentPanel(int border, int arcRadius, Color color, float transparency){
		this.border = border;
		this.arc = arcRadius;
		this.color = color;
		this.transparency = transparency;
		initPanel();
	}
	
	
	/**
	 * Creates an instance of <code>TransparentPanel</code> with the specified border with,
	 * arc radius and color.
	 * <br/><br/>
	 * transparency - 0.5<br/>
	 * 
	 * @param border
	 * 	width of the border
	 * @param arcRadius
	 * 	radius of the rounded corner
	 * @param color
	 * 	background color
	 */
	public TransparentPanel(int border, int arcRadius, Color color){
		this.border = border;
		this.arc = arcRadius;
		this.color = color;
		initPanel();
	}
	
	
	/**
	 * Creates an instance of <code>TransparentPanel</code> with the specified border with and arc radius.
	 * <br/><br/>
	 * color - gray<br/>
	 * transparency - 0.5<br/>
	 * 
	 * @param border
	 * 	width of the border
	 * @param arcRadius
	 * 	radius of the rounded corner
	 */
	public TransparentPanel(int border, int arcRadius){
		this.border = border;
		this.arc = arcRadius;
		initPanel();
	}


	/**
	 * Creates an instance of <code>TransparentPanel</code> with default values.
	 * <br/><br/>
	 * border with - 0<br/>
	 * arc radius - 0<br/>
	 * color - gray<br/>
	 * transparency - 0.5<br/>
	 */
	public TransparentPanel(){
	          initPanel();
	}


	/**
	 * Initializes this panel properly.
	 */
	private void initPanel(){
		setOpaque(false);
		
		int[] rgb = new int[]{color.getRed(), color.getGreen(), color.getBlue()};
		startColor = new Color(calculateValue(rgb[0], 50), calculateValue(rgb[1], 50), calculateValue(rgb[2], 50), calculateValue(transparency, 50));
		endColor = new Color(rgb[0], rgb[1], rgb[2], calculateValue(transparency, -10));
	}


	/**
	 * Calculates new value of a component of <code>Color</code> object.
	 * 
	 * @param oldValue
	 * 	previous value
	 * @param addition
	 * 	change of the value
	 * @return new value which is always between 0 and 255
	 */
	private int calculateValue(int oldValue, int addition){
		oldValue += addition;
		if(oldValue < 0){
			oldValue = 0;
		}
		else if(oldValue > 255){
			oldValue = 255;
		}
		
		return oldValue;
	}
	
	
	/**
	 * Calculates new value of a component of <code>Color</code> object.
	 * 
	 * @param oldValue
	 *        previous value
	 * @param addition
	 *        change of the value
	 * @return new value which is always between 0 and 255
	 */
	private int calculateValue(float oldValue, int addition){
		return calculateValue((int)(oldValue*255), addition);
	}


	/**
	 * Paints this panel.
	 * 
	 * @param graphics
	 * 	<code>Graphics</code> object sent by method caller.
	 */
	@Override
	protected void paintComponent(Graphics graphics){
		Graphics2D g = (Graphics2D)graphics;
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		super.paintComponent(g);
		
		int width = getWidth() - 2*border;
		int height = getHeight() - 2*border;
		
	          GradientPaint gp = new GradientPaint(0, 0, startColor, 0, getHeight(), endColor);
	          g.setPaint(gp);
		g.fillRoundRect(border, border, width, height, arc, arc);
	}
}
