#include <iostream>
#include <stdexcept>

#include "date.h"

using namespace std;

bool Date::est_valide()
{
	bool annee_valide = true;
	bool mois_valide = (mois >= 1) && (mois <= 12);
	bool jour_valide = (jour >= 1) && (jour <= max_jour(annee, mois));

	return annee_valide && mois_valide && jour_valide;
}

Date::Date(int annee, int mois, int jour)
{
	this->annee = annee;
	this->mois = mois;
	this->jour = jour;

	if(!est_valide())
		throw invalid_argument("date invalide");
}
	
void Date::afficher() const
{
	cout << annee << "/" << mois << "/" << jour << endl;
}

int Date::get_annee() const
{
	return annee;
}

int Date::get_mois() const
{
	return mois;
}

int Date::get_jour() const
{
	return jour;
}
	
void Date::rajouter_annee()
{
	annee++;
}

void Date::rajouter_mois()
{
	mois++;

	if(mois > 12)
	{
		mois = 1;
		rajouter_annee();
	}
}

void Date::rajouter_jour()
{
	jour++;

	if(jour > max_jour(annee, mois))
	{
		jour = 1;
		rajouter_mois();
	}
}

int max_jour(int annee, int mois)
{
	int ret = -1;

	switch(mois)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			ret = 31;
			break;

		case 4:
		case 6:
		case 9:
		case 11:
			ret = 30;
			break;

		case 2:
			if(annee_bissextile(annee))
				ret = 29;
			else
				ret = 28;
			break;
	}

	return ret;
}

bool annee_bissextile(int annee)
{
	return (annee % 4 == 0 && annee % 100 != 0) 
		|| (annee % 400 == 0);
}