#include "headers/Interface.h"

// I added it to pass spots, because the parking options need it to check where
// is free parking_spots is declared in main, and if i declare it
// liberal use of
// cin.ignore(10000, '\n');
// so it skips to the next newline, in essence clearing the cin buffer

bool verify(void);

void interface(vector<Park_spot>& spots) {
    /*
    string introduction = "P A R K M A N N E"; //logo animation, disable during testing
    text_animation(introduction, 50);
    */
   __label__ exit;
    cout << "\nWelcome to the parking system. Please login...";
    int id;
    string password;
    cout << "\nEnter your id: ";
    cin >> id;
    cin.ignore(10000, '\n');
    Customer c = query_customer_with_id(id);
    cout << "\nEnter your password: ";
    std::getline(cin, password);
    
    while (!(verify_password(c.password, password))) {
        cout << "ERROR: wrong password. Please retype your password or type [x] to exit:\n";
        std::getline(cin, password);
        if(password=="x")goto exit;
    }
    if (query_role_customer(id) == 1) {
        interface_admin(spots);
    } else if (query_role_customer(id) == 0) {
        interface_member(spots, c);
    } else {
        cout << "ERROR ROLE_INVALID!";
    }
    exit:;
}

void interface_member(vector<Park_spot>& spots, Customer& c) {
    __label__ begin, exit;
    cout << "\nLogged in succesfully!\n";
    
    begin:
    cout <<"Hello! "<<c.name<< ", please select an option:\n[1]Parking\n[2]Monthly report\n"
    "[3]Edit information\n[4]Exit\n";
    int option;
    cin >> option;
    cin.ignore(10000, '\n');
    switch (option) {
    case 1: {
        park(c, spots);
        break;
    }
    case 2: {
        cout << "Has not been implemented yet\n";
        break;
    }

    case 3: {
        cout << "Has not been implemented yet\n";
        break;
    }

    case 4: {
        cout << "Exiting...\n";
        goto exit;
        break;
    }

    default:
        break;
    }
    goto begin;
    exit:;
}

void interface_admin(vector<Park_spot>& spots) {
    __label__ begin,exit;
    begin:
    cout << "\nWelcome to the admin interface\n";
    cout << "\n[1] Reports & analytics";
    cout << "\n[2] Parking spots";
    cout << "\n[3] Make new user";
    cout << "\n[4] Exit";
    cout << "\nEnter option number: ";
    int option;
    cin >> option;
    cin.ignore(10000, '\n');
    switch (option) {
    case 1: {
        cout << "[1] See monthly report of ALL parking spots\n";
        cout << "[2] See weekly report of ALL parking spots\n";
        cout << "[3] See monthly report of a specific parking spot\n";
        cout << "[4] See weekly report of a specific parking spot\n";
        cout << "[5] Return\n";
        cout << "Enter option number: ";
        int option_1;
        cin >> option_1;
        cin.ignore(10000, '\n');
        switch (option_1) {
        case 1: {
            report_all_spots();
            break;
        }
        case 2: {
            report_all_spots(true);
            break;
        }
        case 3: {
            report_single_spot();
            break;
        }
        case 4: {
            report_single_spot(true);
            break;
        }
        case 5:{
            goto begin;
            break;
        }
        break;
        }
    }
    case 2: {
        cout << "[1] See current status of parking spots\n";
        cout << "[2] Make new parking spot\n";
        cout << "[3] Return\n";
        cout << "Enter option number: ";
        int option_2;
        cin >> option_2;
        cin.ignore(10000, '\n');
        switch (option_2) {
        case 1: {
            current_status_parkspots(spots);
            break;
        }
        case 2: {
            new_parkspot(spots);
            break;
        }
        case 3:{
            goto begin;
            break;
        }
        default:
            break;
        }
        break;
    }
    case 3: {
        cout << "[1] Make new customer\n";
        cout << "[2] Make new admin\n";
        cout << "[3] Return\n";
        cout << "Enter option number: ";
        int option_3;
        cin >> option_3;
        cin.ignore(10000, '\n');
        switch (option_3) {
        case 1: {
            new_customer();
            break;
        }
        case 2: {
            new_admin();
            break;
        }
        case 3:{
            goto begin;
            break;
        }
        default:
            break;
        }
        case 4: {goto exit;break;}
        break;
    }

    default:
        break;
    }
    goto begin;
    exit:;
}

// --------- individual things.

void park(Customer& c, vector<Park_spot>& spots) {
    __label__ exit;
    cout << "You have selected parking option.\n";
    if (!(c.parked())) {
        cout << "The following spots fit your vehicle and are available: \n";
        for (Park_spot i : spots) {
            if ((i.v_type == c.vehicle)&(i.taken==false)) {
                cout << i.id << ", ";
            }
        }

        cout << "\nWhere do you want to park? Or type [0] to exit.";
        int parkID;
        cin >> parkID;
        if (!parkID)goto exit;
        cin.ignore(10000, '\n');
        for (Park_spot& i : spots) {
            if (i.id == parkID) {
                if (verify())
                {
                i.clock(c);
                cout << "You have parked sucessfully!";
                }
            }
        }

    } else {
        cout << "You are parked at spot " << c.parked_at()
             << ", do you want to clock out?\n[1] Yes\n[2] No";
        int answer = 0;
        cin >> answer;
        cin.ignore(10000, '\n');
        if (answer) {
            query_parkspot_with_id(c.parked_at(), spots).clock(c);
            cout << "You have sucessfully clocked out.";
        } else {
            cout << "OK, have a nice day."; // exit to customer login
        }
    }
    exit:;
}

