// Include statements
#include <iostream>
#include <string> // String return type functionality

// Local header files
#include "Date.h"

// Namespaces
using namespace std;

Date::Date(int d, int m, int y) : day{d}, month{m}, year{y} { // In-member initialization constructor with validation
    
    while (!setMonth(m)) {
        cout << "Value " << m << " for month is not a valid response. Please try again: ";
        cin >> m;
        setMonth(m);
    }
    while (!setDay(d)) {
        cout << "Value " << d << " for day is not a valid response. Please try again: ";
        cin >> d;
        setDay(d);
    }
    while (!setYear(y)) {
        cout << "Value " << y << " for year is not a valid response. Please try again: ";
        cin >> y;
        setYear(y);
    }

}

bool Date::setDay(int d) { // Validate input for day and set object day variable to input if valid

    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { // Handle day range for months with 31 days
        if (d >= 1 && d <= 31) {
            day = d;
            return true;
        } else {
            return false;
        }
    } else if (month == 2) { // Handle day range for February, which has 28 days
    // This validation does not handle leap years, when February has 29 days, but could easily be expanded to do so.
    // The above is not necessary for this program, since dates are restricted to 2021 and 2022 (neither of which are leap years).
        if (d >= 1 && d <= 28) {
            day = d;
            return true;
        } else {
            return false;
        }
    } else { // Handle day range for all other months, which have 30 days
        if (d >= 1 && d <= 30) {
            day = d;
            return true;
        } else {
            return false;
        }
    }

}

bool Date::setMonth(int m) { // Validate input for month and set object month variable to input if valid

    if (m >= 1 && m <= 12) {
        month = m;
        return true;
    } else {
        return false;
    }
    
}

bool Date::setYear(int y) { // Validate input for year and set object year variable to input if valid

    if (y == 2021 || y == 2022) {
        year = y;
        return true;
    } else {
        return false;
    }

}

int Date::getDay() { // Return value of day variable

    return day;

}

int Date::getMonth() { // Return value of month variable

    return month;

}

int Date::getYear() { // Return value of year variable

    return year;

}

string Date::showDate() { // Stringify an object's date

    string day_string;

    if (day < 10) { // Single-digit
        day_string = "0" + to_string(day);
    } else { // Double-digit
        day_string = to_string(day);
    }

    string month_string;

    if (month < 10) { // Single-digit
        month_string = "0" + to_string(month);
    } else { // Double-digit
        month_string = to_string(month);
    }

    string year_string = to_string(year);

    string date = month_string + "/" + day_string + "/" + year_string;

    return date;

}