import java.awt.*;
import java.applet.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.*;

public class Interface {
	Image top, bot, right, left;
	int begY = 10;
	
	FunctionHandler fnhd = new FunctionHandler();
	
	public int slider1Value = 50, slider2Value = 50;
	
	Button play = new Button("imgs/PlayC.jpg","imgs/PlayH.jpg"
	,"imgs/PlayN.jpg",619,67+begY-2,
	"Start Morphing");
	
	Button pause = new Button("imgs/PauseC.jpg","imgs/PauseH.jpg"
	,"imgs/PauseN.jpg",546,79+begY-2,
	"Pause Morphing");
	
	Button slider1Left = new Button("imgs/Slider1LeftC.jpg",
	"imgs/Slider1LeftH.jpg","imgs/Slider1LeftN.jpg",251,93+begY-1,
	"Decrease Delay between steps");
	
	Button slider1Right = new Button("imgs/Slider1RightC.jpg",
	"imgs/Slider1RightH.jpg","imgs/Slider1RightN.jpg",328,93+begY-1,
	"Increase Delay between steps");
	
	Button slider2Left = new Button("imgs/Slider2LeftC.jpg",
	"imgs/Slider2LeftH.jpg","imgs/Slider2LeftN.jpg",398,93+begY-1,
	"Decrease number of time steps");
	
	Button slider2Right = new Button("imgs/Slider2RightC.jpg",
	"imgs/Slider2RightH.jpg","imgs/Slider2RightN.jpg",471,93+begY-1,
	"Increase number of time steps");
	
	// Not exaclty a button ;)
	Button slider1 = new Button("imgs/Slider1.jpg",
	"imgs/Slider1.jpg","imgs/Slider1.jpg",473,93+begY-1,
	"a");
	
	Button slider2 = new Button("imgs/Slider2.jpg",
	"imgs/Slider2.jpg","imgs/Slider2.jpg",473,93+begY-1,"a");
	
	//Button btnLogo = new Button("imgs/PauseC.jpg","imgs/PauseH.jpg"
	//,"imgs/PauseN.jpg",0,begY,
	//"Pause Morphing");
	
	Button btnLogo = new Button("imgs/LogoH.jpg",
	"imgs/LogoH.jpg","imgs/LogoN.jpg",0,begY," View Model Animation Demo ");
	
	public Interface() {
		top   = Toolkit.getDefaultToolkit().createImage("imgs/Top.jpg");
		bot   = Toolkit.getDefaultToolkit().createImage("imgs/Bottom.jpg");
		right = Toolkit.getDefaultToolkit().createImage("imgs/Right.jpg");
		left  = Toolkit.getDefaultToolkit().createImage("imgs/Left.jpg");
	}
	
	public void delay(long ms) { 
		try {
			Thread.sleep(ms);
		} 
		catch(InterruptedException e) { } 
	}
	
	public void draw(Graphics g) {
		int x, y;
		x = 0;
		y = begY;
		g.drawImage(top,x,y,null);
		y += top.getHeight(null);
		g.drawImage(left,x,y,null);
		x = 679;
		g.drawImage(right,x,y,null);
		x = 0;
		y += left.getHeight(null);
		g.drawImage(bot,x,y,null);
		
		btnLogo.drawNorm(g);
		btnLogo.drawHigh(g);
		if(play.getState() == Button.norm || play.getState() == Button.hilite ) {
			play.drawNorm(g);
		}
		else {
			play.drawClicked(g);
		}
		if(pause.getState() == Button.norm || pause.getState() == Button.hilite ) {
			pause.drawNorm(g);
		}
		else {
			pause.drawClicked(g);
		}
		slider1Left.drawNorm(g);
		slider1Right.drawNorm(g);
		slider2Left.drawNorm(g);
		slider2Right.drawNorm(g);
	
		drawSliderValues(g);	
	
		fnhd.Draw(g);	
		
	}
	
