// RobotBuilder Version: 1.5
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.


package org.usfirst.frc811.RobotCode2015.subsystems;

import org.usfirst.frc811.RobotCode2015.Config;
import org.usfirst.frc811.RobotCode2015.Robot;
import org.usfirst.frc811.RobotCode2015.RobotMap;
import org.usfirst.frc811.RobotCode2015.commands.*;

import edu.wpi.first.wpilibj.*;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;


/**
 *
 */
public class Drive extends Subsystem implements Config {
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
    Gyro gyro1 = RobotMap.driveGyro1;
    SpeedController frontright = RobotMap.drivefrontright;
    SpeedController frontleft  = RobotMap.drivefrontleft;
    SpeedController backleft   = RobotMap.drivebackleft;
    SpeedController backright  = RobotMap.drivebackright;
    RobotDrive robotDrive41    = RobotMap.driveRobotDrive41;
    Encoder drive_encoder      = RobotMap.drivedrive_encoder;
    Joystick joystick1         = RobotMap.joystick1;
    double input1y;
    double input1S;
    double input1x;
    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS

    
    // Put methods for controlling this subsystem
    // here. Call these from Commands.

    public void initDefaultCommand() {
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DEFAULT_COMMAND
        setDefaultCommand(new drive_w_joysticks());
        // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DEFAULT_COMMAND
	
        // Set the default command for a subsystem here.
        //setDefaultCommand(new MySpecialCommand());
    }
    
    public void driveWithJoy() {
    	
    	boolean strafe = false;
    	double correction = 0;
    	
    	if (Math.abs(joystick1.getRawAxis(DRIVE_STRAFING_RIGHT_JOYSTICK_AXIS)) <= 0.1 && Math.abs(joystick1.getRawAxis(DRIVE_STRAFING_LEFT_JOYSTICK_AXIS)) <= 0.1) {
    		strafe = true;
    		correction = 0;
    	}
    	if ((Math.abs(joystick1.getRawAxis(DRIVE_STRAFING_RIGHT_JOYSTICK_AXIS)) >=0.3 || Math.abs(joystick1.getRawAxis(DRIVE_STRAFING_LEFT_JOYSTICK_AXIS)) >= 0.3) && strafe ) {
    		//gyro1.getAngle();
    		gyro1.reset();
    		strafe = false;
    	}
    	 
    	if (Math.abs(joystick1.getRawAxis(DRIVE_STRAFING_RIGHT_JOYSTICK_AXIS)) >= 0.3 || Math.abs(joystick1.getRawAxis(DRIVE_STRAFING_LEFT_JOYSTICK_AXIS)) >= 0.3) {
    		correction = 0; // gyro1.getAngle();
    	}
    	
    	if (joystick1.getRawAxis(DRIVE_Y_JOYSTICK_AXIS) > 0.3 || joystick1.getRawAxis(DRIVE_Y_JOYSTICK_AXIS) < -0.3) {
    		input1y = joystick1.getRawAxis(DRIVE_Y_JOYSTICK_AXIS);
    	} else {
    		input1y = 0.0;
    	}
    	if (joystick1.getRawAxis(DRIVE_STRAFING_RIGHT_JOYSTICK_AXIS) > 0.3) {
    		input1S = joystick1.getRawAxis(DRIVE_STRAFING_RIGHT_JOYSTICK_AXIS);
    	} else if (joystick1.getRawAxis(DRIVE_STRAFING_LEFT_JOYSTICK_AXIS) > 0.3) {
    		input1S = joystick1.getRawAxis(DRIVE_STRAFING_LEFT_JOYSTICK_AXIS) * -1;
    	} else {	
    		input1S = 0.0;
    	}
    	if (joystick1.getRawAxis(DRIVE_X_JOYSTICK_AXIS) > 0.3 || joystick1.getRawAxis(DRIVE_X_JOYSTICK_AXIS) < -0.3 ) {
    		input1x = joystick1.getRawAxis(DRIVE_X_JOYSTICK_AXIS);
    	} else {
    		input1x = 0.0;
    	}
    	robotDrive41.mecanumDrive_Cartesian(input1S, input1y, input1x + correction * -1, 0);
  //  	robotDrive41.mecanumDrive_Cartesian(input1S, 0, 0, 0); 
    	SmartDashboard.putNumber("correction", correction);
    	SmartDashboard.putString("drive status", "drive with joy :D");
    }
    
    @SuppressWarnings("deprecation")
	public void driveAuto() {
    	
      	double pulse = 1;
      	
  	   	RobotMap.pid = new PIDController(4, 0, 0, new PIDSource() { public double pidGet() {
   		return drive_encoder.getDistance();}},
   		new PIDOutput() { public void pidWrite(double d) {
   			robotDrive41.mecanumDrive_Cartesian(0, .2, 0, 0);
   			SmartDashboard.putString("drive status", "in pidloop for driving");
  		}});
  	   	drive_encoder.setDistancePerPulse(pulse);
  	  	RobotMap.pid.setAbsoluteTolerance(0.01);
   		RobotMap.pid.setSetpoint(DRIVE_PID_SETPOINT);

   		SmartDashboard.putString("drive status", "drive forward auto");
   		SmartDashboard.putNumber("Auto value", drive_encoder.getDistance());
    	    	
    }
    
    public void strafeAuto() { //might be totally wrong -TEST!!!
      	
  	   	RobotMap.pid = new PIDController(4, 0, 0, new PIDSource() { public double pidGet() {
   		return drive_encoder.getDistance();}},
   		new PIDOutput() { public void pidWrite(double d) {
   			robotDrive41.mecanumDrive_Cartesian(d, 0, 0, 0);
  		}});
  	  	RobotMap.pid.setAbsoluteTolerance(0.01);
  	  	RobotMap.pid.setSetpoint(DRIVE_PID_SETPOINT);
    	
    	SmartDashboard.putString("drive status", "strafe auto");
    }
    
    public void backUp() {
      	
  	   	RobotMap.pid = new PIDController(4, 0, 0, new PIDSource() { public double pidGet() {
   		return drive_encoder.getDistance();}},
   		new PIDOutput() { public void pidWrite(double d) {
   			robotDrive41.mecanumDrive_Cartesian(0, -d, -gyro1.getAngle() * - 1, 0);
  		}});
  	  	RobotMap.pid.setAbsoluteTolerance(0.01);
   		RobotMap.pid.setSetpoint(DRIVE_PID_SETPOINT);
   		
   		SmartDashboard.putString("drive status", "drive backward auto");
    }
}

