import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.URL;

class Conc extends Frame
    implements ActionListener, WindowListener, MouseListener
{

   ConcCanvas myConcCanvas;
   QuizPair myQuiz;
   Point mySize;
   Point last;
   URL myURL;

   Label myLPlay1;
   Label myLPlay2;
   Label myTurnLabel;
   int myTurnCount;
   int myIPlay1;
   int myIPlay2;
   int myX=6;
   int myY=5;
   boolean myP1Turn;
  
  public Conc(URL url) {
    super("Concentration");
    addWindowListener(this);
    myURL=url;

    Panel p=new Panel();
    p.setLayout(new GridLayout(1,3));
    add(p,"North");

    myLPlay1=new Label();
    p.add(myLPlay1);

    myTurnLabel=new Label();
    p.add(myTurnLabel);

    myLPlay2=new Label();
    p.add(myLPlay2);

    myConcCanvas=new ConcCanvas();
    reset();
    add(myConcCanvas,"Center");
    myConcCanvas.addMouseListener(this);

    MenuBar mb=new MenuBar();
    setMenuBar(mb);
    Menu m=new Menu("File");
    mb.add(m);

    MenuItem mi=new MenuItem("New");
    m.add(mi);
    mi.addActionListener(this);

    mi=new MenuItem("Exit");
    m.add(mi);
    mi.addActionListener(this);

    setSize(300,250);
    show();
  }

  private void reset() {
    mySize=new Point(myX,myY);
    myQuiz=new QuizPair(mySize,myURL);
    myConcCanvas.setQuiz(myQuiz,mySize);

    myIPlay1=0;
    myIPlay2=0;
    myTurnCount=1;
    myP1Turn=true;
    last=null;
    setTurnText();
    myConcCanvas.repaint();
  }

   private void setTurnText() {
      String s1="Player 1: "+myIPlay1;
      String s2="Player 2: "+myIPlay2;
    
      if(myP1Turn) {
         if(last==null) {
            myConcCanvas.setBackground(Color.WHITE);
            s1=s1+"<<";
         }
         else
            s1=s1+"<";
      }
      else {
         if(last==null) {
            myConcCanvas.setBackground(Color.PINK);
            s2=s2+"<<";
         }
         else
            s2=s2+"<";
      }

      if(!myQuiz.isDone())
         myTurnLabel.setText("Turn: "+Integer.toString(myTurnCount));
         
      myLPlay1.setText(s1);
      myLPlay2.setText(s2);
  }

  public void windowActivated(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowOpened(WindowEvent e) {}

  public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
  }

  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}

  public void mouseClicked(MouseEvent e) {
    if(myQuiz.isDone()) {
      return;
    }

    Point mp=e.getPoint();
    Point lp=myConcCanvas.getLocationOnScreen();
    Point sp=new Point(mp.x+lp.x,mp.y+lp.y);

    if(last==null) {
      // first guess
      last=myConcCanvas.getCell(mp);
      myQuiz.get(last, sp);      
    }
    else {
         // second guess
         Point p=myConcCanvas.getCell(mp);
         myQuiz.get(p,sp);
         if(myQuiz.check(last,p)) {
            // a match
            if(myP1Turn) 
               myIPlay1++;
            else
               myIPlay2++;

            if(myIPlay1+myIPlay2==(myX*myY+1)/2) {
 System.out.println("end status:"+myQuiz.isDone());
               if(myIPlay1>myIPlay2)
                  myTurnLabel.setText("Player 1 WON!");
               else
                  myTurnLabel.setText("Player 2 WON!");
            }

            myConcCanvas.repaint();
            new PString("Match!").show(sp);
         }
         else {
            myP1Turn=!myP1Turn;
        
            if(myP1Turn)
               myTurnCount++;
         }

         last=null;
    }

    setTurnText();
  }

  public void actionPerformed(ActionEvent e) {
    String s=e.getActionCommand();
    if(s.equals("Exit")) {
      windowClosing(null);
    }
    else if(s.equals("New")) {
      reset();
    }
    else
System.out.println(s+":"+e);
  }
}