/*
Copyright (c) 2016 Robert Atkinson

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted (subject to the limitations in the disclaimer below) provided that
the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of Robert Atkinson nor the names of his contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESSFOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.firstinspires.ftc.teamcode;


import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.ColorSensor;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;


@TeleOp(name="red_ramp_shoot", group="Linear Opmode")  // @Autonomous(...) is the other common choice
//@Disabled
public class red_ramp_shoot extends LinearOpMode {
    DcMotor leftfront;
    DcMotor rightfront;
    DcMotor leftback;
    DcMotor rightback;
    DcMotor cata;
    ColorSensor colorSensor;

    /* Declare OpMode members. */
    private ElapsedTime runtime = new ElapsedTime();
    static final double COUNTS_PER_MOTOR_REV = 1120;    // eg: Andy mark Motor Encoder
    static final double WHEEL_DIAMETER_INCHES = 3.0;     // For figuring circumference
    double countsl = 0;
    double countsr = 0;

    @Override
    public void runOpMode() throws InterruptedException {

        leftfront  = hardwareMap.dcMotor.get("A1");
        rightfront = hardwareMap.dcMotor.get("B1");
        leftback  = hardwareMap.dcMotor.get("A2");
        rightback = hardwareMap.dcMotor.get("B2");
        cata = hardwareMap.dcMotor.get("C1");


        rightback.setDirection(DcMotor.Direction.REVERSE);
        rightfront.setDirection(DcMotor.Direction.REVERSE);
        leftfront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
        leftfront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
        leftback.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
        leftback.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
        rightfront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
        rightfront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
        rightback.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
        rightback.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);

        telemetry.addData("Status", "Initialized");
        telemetry.update();


        waitForStart();
        runtime.reset();

        // run until the end of the match (driver presses STOP)
        while (opModeIsActive()) {

            //to drive back, set driveFwd power and distance to negative
            //to drive right, set driveLeft power and distance to negative
            shoot();

            telemetry.update();

            idle(); // Always call idle() at the bottom of your while(opModeIsActive()) loop

            break;
        }
    }

    public void drive(double distance, double power) {
        double x;
        x = (distance * 12 * COUNTS_PER_MOTOR_REV) / (WHEEL_DIAMETER_INCHES * 3.1415);
        countsl=countsl-x;
        countsr=countsr-x;
        leftfront.setTargetPosition((int)countsl);
        leftfront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        leftfront.setPower(-1*power);
        leftback.setTargetPosition((int)countsl);
        leftback.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        leftback.setPower(-1*power);
        rightfront.setTargetPosition((int)countsr);
        rightfront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        rightfront.setPower(-1*power);
        rightback.setTargetPosition((int)countsr);
        rightback.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        rightback.setPower(-1*power);
        while (leftfront.isBusy() && leftback.isBusy() && rightfront.isBusy() && rightback.isBusy()) {

        }
    }

    public void turn(double distance, double power) {
        double x;
        x = (distance * 12 * COUNTS_PER_MOTOR_REV) / (WHEEL_DIAMETER_INCHES * 3.14159);
        countsl=countsl-x;
        countsr=countsr+x;
        leftfront.setTargetPosition((int)countsl);
        leftfront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        leftfront.setPower(-1*power);
        leftback.setTargetPosition((int)countsl);
        leftback.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        leftback.setPower(-1*power);
        rightfront.setTargetPosition((int)countsr);
        rightfront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        rightfront.setPower(power);
        rightback.setTargetPosition((int)countsr);
        rightback.setMode(DcMotor.RunMode.RUN_TO_POSITION);
        rightback.setPower(power);
        while (leftfront.isBusy() && leftback.isBusy() && rightfront.isBusy() && rightback.isBusy()) {

        }
    }

//    public void capture(){
//
//        if ((colorSensor.blue() > colorSensor.red()) && (colorSensor.blue() >= 2)) {
//            s2.setPower(FWD);
//            sleep(2000);
//            s2.setPower(BCK);
//            sleep(1500);
//            done=TRUE;
//            s2.setPower(0);
//        } else if ((colorSensor.blue() < colorSensor.red()) && (colorSensor.red() >= 2)) {
//
//            s1.setPower(BCK);
//            sleep(2000);
//            s1.setPower(FWD);
//            sleep(1500);
//            done=TRUE;
//            s1.setPower(0);
//        }
//        else{
//            driveBack(0.5,0.2);
//        }
//    }

    public void shoot(){
        cata.setPower(-0.7);
        sleep(200);
        cata.setPower(0);
        sleep(1500);
        cata.setPower(0.7);
        sleep(300);
        cata.setPower(0);
        sleep(1500);
    }
    public void sleep(int i){
        long initial_time = System.currentTimeMillis();
        while(System.currentTimeMillis()-initial_time <i){

        }
    }

}
