package curve;

import java.awt.*;
import curve.*;

//Defines a generic curve class
public abstract class Curve extends Panel {

  protected Coordinate marker[]; //Array to hold markers

//  MyActionListener actionListener;

  protected DrawCurve drawCurve;
  protected double xMin,xMax;
  protected Label titleLabel;
  
  //Colour of this curve
  protected Color color;

  //Sets drawcurve, but doesn't add curve to drawing list
  //For exercises
  public void setDrawCurve(DrawCurve aDrawCurve) {
    drawCurve=aDrawCurve;
  };
  
  //Are allowed to draw curve; overridden by exercise
  public boolean canDraw() {
    return true;
  };
  
  //Set up an exercise
  public void setExercise() {};
  
  public void setColor(Color aColor) {
    color=aColor;
    titleLabel.setForeground(color);
    if(drawCurve!=null) drawCurve.repaint();
  };
  
  //Get maximum and minimum values of x,y on curve
  public abstract DoubleRect getMinMax();

  //for param t 0 to 1, return x and y
  public abstract Coordinate getValue(double t);
  
  //fuction to draw markers and construction lines when called
  public void drawConstructs() {
    //Default is to draw nothing; sub-classes to override method
  };

  //Function to draw construction lines for animation sequences
  public void drawAnimationConstructs(double t) {};  
   
  //Function to clean up animation constructs
  public void cleanAnimationConstructs(double t) {
    //Default do nothing (XOR mode on, so wipes last frame)
  };
  
  //Default to no procedure of dealing with changes in numbers
  protected void onActionPerformed(java.awt.event.ActionEvent event) {};

  //Adds two elements to a grid: a label, and numeric text field
  protected NumericTextField addNewVariable(GridBagLayout layout,String s,
  String number,MyActionListener actionListener,boolean EndOfLine) {
    NumericTextField numericTextField;
    GridBagConstraints constraints=new GridBagConstraints();
    //Set some defaults
    constraints.weightx=constraints.weighty=1.0;
    constraints.insets=new Insets(5,5,5,5);
    constraints.fill=GridBagConstraints.NONE;
    constraints.gridwidth=1;

    if(s!=null) {
      Label aLabel=new Label(s,Label.RIGHT);
      layout.setConstraints(aLabel,constraints);
      add(aLabel);
    };

    numericTextField=new NumericTextField(number);
    if(EndOfLine) constraints.gridwidth=GridBagConstraints.REMAINDER;
    layout.setConstraints(numericTextField,constraints);
    add(numericTextField);
    if(actionListener!=null)
      numericTextField.addActionListener(actionListener);
    return numericTextField;
  };

  //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) {
    int ctr,returnValue;
    double closest,distance;
    
    //Look for values within 1/25 of width of graph pixels radius
    closest=((xMax-xMin)/25)*((xMax-xMin)/25); 
    returnValue=-1; //default, no marker found

    if(marker!=null) //Array initialised
      for(ctr=0;ctr<marker.length;ctr++) {//for every marker in array
        distance=Math.pow(x-marker[ctr].x,2)+Math.pow(y-marker[ctr].y,2);
        if(distance<closest) {//closer than close
          returnValue=ctr;  //change return value
          closest=distance;
        };
      };
    return returnValue; //Defaults to no marker found
  };

  protected class MyActionListener implements java.awt.event.ActionListener
  {
    public void actionPerformed(java.awt.event.ActionEvent event)
    {
      onActionPerformed(event);
    }; //Pass event to object
  }
};

