package student;

import chess.Chess;
import chess.Screen;
import chess.Sprite;
import chess.Turtle;
import java.awt.*;

public class SumitArmbot {
    
    public static void main(String[] args) {
        /* here you go */
        
        Screen g= Chess.newScreen(800,700);
        Turtle tim= g.newTurtle();
        tim.hide();
        tim.ultraFast();
        
        Chess.report("Welcome. You are needed. An urgent call has come up from the Knight of Ardougnean.\n" +
        "You must hurry and get the Knight and take him to the hospital. Use the crane thats is\n provided." +
        "Press 'k' and 'l' to move the shoulder joint, use 'a' and 's' to move the elbow joint,\nuse  'n' and 'm' to move the wrist joint (which is invisible), and use 'space' to grab the Knight.(Note: Tap the keys don't hold!)");
         
        int jointOneAngle= 45;
        int jointTwoAngle= -90;
        int jointThreeAngle= 0;
        
        boolean haveObject=false;
        
        Sprite object= g.newSprite("/student/knightofardougneanim.gif");
        object.setPosition(178,246);
        boolean done= false;
        
        
        
        while ( !done ) {
            g.clear( java.awt.Color.white );
            tim.home();   // reset orientation
            tim.setPosition(252,304);
            tim.right(90);
            tim.image("/student/images.jpg");
            tim.right(jointOneAngle);
            tim.setLineWidth(5);
            tim.forward(300);
            tim.left(jointTwoAngle);
            tim.setLineWidth(7);
            tim.forward(250);
            tim.left(jointThreeAngle);
            tim.setLineWidth(4);
            tim.penUp();
            tim.forward(50);
            tim.penDown();
            
            if ( haveObject ) {
                object.setPosition( tim.getPositionX(), tim.getPositionY() );
                object.setOrientation( tim.getOrientation() );
            }
            int key=0;
            while ( key==0 ) key=g.getKey();
            if ( key=='a' ) {
                jointTwoAngle= jointTwoAngle-2;
            } else if ( key=='s') {
                jointTwoAngle= jointTwoAngle+2;
                
            } else if  ( key=='k') {
                jointOneAngle= jointOneAngle-2;
            } else if ( key=='l' ) {
                jointOneAngle= jointOneAngle+2;
            } else if ( key=='m' ) {
                jointThreeAngle= jointThreeAngle-5;
            } else if ( key=='n' ) {
                jointThreeAngle= jointThreeAngle+5;
            //} else if ( key=='q' ) {
            //    done= true;
            } else if ( key==' ' ) {
                if ( haveObject ) {
                    // drop the object
                    haveObject=false;
                } else {
                    object= g.getSpriteAt( tim.getPositionX(), tim.getPositionY() );
                    if ( object!=null ) {
                        haveObject= true;
                    }
                }
            }
            
            Rectangle r= new Rectangle( 373,487,114,51 );
            g.getGraphics().draw(r);
            
           // System.err.println( r.contains( object.getPositionX(), object.getPositionY()) );
            if ( r.contains( object.getPositionX(), object.getPositionY()) ) {
                done= true;
            }
            
        } // end of game loop
                
        tim.home();
       tim.back(90);
        tim.right(90);
        
        g.clear( java.awt.Color.white );
        
        tim.image("/student/images.jpg");
        Chess.report("Congrats! You got the knight to the hospital!");
        System.exit(0); 
        /* two braces below */
    }
    
}
