//-------------------- DrawShape (Step 4) ---------------------//

import javabook.*;

 /**
 * 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 MainWindow mainWindow;
         
    /**
     * 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()
    {
        mainWindow  = new MainWindow( "Drawing Shape" );
        outputBox   = new OutputBox ( mainWindow );
        inputBox    = new InputBox  ( mainWindow );
        messageBox  = new MessageBox( mainWindow );
        
        shapeListBox = new ListBox( mainWindow, "Select Shape:" );
        shapeListBox.addItem( "Line"      );
        shapeListBox.addItem( "Circle"    );
        shapeListBox.addItem( "Rectangle" );
        
        colorListBox = new ListBox( mainWindow, "Select Color:" );
        colorListBox.addItem( "Magenta" );
        colorListBox.addItem( "Cyan"    );
        colorListBox.addItem( "Red"     );
        colorListBox.addItem( "Blue"    );
        colorListBox.addItem( "Green"   );
        
        mainWindow.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");
    }


    /**  
     * Draws the selected shape in a chosen position, size,
     * and color.
     *
     */
    private void draw( )
    {
        outputBox.printLine("Inside draw");
    }
    
    /**
     * 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( )
    {
        outputBox.printLine("Inside selectColor");               //TEMP
       
        selectedColor = colorListBox.getSelectedIndex();
        
        outputBox.printLine("Selected Color: " + selectedColor);  //TEMP
    }
    
  
    /**
     * 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
    }
  
}