	void drawSliderValues(Graphics g) {
		Point stPt = slider1Left.getPos();
		Point pWdHt = slider1Left.getWdHt();
		
		slider1.setPos(stPt.x + pWdHt.x - 2, stPt.y );
		slider1.drawNorm(g);
		
		float percent1 = (float)(slider1Value-sliderMin)/(float)(sliderMax-sliderMin) ;
		float percent2 = (float)(slider2Value-sliderMin)/(float)(sliderMax-sliderMin) ;
		
		// System.out.println("Percent1" + percent1);
		// System.out.println("Percent2" + percent2);
		
		float r1 = 100 + (percent1 ) * 155;
		float g1 = 100 + (1.0F - percent1) * 155 ;
		
		float r2 = 100 + percent2 * 125;
		float g2 = 100 + (1.0F - percent2) * 155;
		
		Color c1 = new Color ((int)r1,(int)g1,255);
		Color c2 = new Color ((int)r2,(int)g2,255);
			
		
		Point stPt2 = slider2Left.getPos();
		Point pWdHt2 = slider2Left.getWdHt();
		
		slider2.setPos(stPt2.x + pWdHt2.x - 2, stPt2.y );
		slider2.drawNorm(g);
		
		Color back = g.getColor();
		// g.setColor(c1);
		g.drawString(new Integer(slider1Value).toString(),306,126);
		// g.setColor(c2);
		g.drawString(new Integer(slider2Value).toString(),450,126);
		// g.setColor(back);
	}
	
	int pMx =  -1, pMy = -1;
	String pHelp;
	int helpX = 260,helpY = 550;
	
	void btnMOHnd(Button btn, int mx,int my, Graphics g) {
		if(btn.isInRange(mx, my)) {
			if(btn.getState() != Button.clicked) {
				btn.drawHigh(g);
			}
			
			if(pMx != -1) {
				g.setXORMode(new Color(255,255,255));
				// g.drawString(pHelp,pMx+30,pMy+30);
				g.drawString(pHelp,helpX,helpY);
				g.setPaintMode();
				pMx = -1;
				pMy = -1;
			}

			if( mx != pMx || my != pMy ) {
				g.setXORMode(new Color(255,255,255));
				// NOT g.drawString(btn.getHelp(),pMx+30,pMy+30);
				// g.drawString(btn.getHelp(),mx+30,my+30);
				if(pHelp != null) {
					if(!pHelp.equals(btn.getHelp()) && pMx != -1) {
						g.drawString(pHelp,helpX,helpY);
					}
				}
				pHelp = btn.getHelp();
				g.drawString(pHelp,helpX,helpY);
				
				g.setPaintMode();
				pHelp = btn.getHelp();
			
				// System.out.println(btn.getHelp());
			
				pMx = mx;
				pMy = my;
			}
		}
		else {
			if(btn.getState() == Button.hilite) {
				if(btn.getPrevState() == Button.norm) {
					btn.drawNorm(g);		
				}
				else 
				{
					btn.drawClicked(g);
				}		
			}
		}
	}
	
	
	static boolean cursorHand = false;
	public void handleMouseOver(MouseEvent me, Graphics g) {
		int mx = me.getX();
		int my = me.getY();
		
		if(pMx != -1) {
			g.setXORMode(new Color(255,255,255));
			// g.drawString(pHelp,pMx+30,pMy+30);
			g.drawString(pHelp,helpX,helpY);
			g.setPaintMode();
			pMx = -1;
			pMy = -1;
		}

		if(play.getState() != Button.clicked) {
			if(fnhd.polyReady()) {	
				btnMOHnd(play,mx,my,g);	
			}
		}
		else {
			play.drawClicked(g);
		}
		if(pause.getState() != Button.clicked) {
			if(fnhd.polyReady()) {
				btnMOHnd(pause,mx,my,g);
			}
		}
		else {
			pause.drawClicked(g);
		}
		
		
		btnMOHnd(slider1Left,mx,my,g);
		btnMOHnd(slider1Right,mx,my,g);
		
		if(fnhd.report() == 0) {
			btnMOHnd(slider2Left,mx,my,g);
			btnMOHnd(slider2Right,mx,my,g);
		}
		
		if(btnLogo.isInRange(mx,my)) {
			if(!cursorHand) {
				Cursor hand = new Cursor(Cursor.HAND_CURSOR);
			
				Morph.morph.setCursor(hand);
				cursorHand = true;
			}
		}
		else {
			if(cursorHand) {
				Cursor def = new Cursor(Cursor.DEFAULT_CURSOR);
				Morph.morph.setCursor(def);
				cursorHand = false;
			}
		}
		btnMOHnd(btnLogo,mx,my,g);
		
		if(play.getState() == Button.clicked && fnhd.report() == 0) {
			play.drawNorm(g);
		}
			
	}
	
