#include <fstream>
#include <string>
#include <cstdlib>

#include "utilitaires.h"
#include "type_def.h"

using namespace std;

// ajouter srand(TIME(NULL)) dans le main
Personne genererPersonne(string* listNom, string* listPrenom, int nbNom, int nbPrenom)
{
    char sexe[1], annee[2], moisNaiss[2], depart[2], commune[3], etatCiv[3];
    sprintf(sexe, "%d", rand()%2 + 1);
    sprintf(annee,"%02d",rand()%99+1);
    sprintf(moisNaiss,"%02d",rand()%12+1);
    sprintf(depart,"%02d",rand()%99+1);
    sprintf(commune,"%03d",rand()%999+1);
    sprintf(etatCiv,"%03d",rand()%999+1);

    string num = string(sexe)+string(annee)+string(moisNaiss)+string(depart)+string(commune)+string(etatCiv);

    Personne palea;
    palea.nom = listNom[rand()%nbNom];
    palea.prenom = listPrenom[rand()%nbPrenom];
    palea.num = num;

    return palea;
}


int initTabNomPrenom(vectNom tab, string fichier)
{
    // Ouvrir un flux
    ifstream monFlux(fichier.c_str());

    // Données
    int nb;
    monFlux >> nb;

    for (int k = 0; k < nb; ++k)
    {
        monFlux >> tab[k];
    }

    // Fin
    return nb;
}

void genererRepertoire(vectPersonne tab, std::string* listNom, std::string* listPrenom, int nbNom, int nbPrenom)
{
    // Générer
    for (int k = 0; k < 10000; ++k)
    {
        tab[k] = genererPersonne(listNom, listPrenom, nbNom, nbPrenom);
    }

    // Ecrire
    ofstream monFlux("repertoire.txt");

    for (int k = 0; k < 10000; ++k)
    {
        monFlux << tab[k].nom << ' ' << tab[k].prenom << ' ' << tab[k].num << endl;
    }
}

bool egalitePersonne(Personne a, Personne b)
{
    return (a.nom == b.nom) && (a.prenom == b.prenom) && (a.num == b.num);
}

bool comparerPersonne(Personne a, Personne b)
{
	// Cas nom différent
    if (a.nom != b.nom)
    {
        return a.nom < b.nom;
    }
    // Cas nom identique
    else
    {
        // Cas prenm different
        if (a.prenom != b.prenom)
        {
            return a.prenom < b.prenom;
        }
        // Cas prenom identique
        else
        {
		// Numero de securite sociale forcement different
            return a.num < b.num;
        }
    }
}