void new_customer() {
    int vtype;
    string name;
    string password;
    string telephone;
    int role = 0;
    cout << "\nWhat's the name of the customer? ";
    std::getline(cin, name);
    cout << "\nWhat's the vehicle type? \n[1]Twowheeler\n[2] Fourwheeler\n";
    cin >> vtype;
    cin.ignore(10000, '\n');
    cout << "What's the telephone number? +";
    std::getline(cin, telephone);
    cout << "\nWhat's the password? ";
    std::getline(cin, password);
    Customer newcustomer{name, password, Vehicle_type(vtype), telephone, role};
    cout << "\nNew customer sucessfully created\n";
    if (verify())newcustomer.update_db();
}

void new_admin() {
    int vtype = 2; // revision required! Needs to be set to NULL
    string name;
    string password;
    string telephone;
    int role = 1;
    cout << "\nWhat's the name of the admin? ";
    std::getline(cin, name);
    cout << "\nWhat's the telephone number? +";
    std::getline(cin, telephone);
    cout << "\nWhat's the password?";
    std::getline(cin, password);
    Customer newcustomer{name, password, Vehicle_type(vtype), telephone, role};
    cout << "\nNew customer sucessfully created\n";
    if(verify())newcustomer.update_db();
}

void new_parkspot(vector<Park_spot>& spots) {
    cout << "What type of parking spot? \n[1] Twowheeler\n[2] Fourwheeler\n";
    int vtype;
    cin >> vtype;
    cin.ignore(10000, '\n');
    Park_spot newspot{Vehicle_type(vtype)};
    if(verify()){spots.push_back(newspot);
    cout << "New parking spot sucessfully created.\n";}
}

// time stuff-----------------------------------------------------

pair<int, int> create_month_period() {
    std::time_t t = std::time(0);
    std::tm* date = std::localtime(&t);
    int month, year = 0;
    cout << "Which month do you want a report on?[6 2018 for June 2018]\n";
    cin >> month >> year;
    date->tm_year = year - 1900;
    date->tm_mday = 1;
    date->tm_mon = month - 1;
    pair<int, int> period;
    period.first = mktime(date);
    date->tm_mon = month;
    period.second = mktime(date);
    return period;
}

pair<int, int> create_week_period() {
    std::time_t t = std::time(0);
    std::tm* date = std::localtime(&t);
    int day, month, year = 0;
    cout << "Which month do you want a report on?[ 20 6 2018 for June 20th, 2018]\n";
    cin >> day >> month >> year;
    date->tm_year = year - 1900;
    date->tm_mday = day;
    date->tm_mon = month - 1;
    date->tm_hour = 0;
    date->tm_min = 0;
    pair<int, int> period;
    period.first = mktime(date);
    period.second = period.first + 604800; // plus 7 days in seconds.
    return period;
}

// ------------------------------ report stuff

void report_all_spots(bool weekly) {
    pair<int, int> period;
    if (weekly) {
        period = create_week_period(); // remove the pair<int, int>
    } else {
        period = create_month_period(); // ^
    }

    cout << "working timeperiods: " << period.first << ", " << period.second; // DEBUG

    reports_from_allparkspots(period); // TODO: namechange of reports_from_allparkspots in  query?
}

void report_single_spot(bool weekly) {
    cout << "Which parking spot would you like a report on?\n";
    cout << "Parking spot ID: ";
    int spotID;
    cin >> spotID;
    cin.ignore(10000, '\n');
    pair<int, int> period;
    if (weekly) {
        period = create_week_period(); // remove the pair<int, int>
    } else {
         period = create_month_period();
    }
    reports_from_parkspot(spotID, period);
}

void report_customer(int customerID, bool weekly) {
    // use report_customer(0) to make interactive
    // so admin can call the interactive version, but customer can only call
    // report_customer(own_cid)
    if (!customerID) {
        cout << "What customer do you want a report on? ID: ";
        cin >> customerID;
    }
    pair<int, int> period;
    if (weekly) {
        pair<int, int> period = create_week_period();
    } else {
        pair<int, int> period = create_month_period();
    }
    reports_from_customer(customerID, period);
}

bool verify(void)
{
string ver;
std::cout<<"\nAre you sure you want to commit these actions?"
	"\n[No] Revert."
	"\n[Yes] Commit.";
std::cin>>ver;
if (ver=="YES"|ver=="Yes"|ver=="yes") return true;
else return false;

}