import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

// Representation graphique d'une sortie, herite de GCircuitElement
public class GOut extends GCircuitElement {

	public GOut(HWCanvas canvas, Sortie ce) {
		super(canvas, ce);	
		ce.pin_in.get(0).addPinListener(new Pin.PinListener() {
			public void stateChanged(Pin pin) {
				Invalidate();
			}
			public void disposed(Pin pin) {}
			public boolean askState(Pin pin){return false;}
		});
	}
	
	public void initImg(){
		if(isDisposed) return;
		
		gpins.clear();
		
		width  = 80 + GPin.DEFAULT_PIN_SIZE*2;
		height = 80;
		
		zone = new Rectangle(GPin.DEFAULT_PIN_SIZE, 0, width-GPin.DEFAULT_PIN_SIZE*2, height);
		
		font = new Font("ARIAL", Font.BOLD, Math.min(128, height));
		font_name = new Font("ARIAL", Font.PLAIN, 30);
		
		BufferedImage img = new BufferedImage(width+4, height+4, BufferedImage.TYPE_INT_ARGB);
		Graphics2D gg = (Graphics2D)img.getGraphics();
		gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		for(int i=1; i<=n; i++)
			gpins.add(new GPin(this, new Rectangle(0, (height/(n2+1))*i -GPin.DEFAULT_PIN_SIZE/2, GPin.DEFAULT_PIN_SIZE, GPin.DEFAULT_PIN_SIZE), ce.pin_in.get(i-1)));
		for(int i=1; i<=n2; i++)
			gpins.add(new GPin(this, new Rectangle(width-GPin.DEFAULT_PIN_SIZE, (height/(n2+1))*i -GPin.DEFAULT_PIN_SIZE/2, GPin.DEFAULT_PIN_SIZE, GPin.DEFAULT_PIN_SIZE), ce.pin_out.get(i-1)));
		
		for(GPin gp : gpins)
			gg.drawImage(gp.getImage(), gp.x, gp.y, null);
		
		
		gg.setColor(new Color(75, 75, 75));
		gg.fillOval(GPin.DEFAULT_PIN_SIZE-1, 0, width-GPin.DEFAULT_PIN_SIZE*2+2, height);
		
		gg.setFont(font);
		gg.setColor(new Color(20, 20, 20));
		FontMetrics fm   = gg.getFontMetrics(font);
		Rectangle2D rect = fm.getStringBounds("0", gg);
		int textHeight = (int)(rect.getHeight()); 
		int textWidth  = (int)(rect.getWidth());
		int panelHeight= height;
		int panelWidth = width;

		x_str = (panelWidth  - textWidth)  / 2;
		y_str = (panelHeight - textHeight) / 2 + fm.getAscent();

		gg.drawString("0", x_str, y_str);
		
		gg.dispose();

		i_base = img;
	}
	
	public void repaint(Graphics2D g) {
		if(isDisposed) return;
		try{
			g.drawImage(i_base, x, y, null);

			if(gpins != null)
				for(GPin gp : gpins)
					gp.repaint(g);
			
		
			if(ce.pin_in.get(0).isOn()){
				g.setColor(Color.green);
				g.fillOval(x+GPin.DEFAULT_PIN_SIZE-2, y-1, width-GPin.DEFAULT_PIN_SIZE*2+4, height+2);
				g.setColor(new Color(40, 200, 40));
				g.setFont(font);
				g.drawString("1", x+x_str, y+y_str);
			}
			
			if(isHover()){
				g.setColor(new Color(255, 255, 255, 75));
				g.fillOval(x+GPin.DEFAULT_PIN_SIZE-2, y-1, width-GPin.DEFAULT_PIN_SIZE*2+4, height+2);
				g.setColor(Color.pink);
				g.setFont(font_name);
				if(update_name){
					update_name = false;
					FontMetrics fm   = g.getFontMetrics(font_name);
					Rectangle2D rect = fm.getStringBounds(ce.getName(), g);
					name_x = (width-(int)(rect.getWidth()))/2;
					name_y = height+(int)(rect.getHeight())-10;
				}
				g.drawString(ce.getName(), x+name_x, y+name_y);
			}
			
			if(isSelected()){
				g.setColor(new Color(255, 50, 50, 75));
				g.fillOval(x+GPin.DEFAULT_PIN_SIZE-2, y-1, width-GPin.DEFAULT_PIN_SIZE*2+4, height+2);
			}
		}catch(Exception e){}
	}
	
	public boolean hitTest(int x, int y){
		if(isDisposed) return false;
		return Math.sqrt((x-zone.width/2-GPin.DEFAULT_PIN_SIZE-1)*(x-zone.width/2-GPin.DEFAULT_PIN_SIZE-1) + (y-zone.height/2)*(y-zone.height/2)) <= zone.height/2;
	}
}
