/* Copyright (c) 2017 FIRST. 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 FIRST nor the names of its 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 FITNESS FOR 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.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.util.ElapsedTime;

import org.firstinspires.ftc.robotcontroller.external.samples.HardwarePushbot;

/**
 * This file illustrates the concept of driving a path based on time.
 * It uses the common Pushbot hardware class to define the drive on the robot.
 * The code is structured as a LinearOpMode
 * <p>
 * The code assumes that you do NOT have encoders on the wheels,
 * otherwise you would use: PushbotAutoDriveByEncoder;
 * <p>
 * The desired path in this example is:
 * - Drive forward for 3 seconds
 * - Spin right for 1.3 seconds
 * - Drive Backwards for 1 Second
 * - Stop and close the claw.
 * <p>
 * The code is written in a simple form with no optimizations.
 * However, there are several ways that this type of sequence could be streamlined,
 * <p>
 * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
 * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
 */

@Autonomous(name = "Auto 6417", group = "6417")
//@Disabled
public class Auto6417 extends LinearOpMode {

    /* Declare OpMode members. */
    Hardware6417 robot = new Hardware6417();   // Use a Pushbot's hardware
    private ElapsedTime runtime = new ElapsedTime();

    static final double FORWARD_SPEED = 0.6;
    static final double TURN_SPEED = 0.8;

    int target = 2;  // Target cube
    int delay = 500; // Sloe down value

    @Override
    public void runOpMode() {

        // Set target based on color blob
        //target = 1; // Set target to far left
        target = 2; // Set target to middle
        //target = 3; // Set target to far right

        /*
         * Initialize the drive system variables.
         * The init() method of the hardware class does all the work here
         */
        robot.init(hardwareMap);

        // Send telemetry message to signify robot waiting;
        telemetry.addData("Status", "Ready to run");    //
        telemetry.update();

        // Wait for the game to start (driver presses PLAY)
        waitForStart();

        // Step through each leg of the path, ensuring that the Auto mode has not been stopped along the way

        // Step 0:  Check element
        //
        sleep(1000);

        // Step 0: Raise the lift to drop to the field
        robot.leftliftMotor.setPower(robot.LIFT_UP_POWER);
        robot.rightliftMotor.setPower(robot.LIFT_UP_POWER);
        sleep(2000);
        robot.leftliftMotor.setPower(0.0);
        robot.rightliftMotor.setPower(0.0);
        sleep(delay);

        // Step 1:  Turn left
        turnleft();
        sleep(900);
        halt();
        sleep(delay);

        // Step 2:  Drive backward
        backward();
        sleep(80);
        halt();
        sleep(delay);

        // Step 3:  Turn right based on target cube
        turnright();
        if (target == 3) sleep(300);
        if (target == 2) sleep(300);
        sleep(700);
        halt();
        sleep(delay);

        // Step 4:  Drive forward
        forward();
        sleep(2200);
        halt();
        sleep(delay);

        // Correct for right turn
        if (target == 1) {
            turnright();
            sleep(400);
            halt();
            sleep(delay);
        }
        if (target == 3) {
            turnleft();
            sleep(200);
            halt();
            sleep(delay);
        }

        // Step 5:  Drop marker
        robot.flipServo.setPosition(robot.FLIP_MAX);
        sleep(delay);
        robot.flipServo.setPosition(robot.FLIP_MIN);
        sleep(delay);

        telemetry.addData("Path", "Complete");
        telemetry.update();
        sleep(delay);
    }

    // Methods
    public void halt() {
        robot.leftfrontDrive.setPower(0);
        robot.rightfrontDrive.setPower(0);
        robot.rightbackDrive.setPower(0);
        robot.leftbackDrive.setPower(0);
        sleep(500);
    }

    public void forward() {
        robot.leftfrontDrive.setPower(FORWARD_SPEED);
        robot.rightfrontDrive.setPower(FORWARD_SPEED);
        robot.rightbackDrive.setPower(FORWARD_SPEED);
        robot.leftbackDrive.setPower(FORWARD_SPEED);
    }

    public void backward() {
        robot.leftfrontDrive.setPower(-FORWARD_SPEED);
        robot.rightfrontDrive.setPower(-FORWARD_SPEED);
        robot.rightbackDrive.setPower(-FORWARD_SPEED);
        robot.leftbackDrive.setPower(-FORWARD_SPEED);
    }

    public void turnleft() {
        robot.leftfrontDrive.setPower(TURN_SPEED);
        robot.rightfrontDrive.setPower(-TURN_SPEED);
        robot.rightbackDrive.setPower(-TURN_SPEED);
        robot.leftbackDrive.setPower(TURN_SPEED);
    }

    public void turnright() {
        robot.leftfrontDrive.setPower(-TURN_SPEED);
        robot.rightfrontDrive.setPower(TURN_SPEED);
        robot.rightbackDrive.setPower(TURN_SPEED);
        robot.leftbackDrive.setPower(-TURN_SPEED);
    }

}