/*
 *  Myro.h
 *  Mockup of Myro/C++ for classroom demo
 *
 *  Created by Bruce MacLennan on 2009/1/6.
 *
 */


#ifndef MYRO_H_
#define MYRO_H_
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

void pause (double seconds) {
    if (seconds > 60) {
        cout << "Waiting more than a minute is too long!\n";
        seconds = 60;
    }
    int endTime = time(NULL) + seconds;
    while (time(NULL) < endTime); // tight loop!
}

class Scribbler {
private:
    string name;

    void showAction (string action) {
        cout << name << " " << action;
    }

public:

    Scribbler () {
        name = "Scribbler";
    }

    // Chapter 1

    void connect () {
        showAction ("is connected.\n");
    }

    void disconnect () {
        showAction ("is disconnected.\n");
    }

    void beep (double time, double freq) {
        showAction ("beeps at freq. ");
        cout << freq << "Hz for " << time << " sec.\n";
    }

    string getName () {
        return name;
    }

    void setName (string newName) {
        name = newName;
    }

    // Chapter 2

    void backward (double speed) {
        showAction ("is moving backward at speed ");
        cout << speed << "...\n";
    }

    void backward (double speed, double seconds) {
        showAction ("moves backward at speed ");
        cout << speed << " for " << seconds << " sec.\n";
        pause(seconds);
    }

    void forward (double speed) {
        showAction ("is moving forward at speed ");
        cout << speed << "...\n";
    }

    void forward (double speed, double seconds) {
        showAction ("moves forward at speed ");
        cout << speed << " for " << seconds << " sec.\n";
        pause(seconds);
    }

    void motors (double left, double right) {
        if (left == 0 && right == 0)
            showAction ("stops.\n");
        else {
            showAction ("moving with left motor = ");
            cout << left << " and right motor = " << right << "...\n";
        }
    }

    void move (double translate, double rotate) {
        showAction ("translating at speed ");
        cout << translate << " and rotating at speed "
            << rotate << "...\n";
    }

    void rotate (double speed) {
        showAction ("rotating at speed ");
        cout << speed << "...\n";
    }

    void stop () {
        showAction ("stops.\n");
    }

    void turnLeft (double speed) {
        showAction ("is turning left at speed ");
        cout << speed << ".\n";
    }

    void turnLeft (double speed, double seconds) {
        showAction ("is turning left at speed ");
        cout << speed << " for " << seconds << " sec.\n";
        pause(seconds);
    }

    void turnRight (double speed) {
        showAction ("is turning right at speed ");
        cout << speed << ".\n";
    }

    void turnRight (double speed, double seconds) {
        showAction ("is turning right at speed ");
        cout << speed << " for " << seconds << " sec.\n";
        pause(seconds);
    }

    // Chapter 3

    void speak (string something, int synch = 0) {
        showAction ("is speaking: ");
        cout << something << endl;
    }

    // Chapter 6

    // Higher values are darker
    int getLight (string position) {
        int reading;
        cout << position << " light sensor value (0..5000) = ";
        cin >> reading;
        return reading;
    }

    int getLight (int position) {
        char* posName[3] = {"left", "center", "right"};
        return getLight(string(posName[position]));
    }

    // Higher values are brighter
    int getBright (string position) {
        int reading;
        cout << position << " brightness value (unsigned int) = ";
        cin >> reading;
        return reading;
    }

    int getBright (int position) {
        char* posName[3] = {"left", "center", "right"};
        return getBright(string(posName[position]));
    }

    // Returns true for no obstacle
    bool getIR (string position) {
        bool reading;
        cout << position << " IR detector (0/1) = ";
        cin >> reading;
        return reading;
    }

    bool getIR (int position) {
        char* posName[2] ={"left", "right"};
        return getIR(string(posName[position]));
    }

};

Scribbler robot;

void wait (double seconds) {
    cout << "Waiting for " << seconds << " sec. ...\n";
    pause(seconds);
}

int TRtime = 0;

bool timeRemaining (double seconds) {
    if (seconds > 60) {
        cout << "Waiting more than a minute is too long!\n";
        seconds = 60;
    }
    if (TRtime == 0) { // we are starting the delay
        TRtime = time(NULL) + seconds;
        return true;
    } else if (time (NULL) >= TRtime) { // we are done waiting
        TRtime = 0; // indiate we are not waiting anymore
        return false;
    } else return true;
}

string askQuestion (string message, char* options = "Yes, No") {
    string answer;
    cout << "Myro Question: " << message
        << " (" << options << "): ";
    cin >> answer; // No error checking! Beware!
    return answer;
}

void speak (string something, bool synch = true) {
    if (synch) {
        cout << "Computer stops to say: \"" << something << "\"\n";
    } else {
        cout << "Computer says: \"" << something << "\" while continuing...\n";
    }
}

void connect () {
    robot.connect();
}

void disconnect () {
    robot.disconnect();
    exit(0);
}



#endif /* MYRO_H_ */

