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 InterfaceMd2 {
	
	Model md2 = new Model();
	
	Image top, bot, right, left;
	int begY = 10;
	
	// can play now
	public final int statePlay = 10;
	// can pause now
	public final int statePause = 11;
	
	public int state;
	
	Button bPlay = new Button("imgsmd2/Play.jpg","imgsmd2/PlayH.jpg"
	,"imgsmd2/Play.jpg",567,101+begY,
	"Start Animation");
	
	Button bPause = new Button("imgsmd2/Pause.jpg","imgsmd2/PauseH.jpg"
	,"imgsmd2/Pause.jpg",567,101+begY,
	"Pause Animation");
	
	Button bUp = new Button("imgsmd2/Up.jpg","imgsmd2/UpH.jpg"
	,"imgsmd2/Up.jpg",581,11+begY,
	"Rotate Model up");
	
	Button bDown = new Button("imgsmd2/Down.jpg","imgsmd2/DownH.jpg"
	,"imgsmd2/Down.jpg",581,225+begY,
	"Rotate Model down");
	
	Button bRight = new Button("imgsmd2/Right.jpg","imgsmd2/RightH.jpg"
	,"imgsmd2/Right.jpg",694,116+begY,
	"Rotate Model right");
	
	Button bLeft = new Button("imgsmd2/Left.jpg","imgsmd2/LeftH.jpg"
	,"imgsmd2/Left.jpg",475,116+begY,
	"Rotate Model left");
	
	Button slider1Left = new Button("imgsmd2/Sl1Left.jpg",
	"imgsmd2/Sl1LeftH.jpg","imgsmd2/Sl1Left.jpg",475,336+begY,
	"Change Model");
	
	Button slider1Right = new Button("imgsmd2/Sl1Right.jpg",
	"imgsmd2/Sl1RightH.jpg","imgsmd2/Sl1Right.jpg",728,336+begY,
	"Change Model");
	
	Button slider2Left = new Button("imgsmd2/Sl2Left.jpg",
	"imgsmd2/Sl2LeftH.jpg","imgsmd2/Sl2Left.jpg",475,393+begY,
	"Change action");
	
	Button slider2Right = new Button("imgsmd2/Sl2Right.jpg",
	"imgsmd2/Sl2RightH.jpg","imgsmd2/Sl2Right.jpg",728,394+begY-1,
	"Change action");
	
	String status = new String(" ");
	
	public InterfaceMd2() {
		top   = Toolkit.getDefaultToolkit().createImage("imgsmd2/ITop.jpg");
		bot   = Toolkit.getDefaultToolkit().createImage("imgsmd2/IBottom.jpg");
		right = Toolkit.getDefaultToolkit().createImage("imgsmd2/IRight.jpg");
		left  = Toolkit.getDefaultToolkit().createImage("imgsmd2/ILeft.jpg");
		
		state = statePlay;
		
		bPlay.initAll();
		bPause.initAll();
		md2.reset();
		try {
			
		}
		catch(Exception e) {}
		
	}
	
	public void delay(long ms) { 
		try {
			Thread.sleep(ms);
		} 
		catch(InterruptedException e) { } 
	}
	
	public void draw(Graphics g) {
		g.drawImage(top,48,begY,null);
		g.drawImage(left,0,begY,null);
		g.drawImage(right,453,begY,null);
		g.drawImage(bot,48,530+begY,null);
		
		if(state == statePlay)
			bPlay.drawNorm(g);
		else if(state == statePause)
			bPause.drawNorm(g);
		
		
		bUp.drawNorm(g);
		bDown.drawNorm(g);
		bLeft.drawNorm(g);
		bRight.drawNorm(g);

		slider1Left.drawNorm(g);
		slider1Right.drawNorm(g);
		slider2Left.drawNorm(g);
		slider2Right.drawNorm(g);	
		
		if(state == statePlay)
			md2.Draw(g,false);
		else
			md2.Draw(g,true);
			
		Font myFont = new Font("Arial",Font.PLAIN,25);
		Font oldFont = g.getFont();
		g.setFont(myFont);
		g.drawString(md2.auxFile.anims[md2.actionNumber].name,570,435);
		g.drawString(md2.modelNames.current(),570,382);
		g.setFont(oldFont);
		
		// draw status message
		g.drawString(status,510,487);
	}
	
	void btnMOHnd(Button btn, int mx,int my, Graphics g) {
		if(btn.isInRange(mx, my)) {
			btn.drawHigh(g);
			
			// Draw the status ...
			BufferedImage Im1 = new BufferedImage(108,20,BufferedImage.TYPE_INT_RGB);
			Graphics g1 = Im1.getGraphics();
			
			//g.setXORMode(new Color(255,255,255));
			//g1.drawString(status,510,487);
			//g1.setPaintMode();
			status = btn.getHelp();
			g1.setColor(new Color(218,235,249));
			g1.fillRect(0,0,108,9);
			g1.setColor(new Color(235,244,251));
			g1.fillRect(0,9,108,11);
			g1.setColor(new Color(0,0,0));
			g1.drawString(status,0,17);
			g.drawImage(Im1,510,470,null);
			// Repaint the satus
			//Md2.theFrame.repaint(498,470,650,488);
		}
		else {
			btn.drawNorm(g);
		}
	}
	
	
	public void handleMouseOver(MouseEvent me, Graphics g) {
	
		int mx = me.getX();
		int my = me.getY();
		

		if(state == statePlay)
			btnMOHnd(bPlay,mx,my,g);	
		else if(state == statePause)
			btnMOHnd(bPause,mx,my,g);	

		
		// if(state == statePause) {
			btnMOHnd(slider1Left,mx,my,g);
			btnMOHnd(slider1Right,mx,my,g);
		// }
		
		btnMOHnd(slider2Left,mx,my,g);
		btnMOHnd(slider2Right,mx,my,g);
		
		btnMOHnd(bUp,mx,my,g);
		btnMOHnd(bDown,mx,my,g);
		btnMOHnd(bRight,mx,my,g);
		btnMOHnd(bLeft,mx,my,g);
		
	}
	

	public void handleMouseClick(MouseEvent me, Graphics g) {
		
		// When in paused state...
		if(state == statePlay) {
			// If any button is pressed repaint the model,
			// but dont animate it
			md2.repaintModel(false);
		}

		if(bPlay.isInRange(me.getX(),me.getY())) {
			if(state == statePlay) {
				state = statePause;
				bPause.drawNorm(g);
			}
			
			else if(state == statePause) {
				state = statePlay;
				bPlay.drawNorm(g);
			}
		}
	
		if(bUp.isInRange(me.getX(),me.getY())) {
			// Do Whatever
			md2.setAngleY(-1);
			// System.out.println("Y Up");
		}
		
		if(bDown.isInRange(me.getX(),me.getY())) {
			// Do Whatever
			md2.setAngleY(1);
			// System.out.println("Y Down");
		}
		
		if(bRight.isInRange(me.getX(),me.getY())) {
			// Do Whatever
			md2.setAngleZ(1);
			// System.out.println("Z Right");
		}
		
		if(bLeft.isInRange(me.getX(),me.getY())) {
			// Do Whatever
			md2.setAngleZ(-1);
			// System.out.println("Z Left");
		}
		
		if(slider2Left.isInRange(me.getX(),me.getY())) {
			if( md2.actionNumber > 0 ) {
				md2.actionNumber --;
			}
			else {
				md2.actionNumber = md2.actionMax - 1;
			}
			
			md2.startFrame = md2.auxFile.anims[md2.actionNumber].animBeginFrame;
			md2.endFrame = md2.auxFile.anims[md2.actionNumber].animEndFrame;
			
			md2.frame1 = md2.startFrame;
			md2.frame2 = md2.startFrame + 1;
			if(md2.frame2 > md2.endFrame)
			{
				md2.frame2 = md2.frame1;
			}
			Md2.theFrame.repaint(536,398,726,436);
		}
		
		if(slider2Right.isInRange(me.getX(),me.getY())) {
			md2.actionNumber ++;
			md2.actionNumber %= md2.actionMax;
			
			md2.startFrame = md2.auxFile.anims[md2.actionNumber].animBeginFrame;
			md2.endFrame = md2.auxFile.anims[md2.actionNumber].animEndFrame;
			
			md2.frame1 = md2.startFrame;
			md2.frame2 = md2.startFrame + 1;
			
			Md2.theFrame.repaint(536,398,726,436);
		}
		
		if(slider1Left.isInRange(me.getX(),me.getY())) {
			// if(state == statePause) {
				md2.modelNames.nextModel();
				md2.md2	= new Md2File(md2.modelNames.currentJmdl());
				md2.auxFile = new JMd2Aux(md2.modelNames.currentJaux());
				md2.reset();
				
				Md2.theFrame.repaint();
			// }
		}
		
		if(slider1Right.isInRange(me.getX(),me.getY())) {
			// if(state == statePause) {
				md2.modelNames.prevModel();
				md2.md2	= new Md2File(md2.modelNames.currentJmdl());
				md2.auxFile = new JMd2Aux(md2.modelNames.currentJaux());
				md2.reset();
				
				Md2.theFrame.repaint();				
			// }
		}
		
		int x = me.getX();
		int y = me.getY();
		if(x > 574 && y > 511 && x < 679 && y < 550) {
			// If there isnt another frame, this doesnt mean anything
			if(! Md2.md2Frame.singleFrameMe ) {
				// Back button
				Md2.md2Frame.stop();
				Md2.md2Frame.hide();
			
				Md2.morphFrame.setVisible(true);
			}
			
		}
	}
	
	public void setState(int _state) {
		if(_state == statePlay || _state == statePause) 
			state = _state;
	}
}