	void btnClkHnd(Button btn, int mx, int my, Graphics g, Button release) {

		if(btn.isInRange(mx, my)) {
			btn.drawClicked(g);
			// System.out.println("clicked");
			if(release != null) {
				if(release.getState() == Button.clicked) {
					release.drawNorm(g);
					// System.out.println("other relsease done");
				}
			}
		}
	}
	
	int sliderMax = 99, sliderMin = 1;
	void decSlider(int sliderNo) {
		if(sliderNo == 1) {
			if(slider1Value > sliderMin)
				slider1Value --;
		}
		else {
			if(slider2Value > sliderMin)
				slider2Value --;
		}
	}
	
	void incSlider(int sliderNo) {
		if(sliderNo == 1) {
			if(slider1Value < sliderMax)
				slider1Value ++;
		}
		else {
			if(slider2Value < sliderMax)
				slider2Value ++;
		}
	}
	
	void sliderHnd(Button slider, int mx, int my, Graphics g,
	int SliderNo, int leftRight)
	{
		if(slider.isInRange(mx, my)) {
			if(leftRight == 0) {
				decSlider(SliderNo);
				drawSliderValues(g);
				
			}
			else {
				incSlider(SliderNo);
				drawSliderValues(g);
			}
		}
	}
	
	public void handleMouseClick(MouseEvent me, Graphics g) {
		int mx = me.getX();
		int my = me.getY();

		if(fnhd.polyReady()) {
			// System.out.println("Play");
			btnClkHnd(play,mx,my,g,pause);
			btnClkHnd(pause,mx,my,g,play);
		}
		
		if(me.getModifiers() == me.BUTTON1_MASK)
		{
			fnhd.handleMouseClick(mx,my,g);	
		}
		else
		if(me.getModifiers() == me.BUTTON3_MASK)
		{
			fnhd.handleRightClick(g);
		}
		
		if(play.getState() == Button.clicked)
		{
			// System.out.println("play getstate is clicked");
			if(fnhd.report() == 0) {
				// System.out.println("now starting thread in fnhd");
				fnhd.setup(slider1Value,slider2Value,g);
				fnhd.start();
				// play.state = Button.clicked;
				play.drawClicked(g);
			}
			else if(fnhd.report() == 1) {
				// System.out.println("Unpausing");
				fnhd.restart();
				// play.state = Button.clicked;
				play.drawClicked(g);
				pause.drawNorm(g);
			}
		}
		
		if(pause.getState() == Button.clicked) {
			if(pause.isInRange(mx,my)) {
			
				// System.out.println("Getstate() for pause is clicked");
				if(fnhd.report() == 2) {
					// System.out.println("running fndh being paused");
					fnhd.pause();
					play.drawNorm(g);
					pause.drawClicked(g);
				}
				else if(fnhd.report() == 1) {
					// System.out.println("Unpausing");
					fnhd.restart();
					// play.state = Button.clicked;
					play.drawClicked(g);
					pause.drawNorm(g);
				}
				else {
					pause.drawNorm(g);
				}
			}
		}
		
		if(btnLogo.isInRange(mx,my)) {
			Morph.morph.hide();
			Md2.md2Frame.init();
		}
		
		sliderHnd(slider1Left,mx,my,g,1,0);
		sliderHnd(slider1Right,mx,my,g,1,1);
			
		if(fnhd.report() == 0) {
			sliderHnd(slider2Left,mx,my,g,2,0);
			sliderHnd(slider2Right,mx,my,g,2,1);
		}
	}
	
	public void setNorm(Graphics g) {
		play.drawNorm(g);
		pause.drawNorm(g);
	}
}
