import java.awt.*;
import java.awt.image.*;
import gie.*;

public class Graph1001b extends Frame{
  protected Image image;
  protected Image bgImage;
  protected int bg_width = 0;
  protected int bg_height = 0;

  // pioche
  static Image bgCarte;
  // whoPlay
  static Image whoPlayImg[]=new Image[4];
  static Image imgCarte[]=new Image[20];

  Jeu1001b jeu=new Jeu1001b(this);

  public  Graph1001b(){
    image = Toolkit.getDefaultToolkit().getImage("graphics/bk1001.gif");
    // whoPlay
    whoPlayImg[0]=Toolkit.getDefaultToolkit().getImage("graphics/north.gif");
    whoPlayImg[1]=Toolkit.getDefaultToolkit().getImage("graphics/east.gif");
    whoPlayImg[2]=Toolkit.getDefaultToolkit().getImage("graphics/south.gif");
    whoPlayImg[3]=Toolkit.getDefaultToolkit().getImage("graphics/west.gif");
    // pioche
    bgCarte=Toolkit.getDefaultToolkit().getImage("graphics/bgcarte.gif");
    // les cartes
    imgCarte[0]=Toolkit.getDefaultToolkit().getImage("graphics/carte.gif");
    for(int c=1; c<20;c++)
      imgCarte[c]=imgCarte[0];
    imgCarte[v200]=Toolkit.getDefaultToolkit().getImage("graphics/cartes/v200.gif");
    imgCarte[v100]=Toolkit.getDefaultToolkit().getImage("graphics/cartes/v100.gif");
    imgCarte[v75]=Toolkit.getDefaultToolkit().getImage("graphics/cartes/v75.gif");
    imgCarte[v50]=Toolkit.getDefaultToolkit().getImage("graphics/cartes/v50.gif");
    imgCarte[v25]=Toolkit.getDefaultToolkit().getImage("graphics/cartes/v25.gif");
  }

  public boolean imageUpdate(Image img, int infoflags,
                              int x, int y, int width, int height){
      if ((infoflags & ImageObserver.ALLBITS) != 0){
        // image is complete     
         bg_width = image.getWidth(null);
         bg_height = image.getHeight(null);
      //resize(579, 364);
      //resize(bg_width, bg_height);
         repaint();
         return false;      
      }         
      return true; // want more info
   }

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

  // fond de carte nord
  static int bgCNx=260, bgCNy=144;
  static int bgCNtx[]={bgCNx+6,bgCNx+41,bgCNx+47,bgCNx+0};
  static int bgCNty[]={bgCNy+0,bgCNy+0,bgCNy+38,bgCNy+38};
  static Polygon bgCNorth=new Polygon(bgCNtx,bgCNty,4);
  // fond de carte est
  static int bgCEx=329, bgCEy=188;
  static int bgCEtx[]={bgCEx+0,bgCEx+73,bgCEx+100,bgCEx+10};
  static int bgCEty[]={bgCEy+0,bgCEy+0,bgCEy+27,bgCEy+27};
  static Polygon bgCEast=new Polygon(bgCEtx,bgCEty,4);
  // fond de carte ouest
  static int bgCWx=139, bgCWy=188;
  static int bgCWtx[]={bgCWx+26,bgCWx+96,bgCWx+87,bgCWx+0};
  static int bgCWty[]={bgCWy+0,bgCWy+0,bgCWy+27,bgCWy+27};
  static Polygon bgCWest=new Polygon(bgCWtx,bgCWty,4);

  void piochePaint(Graphics g){
    g.drawImage(bgCarte, 290, 254, this);
    if(tableDerniereCarteJetee!=0){
      g.drawRoundRect(250,254,30,43,5,5);
    }
    
  }
  void whoPlayPaint(Graphics g){
    g.drawImage(whoPlayImg[0], 265, 188, this);
  }
  void jNorthPaint(Graphics g){
    g.fillPolygon(bgCNorth);
  }
  void jEastPaint(Graphics g){
    g.fillPolygon(bgCEast);
  }
  void jSouthPaint(Graphics g){
    for (int i = 0; i < nsquares; i++)
      draw(g, i);
    for (int i = 0; i < 7; i++){ // les cartes de la main
      g.drawImage(imgCarte[jMain[i]], 12+i*80, 310, this);
    }
    for (int c = 0; c < tableV200[SOUTH]; c++){ // carte v200
      g.drawImage(imgCarte[v200], 12, 410+8*c, this);
    }
    for (int c = 0; c < tableV100[SOUTH]; c++){ // carte v100
      g.drawImage(imgCarte[v100], 12+80, 410+8*c, this);
    }
    for (int c = 0; c < tableV75[SOUTH]; c++){ // carte v75
      g.drawImage(imgCarte[v75], 12+160, 410+8*c, this);
    }
    for (int c = 0; c < tableV50[SOUTH]; c++){ // carte v50
      g.drawImage(imgCarte[v50], 12+240, 410+8*c, this);
    }
    for (int c = 0; c < tableV25[SOUTH]; c++){ // carte v25
      g.drawImage(imgCarte[v25], 12+320, 410+8*c, this);
    }

    g.drawImage(imgCarte[tableRoute[SOUTH]], 12+400, 410+18, this);// carte route
    

  }
  void jWestPaint(Graphics g){
    g.fillPolygon(bgCWest);
  }

