 //-------------------- DrawShape (Step 6 - Final) ---------------------//

import javabook.*;
import java.awt.*;

 /**
 * This class is the top-level controller object for managing
 * all other objects in the program.
 *
 * @author Dr. Caffeine
 *
 */
class DrawShape
{

//--------------------------------
//  Data Members
//--------------------------------

    /**
     * Constant for a line
     */
    private final int LINE        = 0;
    
    /**
     * Constant for a circle
     */
    private final int CIRCLE      = 1;
    
    /**
     * Constant for a rectangle
     */
    private final int RECTANGLE   = 2;
    
    /**
     * Constant for color magenta
     */
    private final int MAGENTA     = 0;
    
    /**
     * Constant for color cyan
     */
    private final int CYAN        = 1;
    
    /**
     * Constant for color red
     */
    private final int RED         = 2;
    
    /**
     * Constant for color blue
     */
    private final int BLUE        = 3;
    
    /**
     * Constant for color green
     */
    private final int GREEN       = 4;
    
    /**
     * The main window of the program
     */
    private DrawingBoard canvas;
         
    /**
     * OutputBox for displaying the test messages
     */
    private OutputBox  outputBox;
    
    /**
     * Input for getting the shape dimension
     */
    private InputBox   inputBox;
   
    /**
     * MessageBox for error messages
     */
    private MessageBox messageBox;
   
    /**
     * ListBox for listing shapes
     */
    private ListBox    shapeListBox;

    /**
     * The shape selected by the user
     */
    private int        selectedShape;
    
    /**
     * ListBox for listing colors
     */
    private ListBox    colorListBox;

    /**
     * The color selected by the user
     */
    private int        selectedColor;
    
    /**
     * Boolean flag for input error. False if there's
     * an input error.
     */
    private boolean     canDraw; 

    /**
     * X-coordinate of the shape's origin point
     */
    private int  originX;
    
    /**
     * Y-coordinate of the shape's origin point
     */
    private int  originY;
    
    /**
     * X-coordinate of the line's end point
     */
    private int  endX;
    
    /**
     * Y-coordinate of the line's end point
     */
    private int  endY;
    
    /**
     * The radius of the circle
     */
    private int  radius;
    
    /**
     * The width of the rectangle
     */
    private int  width;
    
    /**
     * The height of the rectangle
     */
    private int  height;
      
//--------------------------------
//  Constructors
//--------------------------------

    /**
     * Default constructor. The default window title is "Drawing Shape".
     */
    public DrawShape()
    {
        canvas      = new DrawingBoard("Drawing Shape");
        outputBox   = new OutputBox ( canvas );
        inputBox    = new InputBox  ( canvas );
        messageBox  = new MessageBox( canvas );
        
        shapeListBox = new ListBox( canvas, "Select Shape:" );
        shapeListBox.addItem( "Line"      );
        shapeListBox.addItem( "Circle"    );
        shapeListBox.addItem( "Rectangle" );
        
        colorListBox = new ListBox( canvas, "Select Color:" );
        colorListBox.addItem( "Magenta" );
        colorListBox.addItem( "Cyan"    );
        colorListBox.addItem( "Red"     );
        colorListBox.addItem( "Blue"    );
        colorListBox.addItem( "Green"   );
        
        canvas.setVisible( true );
        outputBox.setVisible( true );
    }

//-------------------------------------------------
//    Public Methods:
// 
//      void    start    (    )
//
//------------------------------------------------
    
    /**
     * Top-level method that calls other private methods
     * to draw the selected shape in user-specified
     * position, size, and color.   
     */
    public void start( )
    {
        describeProgram( );
        selectShape( );
        selectColor( );
        selectDimension( );
        draw( );
    }

//-------------------------------------------------
//    Private Methods:
// 
//    void    describeProgram       (      )
//    void    draw                  (      )
//
//    void    getCircleDimension    (      )      
//    void    getLineDimension      (      )
//    void    getRectangleDimension (      )
//
//    void    selectColor           (      )
//    void    selectDimension       (      )
//    void    selectShape           (      )
//
//------------------------------------------------

    /**
     * Provides a brief explanation of the program to the user.   
     *
     */    
    private void describeProgram( )
    {
        //outputBox.printLine("Inside describeProgram");
        
        outputBox.skipLine(1);
        outputBox.printLine("This program draws a line, circle, or rectangle");
        outputBox.printLine("you choose in the color of your choice.");

        outputBox.skipLine(1);
        outputBox.printLine("Close this description window to continue");
        outputBox.printLine("with the program.");
        
        outputBox.waitUntilClose( );
    }


