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.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

// Representation graphique d'une entree, herite de GCircuitElement
public class GIn extends GCircuitElement {

	public GIn(HWCanvas canvas, final Entree ce) {
		super(canvas, ce);	
		ce.pin_out.get(0).addPinListener(new Pin.PinListener() {
			public void stateChanged(Pin pin) {
				Invalidate();
			}
			public void disposed(Pin pin){}
			public boolean askState(Pin pin){return on;}
		});
	}
	
	public void initImg(){
		if(isDisposed) return;
		
		gpins.clear();
		
		width  = 80 + GPin.DEFAULT_PIN_SIZE;
		height = 80;
		
		zone = new Rectangle(0, 0, width-GPin.DEFAULT_PIN_SIZE, height);
		
		font = new Font("ARIAL", Font.BOLD, 92);
		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);
		
		gg.setColor(new Color(220, 220, 220));
		gg.fillOval(0, 0, width-GPin.DEFAULT_PIN_SIZE+2, height);
		
		gg.setFont(font);
		gg.setColor(new Color(200, 200, 200));
		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-GPin.DEFAULT_PIN_SIZE;

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

		gg.drawString("0", x_str, y_str);
		
		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.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(on){
				g.setColor(Color.green);
				g.fillOval(x-1, y-1, width-GPin.DEFAULT_PIN_SIZE+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, y, width-GPin.DEFAULT_PIN_SIZE+2, height);
			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, y, width-GPin.DEFAULT_PIN_SIZE+2, height);
		}
		}catch(Exception ex){}
	}
	
	boolean on;
	public boolean mouseClicked(MouseEvent me){
		if(isDisposed) return false;
		if(hitTest(me.getPoint().x-this.x, me.getPoint().y-this.y)){
			switchOnOff();
			return true;
		}
		return false;
	}
	
	public boolean hitTest(int x, int y){
		if(isDisposed) return false;
		return Math.sqrt((x-zone.width/2)*(x-zone.width/2) + (y-zone.height/2)*(y-zone.height/2)) <= zone.height/2;
	}
	
	public void switchOnOff(){
		ce.pin_out.get(0).setState(on=!on);
	}

}