   public void paint(Graphics g){ 

     if (bg_width <= 0 || bg_height <= 0){
      bgImage =createImage(1,1);
      Graphics gbg=bgImage.getGraphics();
      gbg.drawImage(image, 0, 0, this);
      gbg.dispose();
      // ceci permet d'attendre que l'image soit chargee
      // avant de l'afficher (avec utilisation de imageUpdate )
      g.drawString("Gilles et laurent présentent", 100,100);
      g.drawString(" Les milles et une bornes...", 100,125);
      return;
     }
     // bgImage =createImage(bg_width, bg_height);
      bgImage =createImage(579,564);
      Graphics gbg=bgImage.getGraphics();
      gbg.drawImage(image, 0, 0, this);

      //g.drawImage(image, 0, 0, this);
      whoPlayPaint(gbg); // indique celui qui joue
      jWestPaint(gbg);  // la carte ouest
      jNorthPaint(gbg); // la carte nord
      jEastPaint(gbg);  // la carte est

      piochePaint(gbg); // les (la) carte(s) de la pioche
      
      jSouthPaint(gbg); // la carte est

      gbg.dispose();
      g.drawImage(bgImage, 0, 0, this);
   }
   
   public int find(int x, int y){
     for (int i = 0; i < nsquares; i++)
         if (squares[i].x <= x && x <= squares[i].x 
            + 30 && squares[i].y <= y 
            && y <= squares[i].y + 43)
            return i;
      return -1;
   }

   public void draw(Graphics g, int i){
     g.drawRoundRect(squares[i].x, squares[i].y,30,43,5,5);
   }

   public void add(int x, int y)
   {  if (nsquares < MAXNSQUARES)
      {  squares[nsquares] = new Point(x, y);
         nsquares++;
         repaint();
      }
   }

   public void remove(int n)
   {  nsquares--;
      squares[n] = squares[nsquares];
      if (current == n) current = -1;
      repaint();
   }

  MenuBar mm=null;
   public boolean mouseDown(Event evt, int x, int y) {
     if(menuBox.inside(x,y)&& (evt.clickCount >= 2)){//show/hide menu
       if(mm!=null){
	 mm=getMenuBar();setMenuBar(null); 
       }else setMenuBar(mm);
     }
     current = find(x, y);
      if (current < 0) // not inside a square
      {  add(x, y);
      }
      else if (evt.clickCount >= 2){
        remove(current);
      }//else{ // on vas faire un drag
       // remove(current);
      
      //}
      return true;
   }

   public boolean mouseDrag(Event evt, int x, int y) 
   {  if (current >= 0){
        //Graphics g = getGraphics();
        // g.setXORMode(getBackground());
        // draw(g, current);      
        squares[current].x = x;
        squares[current].y = y;
        // draw(g, current);      
        // g.dispose();
        repaint();
      }
      return true;       
   }
  
   static boolean showPl=false;
  Rectangle piocheDCJBox=new Rectangle(250,254,30,43);
  Rectangle menuBox=new Rectangle(120,250,40,30);

   public boolean mouseMove(Event evt, int x, int y) {
     if(piocheDCJBox.inside(x,y) && !showPl){
       getGraphics().drawImage(imgCarte[tableDerniereCarteJetee], 190, 234, this);
       showPl=true;
     }
     if(showPl && !piocheDCJBox.inside(x,y))  {
       showPl=false;
       repaint();
     }
     if (find(x, y) >= 0) 
         setCursor(Frame.HAND_CURSOR); 
      else 
         setCursor(Frame.DEFAULT_CURSOR);
      return true;
   }



   
   protected static final int SQUARELENGTH = 10;

   protected static final int MAXNSQUARES = 100;
   protected Point[] squares = new Point[MAXNSQUARES];
   protected int nsquares = 0;
   protected int current = -1;
  //=================================================================================

  //
  static int NORTH=0,EAST=1,SOUTH=2,WEST=3;
  int jActif=SOUTH;
  boolean jPresent[]={false,false,true,false};
  String jPseudo[]={"Nord", "Est", "Sud", "Ouest"};
  int jScore[]={0,0,0,0};

  // les cartes du jeu
  static int v200=3, v100=5, v75=6, v50=7, v25=9;
  static int roulez=10, stop=11, vLimite=12, finVLimite=13, essence=14, panneEssence=15;
  static int roueDeSec=16, creve=17, accident=18, reparation=19;
  static int prioritaire=8, increvable=4, asDuVolant=2, citerne =1;

  // le jeu sur la table
  //  int jMain[]=new int[7];          // les cartes de la main du joueur
  int jMain[]={roulez,stop,essence,accident,increvable, v100, v75};
  //int tableV200[]=new int[4];      // nb de cartes V200 qu'a etale chaque joueur
  int tableV200[]={3,2,4,1};
  //int tableV100[]=new int[4];      // nb de cartes V100 qu'a etale chaque joueur
  int tableV100[]={1,2,2,1};
  //int tableV75[]=new int[4];       // nb de cartes V75 qu'a etale chaque joueur
  int tableV75[]={1,2,0,1};
  //int tableV50[]=new int[4];       // nb de cartes V50 qu'a etale chaque joueur
  int tableV50[]={1,2,1,1};
  //int tableV25[]=new int[4];       // nb de cartes V25 qu'a etale chaque joueur
  int tableV25[]={1,2,3,1};
  //int tableRoute[]=new int[4];     // la carte sur la route de chaque joueur
  int tableRoute[]={stop,vLimite,roulez,panneEssence};
  //int tableAtouts[]=new int[4];    // les atouts de chaque joueur
  int tableAtouts[]={0,increvable,prioritaire+citerne,asDuVolant};
  int tableDerniereCarteJetee=creve;           // la derniere carte jetee
}
