package curve;

import java.awt.*;
import curve.*;

//Defines ExercisePanel class
public class ExercisePanel extends Curve {

  protected Curve curve;
  protected Button button;
  protected int mode;
  protected DoubleRect curveRect;
  protected static final int OFF=0;
  protected static final int EXERCISE=1;
  protected static final int ANSWER=2;

  public ExercisePanel(DrawCurve drawCurve,Curve curve) {
    this.drawCurve=drawCurve;
    this.curve=curve;
    curve.setDrawCurve(drawCurve);
    drawCurve.addCurve(this);
    mode=0;

    setLayout(new FlowLayout());
    setBackground(Color.lightGray);
    button=new Button("Show Exercise");
    button.addActionListener(new ButtonActionListener());
    add(button);
  };

  public void setColor(Color aColor) {
    curve.setColor(aColor);
    color=aColor;
  };

  //Get maximum and minimum values of x,y on curve
  public DoubleRect getMinMax() {
    DoubleRect rect;
    double rand;
    if(mode==OFF) return null;
    else return curveRect;
  }

  public boolean canDraw() {
    if(mode==OFF) return false;
    else return true;
  };

  //for param t 0 to 1, return x and y
  public Coordinate getValue(double t) {
    return curve.getValue(t);
  }

  //fuction to draw markers and construction lines when called
  public void drawConstructs() {
    if(mode==ANSWER) curve.drawConstructs();
  };

  //Function to draw construction lines for animation sequences
  public void drawAnimationConstructs(double t) {
    if(mode==ANSWER) curve.drawAnimationConstructs(t);
  };

  //Function to clean up animation constructs
  public void cleanAnimationConstructs(double t) {
    if(mode==ANSWER) curve.cleanAnimationConstructs(t);
  };

  //Default to no procedure of dealing with changes in numbers
protected void onActionPerformed(java.awt.event.ActionEvent event) {};

  //fuction to set a marker
  public void setMarker(int i,double x,double y) {
    if(marker!=null) //array initialised
    if(i>=0 && i<marker.length) {
      marker[i].x=x;
      marker[i].y=y;
    };
  };

  //function to get a marker
  public int getMarker(double x,double y) {
    return -1; //No marker found
  };

  protected void buttonPressed() {
    double rand;
    switch(mode) {
      case OFF:
      curve.setExercise();
      curveRect=curve.getMinMax();
      rand=Math.random()*0.5;
      curveRect.x-=rand;
      curveRect.width+=rand+Math.random()*0.5;
      rand=Math.random()*0.5;
      curveRect.y-=rand;
      curveRect.height+=rand+Math.random()*0.5;
      mode=EXERCISE;
      button.setLabel("Show Answer");
      break;
      case EXERCISE:
      mode=ANSWER;
      button.setLabel("Hide Exercise");
      break;
      case ANSWER:
      mode=OFF;
      button.setLabel("Show Exercise");
      break;
    };
    drawCurve.repaint();
  }

  protected class ButtonActionListener implements java.awt.event.ActionListener
  {
    public void actionPerformed(java.awt.event.ActionEvent event)
    {
      buttonPressed();
    }
  }
};

