/* 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.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.util.Range;

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

/**
 * This OpMode uses the common Pushbot hardware class to define the devices on the robot.
 * All device access is managed through the HardwarePushbot class.
 * The code is structured as a LinearOpMode
 * <p>
 * This particular OpMode executes a POV Game style Teleop for a PushBot
 * In this mode the left stick moves the robot FWD and back, the Right stick turns left and right.
 * It raises and lowers the claw using the Gampad Y and A buttons respectively.
 * It also opens and closes the claws slowly using the left and right Bumper buttons.
 * <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
 */
// Based on PushbotTeleopPOV_Linear
@TeleOp(name = "Teleop 6417", group = "6417")
//@Disabled
public class Teleop6417 extends LinearOpMode {

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

    double armOffset = robot.ARM_MIN;          // Arm servo current position
    double handOffset = robot.HAND_MIN;        // Hand servo current position
    final double HAND_SPEED = 0.04;            // Hand servo move speed
    final double ARM_SPEED = 0.04;             // Arm servo to move speed
    static final double NUDGE_SPEED = 0.6;   // Nudge motor speed
    static final int NUDGE_TIME = 50;        // Nudge time in milliseconds
    int tank = 0; // Flag to switch tank drive method
    int wait_some = 0; // Wait after servo set

    @Override
    public void runOpMode() {
        double left;
        double right;
        double drive;
        double turn;
        double max;

        /* Initialize the hardware 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("Say", "Hello Driver");    //
        telemetry.update();

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

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

            if (tank == 0) {  // If drive left stick straight, right stick turn
                // Run wheels in POV mode (note: The joystick goes negative when pushed forwards, so negate it)
                // In this mode the Left stick moves the robot fwd and back, the Right stick turns left and right.
                // This way it's also easy to just drive straight, or just turn.
                drive = -gamepad1.left_stick_y;
                turn = gamepad1.right_stick_x;
                // Combine drive and turn for blended motion
                left = drive - turn;
                right = drive + turn;
                if (gamepad1.back) tank = 1;  // If Back pressed, switch drive mode

            } else {  // Else drive left/right sticks the same
                // Left stick drives left wheels, right stick drives right wheels
                left = -gamepad1.right_stick_y;
                right = -gamepad1.left_stick_y;
                if (gamepad1.back) tank = 0;  // If Back pressed, switch drive mode
            }

            // Normalize the values so neither exceed +/- 1.0
            max = Math.max(Math.abs(left), Math.abs(right));
            if (max > 1.0) {
                left /= max;
                right /= max;
            }

            // Output the safe vales to the motor drives.
            robot.leftfrontDrive.setPower(left);
            robot.rightfrontDrive.setPower(right);
            robot.leftbackDrive.setPower(left);
            robot.rightbackDrive.setPower(right);

            // Use gamepad1 Y, A, X, B buttojns for nudges
            if (gamepad1.y) {
                robot.leftfrontDrive.setPower(NUDGE_SPEED);
                robot.rightfrontDrive.setPower(NUDGE_SPEED);
                robot.leftbackDrive.setPower(NUDGE_SPEED);
                robot.rightbackDrive.setPower(NUDGE_SPEED);
                sleep(NUDGE_TIME);
                robot.leftfrontDrive.setPower(0);
                robot.rightfrontDrive.setPower(0);
                robot.rightbackDrive.setPower(0);
                robot.leftbackDrive.setPower(0);
                sleep(200);
            } else {
                if (gamepad1.a) {
                    robot.leftfrontDrive.setPower(-NUDGE_SPEED);
                    robot.rightfrontDrive.setPower(-NUDGE_SPEED);
                    robot.leftbackDrive.setPower(-NUDGE_SPEED);
                    robot.rightbackDrive.setPower(-NUDGE_SPEED);
                    sleep(NUDGE_TIME);
                    robot.leftfrontDrive.setPower(0);
                    robot.rightfrontDrive.setPower(0);
                    robot.rightbackDrive.setPower(0);
                    robot.leftbackDrive.setPower(0);
                    sleep(200);
                }
            }
            if (gamepad1.x) {
                robot.leftfrontDrive.setPower(NUDGE_SPEED);
                robot.rightfrontDrive.setPower(-NUDGE_SPEED);
                robot.leftbackDrive.setPower(NUDGE_SPEED);
                robot.rightbackDrive.setPower(-NUDGE_SPEED);
                sleep(NUDGE_TIME);
                robot.leftfrontDrive.setPower(0);
                robot.rightfrontDrive.setPower(0);
                robot.rightbackDrive.setPower(0);
                robot.leftbackDrive.setPower(0);
                sleep(200);
            } else {
                if (gamepad1.b) {
                    robot.leftfrontDrive.setPower(-NUDGE_SPEED);
                    robot.rightfrontDrive.setPower(NUDGE_SPEED);
                    robot.leftbackDrive.setPower(-NUDGE_SPEED);
                    robot.rightbackDrive.setPower(NUDGE_SPEED);
                    sleep(NUDGE_TIME);
                    robot.leftfrontDrive.setPower(0);
                    robot.rightfrontDrive.setPower(0);
                    robot.rightbackDrive.setPower(0);
                    robot.leftbackDrive.setPower(0);
                    sleep(200);
                }
            }
            // Use gamepad right Bumper & right Trigger to move hand servo
            if (gamepad1.right_bumper) {
                handOffset += HAND_SPEED;
                wait_some = 1;
            } else {
                if (gamepad1.right_trigger > 0.0) {
                    handOffset -= HAND_SPEED;
                    wait_some = 1;
                }
            }
            handOffset = Range.clip(handOffset,robot.HAND_MIN,robot.HAND_MAX);
            robot.handServo.setPosition(handOffset);
            if (wait_some > 0) {
                sleep (60);
                wait_some = 0;
            }

            // Use gamepad left Bumper & left Trigger to move arm servo
            if (gamepad1.left_bumper) {
                armOffset += ARM_SPEED;
                wait_some = 1;
            } else {
                if (gamepad1.left_trigger > 0.0) {
                    armOffset -= ARM_SPEED;
                    wait_some = 1;
                }
            }
            armOffset = Range.clip(armOffset,robot.ARM_MIN,robot.ARM_MAX);
            robot.armServo.setPosition(armOffset);
            if (wait_some > 0) {
                sleep (60);
                wait_some = 0;
            }

            // Use gamepad dpad buttons to move lift up and down
            if (gamepad1.dpad_up) {
                robot.leftliftMotor.setPower(robot.LIFT_UP_POWER);
                robot.rightliftMotor.setPower(robot.LIFT_UP_POWER);
            } else {
                if (gamepad1.dpad_down) {
                    robot.leftliftMotor.setPower(robot.LIFT_DOWN_POWER);
                    robot.rightliftMotor.setPower(robot.LIFT_DOWN_POWER);
                } else {
                    robot.leftliftMotor.setPower(0.0);
                    robot.leftliftMotor.setPower(0.0);
                }
            }

            // Send telemetry message to signify robot running;
            telemetry.addData("mode", tank);
            telemetry.addData("left", "%.2f", left);
            telemetry.addData("right", "%.2f", right);
            telemetry.addData("servo1", "Offset = %.2f", armOffset);
            telemetry.addData("servo2", "Offset = %.2f", handOffset);
            telemetry.update();

            // Pace this loop so jaw action is reasonable speed.
            // sleep(10);
        }
    }
}
