#pragma once

struct MWDate {
    int day, month, year;

    MWDate(int d = 1, int m = 1, int y = 3001)
        : day(d),
        month(m),
        year(y)
    {
    }

    bool operator<(const MWDate& other) const 
    {
        if (year != other.year) {
            if (year < other.year) {
                return true;
            } else {
                return false;
            }
        }

        if (month != other.month) {
            if (month < other.month) {
                return true;
            } else {
                return false;
            }
        }

        return (day < other.day);
    }

    bool operator==(const MWDate& other) const
    {
        if (year == other.year && month == other.month && day == other.day) {
            return true;
        } else {
            return false;
        }
    }

	bool operator <=(const MWDate& other) const
	{
		if (operator<(other) || operator==(other)) {
			return true;
		} else {
			return false;
		}
	}

	BGCPString toString() const
	{
		BGCPString result = BGCPString::fromInt(month) + "/" + BGCPString::fromInt(day) + "/" +
			BGCPString::fromInt(year);
		return result;
	}

	static MWDate fromStr(const BGCPString& str)
	{
		MWDate result (1,1,3000);
		size_t pos1 = str.scan("/", 0, str.getByteCount());
		size_t clen = str.getCharSize(pos1);
		size_t pos2 = str.scan("/", pos1+clen, str.getByteCount()-pos1-clen);
		result.month = (int)str.copyPart(0, pos1).toInt();
		result.day  = (int)str.copyPart(pos1+clen, pos2-pos1-clen).toInt();
		result.year = (int)str.copyPart(pos2+clen, str.getByteCount()-pos2-clen).toInt();
		return result;
	}

	void addMonths(int monthsToAdd)
	{
		// Make sure we are on a valid day also ...
		if (day > 28) {
			day = 1;
			++month;
		}
		month += monthsToAdd;
		if (month > 12) {
			year += month / 12;
			month = month % 12;
		}
	}
};