    /**  
     * Draws the selected shape in a chosen position, size,
     * and color.
     *
     */
    private void draw( )
    {
        if (canDraw) {
            switch (selectedShape) {
         
                case LINE:
                            canvas.drawLine(originX, originY, endX, endY);
                            break;
                  
                case CIRCLE:
                            canvas.drawCircle(originX, originY, radius);
                            break;
                            
                case RECTANGLE:
                            canvas.drawRectangle(originX, originY, width, height);
                            break;
            }
       }
    }
    
    /**
     * Gets the dimension of a circle
     */
    private void getCircleDimension()
    {
        originX  = inputBox.getInteger("X-coord of the center");
        originY  = inputBox.getInteger("Y-coord of the center");
        radius   = inputBox.getInteger("Radius of the circle");
  
        //make sure everything is positive
        if (originX < 0 || originY < 0 || radius < 0) {
           messageBox.show("Negative number is entered. " +
                           "Cannot draw a circle.");
           canDraw = false;
        }
        else { //input okay
           canDraw = true;
          // outputBox.printLine("circle dimension okay");  //TEMP
        }
    }
  
    /**
     * Gets the dimension of a line
     */
    private void getLineDimension( )
    {
        originX = inputBox.getInteger("X-coord of starting point");
        originY = inputBox.getInteger("Y-coord of starting point");
        endX    = inputBox.getInteger("X-coord of ending point");
        endY    = inputBox.getInteger("Y-coord of ending point");
  
        //make sure everything is positive
        if (originX < 0 || originY < 0 || endX < 0 || endY < 0) {
           messageBox.show("Negative number is entered. " +
                           "Cannot draw a line.");
           canDraw = false;
        }
        else { //input okay
           canDraw = true;
         //  outputBox.printLine("line dimension okay");  //TEMP
        }
    }
    
   
    /**
     * Gets the dimension of a rectangle.
     */
    private void getRectangleDimension( )
    {
        originX  = inputBox.getInteger("X-coord of origin");
        originY  = inputBox.getInteger("Y-coord of origin");
        width    = inputBox.getInteger("Rectangle width");
        height   = inputBox.getInteger("Rectangle height");
  
        //make sure everything is positive
        if (originX < 0 || originY < 0 || width < 0 || height < 0) {
           messageBox.show("Negative number is entered. " +
                           "Cannot draw a rectangle.");
           canDraw = false;
        }
        else { //input okay
           canDraw = true;
        //   outputBox.printLine("rectangle dimension okay");  //TEMP
        }
    }
   
   
    /**
     * Lets the user select the color from a ListBox.   
     */
    private void selectColor( )
    {
        selectedColor = colorListBox.getSelectedIndex();
      
        switch (selectedColor) {
      
            case ListBox.CANCEL:
            case ListBox.NO_SELECTION:
                         messageBox.show("No color was selected. " +
                                         "Will draw in black.");
                         break;
               
            case MAGENTA: 
                         canvas.setColor(Color.magenta);
                         break;
               
            case CYAN: 
                         canvas.setColor(Color.cyan);
                         break;
               
            case RED: 
                         canvas.setColor(Color.red);
                         break;
               
            case GREEN: 
                         canvas.setColor(Color.green);
                         break;
               
            case BLUE: 
                         canvas.setColor(Color.blue);
                         break;
               
            default:
                         messageBox.show("ListBox error");
                         break;
        }
    }
    
  
    /**
     * Lets the user select the size and position.   
     *
     */
    private void selectDimension( )
    {
        switch (selectedShape) {
         
            case ListBox.CANCEL:  // user canceled, so do nothing
                             canDraw = false;
                             break;

            case ListBox.NO_SELECTION:  // no shape selected
                             canDraw = false;
                             break;

            case LINE:
                             getLineDimension();
                             break;
               
            case CIRCLE:
                             getCircleDimension();
                             break;
               
            case RECTANGLE:
                             getRectangleDimension();
                             break;
               
            default:
                             messageBox.show("ListBox error");
                             canDraw = false;
                             break;
      }

    }
    
    /**
     * Lets the user select the shape from a ListBox.   
     *
     */
    private void selectShape( )
    {
      //  outputBox.printLine("Inside selectShape");                //TEMP
        
        selectedShape = shapeListBox.getSelectedIndex();
        
      //  outputBox.printLine("Selected Shape: " + selectedShape);  //TEMP
    }
  
}
     
   