package trianglepackage;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TriangleApplet extends Applet 
       implements MouseListener {

    TriangleBoard triangleBoard=new TriangleBoard();
    ArrayList<TriangleBoard.Move> moveSequence;
    int sideWidth = 200;
    int width;
    int height;
    int margin;
    int pegRadius;
    int moveNumber;
    int mode;
    float sqrt3;
    Polygon fwdIcon=new Polygon();
    Polygon resetIcon=new Polygon();
 
    public void init() {
        setBackground(Color.lightGray);
        width=getSize().width;
        height=getSize().height;
        sqrt3=(float)Math.sqrt(3);
        sideWidth=width-80;
        if (sideWidth*sqrt3/2+80>height) {sideWidth=Math.round((height-80)*2/sqrt3);}
        margin=(width-sideWidth)/2;
        pegRadius=sideWidth/20;
        triangleBoard.setup(5,new TriangleBoard.Point(2,1));
        addMouseListener(this);
        fwdIcon.addPoint(width/2+22,height-35);
        fwdIcon.addPoint(width/2+22,height-20);
        fwdIcon.addPoint(width/2+37,height-27);
        resetIcon.addPoint(width/2-28,height-27);
        resetIcon.addPoint(width/2-20,height-35);
        resetIcon.addPoint(width/2-20,height-20);
        resetIcon.addPoint(width/2-28,height-27);
        resetIcon.addPoint(width/2-28,height-20);
        resetIcon.addPoint(width/2-36,height-27);
        resetIcon.addPoint(width/2-28,height-35);
        mode=0; //setup
    }
    public void start() {
    }
    public void mouseEntered( MouseEvent e ) {
      // called when the pointer enters the applet's rectangular area
    }
    public void mouseExited( MouseEvent e ) {
      // called when the pointer leaves the applet's rectangular area
    }
    public void mousePressed( MouseEvent e ) {
      // called when the pointer enters the applet's rectangular area
    }
    public void mouseReleased( MouseEvent e ) {
      // called when the pointer leaves the applet's rectangular area
    }
     public void mouseClicked(MouseEvent e) {
        int mx=e.getX();
        int my=e.getY();
        if (mode==1) {e.consume(); return;}
        if (mx>width/2+10 && mx<width/2+50 && my>height-40 && my<height-15) {
           if (mode==0) {
              if (moveSequence!=null) {moveSequence.clear();}
              moveNumber=0;
              mode=1;
              repaint();
              moveSequence=triangleBoard.bestSequence();
              if (moveSequence.isEmpty()) {mode=3;} else {mode=2;} 
           } else if (mode==2) {
              triangleBoard.move(moveSequence.get(moveNumber));
              moveNumber++;
              if (moveNumber==moveSequence.size()) {mode=3;}
           }   
           repaint();
        } else if (mx>width/2-48 && mx<width/2-8 && my>height-40 && my<height-15) {
           triangleBoard.setup(5,new TriangleBoard.Point(2,1));
           mode=0;
           repaint();
        } else if (mode==0) {
           float row=4.7F-(height-50-my)/(sqrt3/12*sideWidth);
           int i=Math.round(row);
           float col=(mx-margin)*6F/sideWidth-(6-i)/2F;
           int j=Math.round(col);
           if (i>=0 && i<5 && j>=0 && j<=i) {
               triangleBoard.peg[i][j]=!triangleBoard.peg[i][j];
               repaint();
           }
        }
        e.consume();
    }
    private void showPeg(Graphics2D g2D,int i,int j,boolean peg) {
       int x=(int)Math.round(margin+(j+(4-i)/2.0+1)*sideWidth/6);
       int y=height-50-Math.round(sqrt3/2*sideWidth/6*(4.7F-i));
       if (peg) {
           g2D.setColor(Color.red);
           g2D.fillOval(x-pegRadius,y-pegRadius,2*pegRadius,2*pegRadius);
       } else {
           g2D.setColor(Color.black);
           g2D.fillOval(x-4,y-4,8,8);
       }  
    }
    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        g2D.setStroke(new BasicStroke(3)); 
        g2D.setColor(Color.blue);
        g2D.drawLine(margin,height-50,margin+sideWidth,height-50);
        g2D.drawLine(margin,height-50,margin+sideWidth/2,(int) Math.round(height-50-sqrt3*sideWidth/2));
        g2D.drawLine(margin+sideWidth,height-50, margin+sideWidth/2,(int) Math.round(height-50-sqrt3*sideWidth/2));
        if (mode!=1) {
           for (int i=0;i<5;i++) {
               for (int j=0;j<=i;j++) {
                   showPeg(g2D,i,j,triangleBoard.peg[i][j]);
               }
           }
        }
        g2D.setColor(Color.blue);
        g2D.fillRoundRect(width/2-48,height-40,40,25,10,10);
        if (mode==1 || mode==3) {   //calculating or done
           g2D.setColor(Color.gray);
        }   
        g2D.fillRoundRect(width/2+10,height-40,40,25,10,10);
        g2D.setColor(Color.white);
        if (mode<2) {
            g2D.setFont(new Font("arial",1,14));
            g2D.drawString("?", width/2+25,height-20);   
        } else {
           g2D.fillPolygon(fwdIcon);
        }   
        g2D.fillPolygon(resetIcon);
    }
}
