import java.awt.*;
import java.applet.*;
import java.util.*;

public class UI extends Canvas {

   long timer;
   long framedelay;
   int playing;
   Picture icon;
   int depth;
   int cursor = -3;
   boolean paused=false;
   Image offscreenimg;
   Image offscreen2img;
   int pauseiteration;
   Graphics offscreen;
   Graphics offscreen2;
   
   
   JBill2 game;

   public UI(JBill2 a) {
      game=a;
      playing=0;
      timer=0;
      icon = new Picture(a);
   }


   /**************************/
   /* Timer control routines */
   /**************************/

   void restart_timer() {
      timer = new Date().getTime();
      framedelay = 250;/* 250 ms */
   }

   void kill_timer() {
      timer = -1;
   }

   void idle_timer() {
      try {
         long time = new Date().getTime();
         pauseiteration = 0;
         do {
            Thread.currentThread().sleep(framedelay-(time-timer));
            if ((paused)&&(pauseiteration%2==1))
                repaint();
            pauseiteration++;
         } while (paused);
         if (pauseiteration%4>1)
           repaint();
      }
      catch (Exception e) {
      }
      if (timer>=0)
        timer = new Date().getTime();
   }

   /*******************/
   /* Window routines */
   /*******************/

   void initialize(String[] argv) {
   }

   void make_mainwin() {
   }

   void make_windows() {
   }

   void popup_dialog (int dialog) {
   }

   /*********************/
   /* Graphics routines */
   /*********************/

   void set_cursor(int curs) {
      switch (curs) {
       case game.BUCKETC:
         cursor = -1;
         break;
       case game.DOWNC:
         cursor = -2;
         break;
       case game.DEFAULTC:
         cursor = -3;
         break;
       default:
         cursor = curs;
      }
   }

   void load_cursors() {
   }

   void graph_init() {
      offscreenimg = createImage(400,400);
      offscreen2img = createImage(400,400);
      offscreen = offscreenimg.getGraphics();
      offscreen2 = offscreen2img.getGraphics();
   }

   void clear() {
      offscreen.setColor(Color.black);
      offscreen.fillRect(0,0,400,400);
   }

   public void update(Graphics g) {
      offscreen2.drawImage(offscreenimg,0,0,this);
      if (cursor!=-3) {
         if (cursor==-1)
           game.bucket.draw(offscreen2,mousex-(game.bucket.width/2),mousey-(game.bucket.height/2));
         if (cursor>=0)
           game.OS.os[cursor].draw(offscreen2,mousex-(game.OS.os[0].width/2),mousey-(game.OS.os[0].height/2));
      }
      if ((game.state!=-1)&&(paused)&&(pauseiteration%4<2)) {
         offscreen2.setFont(new Font("Courier",Font.BOLD,32));
         FontMetrics fm =getFontMetrics(offscreen2.getFont());
         int width = fm.stringWidth("P A U S E D");
         int height = fm.getHeight();
         int offset = fm.getMaxAscent();
         offscreen2.setColor(Color.red);
         offscreen2.drawString("P A U S E D",(game.scrwidth-width)/2,(game.scrheight-height)/2+offset);
      }
      g.drawImage(offscreen2img,0,0,this);
   }

   public void paint(Graphics g) {
      update(g);
   }

   void draw (Picture pict, int x, int y) {
      pict.draw(offscreen,x,y);
   }

   void draw_centered (Picture pict) {
      pict.draw_centered(offscreen);
   }

   void draw_line(int x1, int y1, int x2, int y2) {
      offscreen.setColor(Color.white);
      offscreen.drawLine(x1,y1,x2,y2);
   }

   void draw_str(String str, int x, int y) {
      offscreen.setColor(Color.white);
      offscreen.drawString(str,x,y);
   }

   void draw_str_centered(String s,Color c,int y) {
      FontMetrics fm =getFontMetrics(offscreen.getFont());
      int width = fm.stringWidth(s);
      int height = fm.getHeight();
      int offset = fm.getMaxAscent();
      offscreen.setColor(c);
      offscreen.drawString(s,(game.scrwidth-width)/2,y-(height-10)/2+offset);
   }

   /******************/
   /* Mouse routines */
   /******************/

   void set_pausebutton (boolean action) {
   }

   int mousex;
   int mousey;

   public boolean mouseDrag(Event e,int x,int y) {
      mousex=e.x;
      mousey=e.y;
      if (cursor!=-3)
        repaint();
      return false;
   }

   public boolean mouseMove(Event e, int x, int y) {
      mousex=x;
      mousey=y;
      if (cursor!=-3)
        repaint();
      return false;
   }

   int mdx;
   int mdy;

   public boolean mouseEnter(Event e, int x, int y) {
      mousex=x;
      mousey=y;
      if (cursor!=-3)
        repaint();
      paused=false;
      return false;
   }
   public boolean mouseExit(Event e, int x, int y) {
      mousex=x;
      mousey=y;
      if (cursor!=-3)
        repaint();
      if (game.state!=game.WAITING)
        paused=true;
      return false;
   }
   public boolean mouseUp(Event e, int x, int y) {
      game.button_release(x,y);
      mousex=x;
      mousey=y;
      if (cursor!=-3) {
         cursor=-3;
         repaint();
      }
      return false;
   }
   public boolean mouseDown(Event e, int x, int y) {
      game.button_press(x,y);
      mousex=x;
      mousey=y;
      if (cursor!=-3)
        repaint();
      return false;
   }

   void drawTextCentered(String s,Color c) {
      FontMetrics fm =getFontMetrics(offscreen.getFont());
      int width = fm.stringWidth(s);
      int height = fm.getHeight();
      int offset = fm.getMaxAscent();
      offscreen.setColor(Color.gray);
      offscreen.fillRect(0,(game.scrheight-height-10)/2,game.scrwidth,height);
      offscreen.setColor(c);
      offscreen.drawString(s,(game.scrwidth-width)/2,(game.scrheight-height-10)/2+offset);
   }



}
