#include "accountManager.h"

AccountManager::AccountManager(std::string pathToFile, std::string pathToAccountNumberFile)
    : pathToAccountNumberFile(pathToAccountNumberFile)
{
    std::ifstream ifs(pathToFile);
    if(!ifs.is_open()) {
        if(writeDefault(pathToFile) != 0)
            exit(12);
        ifs.open(pathToFile);
        if(!ifs.is_open()) {
            std::cout << " => Error: Konnte Account File nicht öffnen" << std::endl;
            exit(13);
        }
    }


    std::string line;
    while (std::getline(ifs, line)) {
        if(line.length() > 0 && line[0] == '#')
            continue;
        Account account;
        size_t Delimeter = line.find("/");

        if(Delimeter == std::string::npos) {
            account.Email = line;
            account.Password = line;
        } else {
            account.Email = std::string(line).erase(Delimeter, line.length() - Delimeter);
            account.Password = line.erase(0, Delimeter + 1);
        }
        if(account.Email == "" || account.Password == "")
            continue;
        else
            accounts.push_back(account);
    }

    ifs.close();

}

Account AccountManager::getNextAccount()
{
    if(accounts.size() == 0) {
        std::cout << " => Error: Keine Accounts vorhanden." << std::endl;
        exit(36);
    }
    size_t accountNumber = getLastAccountNumber();
    ++accountNumber;

    if( accountNumber >= accounts.size() )
        accountNumber=0;
    if(setLastAccountNumber(accountNumber) != 0)
        exit(45);

    return accounts.at(accountNumber);
}

int AccountManager::writeDefault(std::string path)
{
    std::ofstream ofs(path);
    if(!ofs.is_open()) {
        perror((std::string(" => Error: Konnte Account Datei nicht öffnen: ") + path).c_str());
        return -1;
    }
    std::cout << " => Erstelle Datei mit Accounts unter: " << path << "..." <<std::endl;
    ofs << "# <- Am Anfang der Zeile deaktiviert diese." << std::endl;
    ofs << "#Email/Passwort - Falls diese gleich sind, geht auch nur Email oder Passwort" << std::endl;
    {
    ofs << "EntzueckendPackenderSkorpion@oida.icu" << std::endl;
    ofs << "HumorvollWundervollerDelfin@spam.care/HumorvollWundervollerDelfin@spam.care" << std::endl;
    ofs << "ExzellentPackenderHirsch@muell.icu/ExzellentPackenderHirsch@muell.icu" << std::endl;
    ofs << "GrandiosLeuchtenderPanda@papierkorb.me" << std::endl;
    ofs << "AttraktivPositiverSteinbock@magspam.net" << std::endl;
    ofs << "FreundlichKostbarerSteinbock@spam.care" << std::endl;
    ofs << "FabelhaftSelbstsichererUhu@spam.care" << std::endl;
    ofs << "HumorvollSinnlicherRabe@muell.icu" << std::endl;
    ofs << "AnspruchsvollTraumhafterPavian@ultra.fyi" << std::endl;
    ofs << "FaszinierendRiskanterLuchs@magspam.net" << std::endl;
    ofs << "BeruehmtMystischerPanda@ultra.fyi" << std::endl;
    ofs << "CharmantRasanterLoewe@existiert.net" << std::endl;
    ofs << "BedeutendLuesternerAal@existiert.net" << std::endl;
    ofs << "ElegantSelbstsichererAdler@ultra.fyi" << std::endl;
    ofs << "FaszinierendKoeniglicherBaer@spam.care" << std::endl;
    ofs << "CharmantKostbarerLoewe@magspam.net" << std::endl;
    ofs << "BedeutendPrallerSkorpion@muellemail.com" << std::endl;
    ofs << "FreundlichPerfekterRochen@muellmail.com" << std::endl;
    ofs << "AusgelassenKostbarerBasilisk@magspam.net" << std::endl;
    ofs << "FabelhaftLebhafterSchakal@ultra.fyi" << std::endl;
    ofs << "ExzellentSchoenerSteinbock@existiert.net" << std::endl;
    ofs << "ErhellendLustvollerAdler@muell.icu" << std::endl;
    ofs << "ExzellentIdealerSchakal@muell.icu" << std::endl;
    ofs << "BrillantVitalerSalamander@spam.care" << std::endl;
    ofs << "BezauberndRiesigerSchakal@oida.icu" << std::endl;
    ofs << "DynamischWarmerBaer@magspam.net" << std::endl;
    ofs << "EmotionalLeidenschaftlicherBaer@oida.icu" << std::endl;
    ofs << "BewundernswertWarmerSchwan@oida.icu" << std::endl;
    ofs << "HuebschUnfassbarerAal@papierkorb.me" << std::endl;
    ofs << "FroehlichWundervollerBiber@muellmail.com" << std::endl;
    ofs << "BefreiendWilderDelfin@spam.care" << std::endl;
    ofs << "FreundlichStilvollerTiger@oida.icu" << std::endl;
    ofs << "EchtKoeniglicherStorch@existiert.net" << std::endl;
    ofs << "BefluegeltPrallerSchwan@spam.care" << std::endl;
    ofs << "ErhellendSeltenerSeeloewe@ultra.fyi" << std::endl;
    ofs << "FreundlichZuverlaessigerSchwan@spam.care" << std::endl;
    ofs << "HervorragendWundervollerReiher@oida.icu" << std::endl;
    ofs << "AnspruchsvollStilvollerUhu@magspam.net" << std::endl;
    ofs << "AnmutigTraumhafterHai@muellemail.com" << std::endl;
    ofs << "AtemberaubendIdealerTapir@ultra.fyi" << std::endl;
    ofs << "GeheimLeuchtenderLuchs@muellemail.com" << std::endl;
    ofs << "DuftendKlarerTapir@muellmail.com" << std::endl;
    ofs << "GeheimUnwiderstehlicherRabe@muell.icu" << std::endl;
    ofs << "ExtravagantSpektakulaererAdler@spam.care" << std::endl;
    ofs << "BildlichWarmerLoewe@oida.icu" << std::endl;
    ofs << "AktivKlarerHai@spam.care" << std::endl;
    ofs << "AktivKostbarerElch@oida.icu" << std::endl;
    ofs << "Strust1991@jourrapide.com" << std::endl;
    ofs << "Natchoughe1928@dayrep.com" << std::endl;
    ofs << "zweirot@einrot.com" << std::endl;
    ofs << "AktivMagischerFrosch@existiert.net" << std::endl;
    ofs << "ErhellendPerfekterDachs@existiert.net" << std::endl;
    ofs << "ojfvwj.fgf@existiert.net" << std::endl;
    ofs << "lusiouae@wegwerfemail.de" << std::endl;
    ofs << "fraiwrou@wegwerfemail.de" << std::endl;
    ofs << "driotean@wegwerfemail.de" << std::endl;
    ofs << "tiaprour@wegwerfemail.de" << std::endl;
    ofs << "FeurigWarmerPapagei@existiert.net" << std::endl;
    ofs << "FrischStilvollerPavian@magspam.net" << std::endl;
    ofs << "FeurigZaghafterLoewe@muellmail.com" << std::endl;
    ofs << "GigantischKoeniglicherSchwan@magspam.net" << std::endl;
    ofs << "FreizuegigPersoenlicherLemming@spam.care" << std::endl;
    ofs << "BelebtIntelligenterHusky@papierkorb.me" << std::endl;
    ofs << "FreundlichMaximalerSpecht@muellmail.com" << std::endl;
    ofs << "FreundlichStarkerHirsch@ultra.fyi" << std::endl;
    ofs << "ErstklassigRasanterElch@oida.icu" << std::endl;
    ofs << "FreundlichStarkerPavian@ultra.fyi" << std::endl;
    ofs << "EinfuehlsamRiskanterPapagei@oida.icu" << std::endl;
    ofs << "FantastischPositiverHirsch@existiert.net" << std::endl;
    ofs << "GeborgenIndividuellerOtter@muell.icu" << std::endl;
    ofs << "HervorragendSensibelerWal@oida.icu" << std::endl;
    ofs << "HerrlichIndividuellerPelikan@spam.care" << std::endl;
    ofs << "HerrlichLeuchtenderTapir@muellmail.com" << std::endl;
    ofs << "ErstaunlichIntensiverEsel@magspam.net" << std::endl;
    ofs << "ExtravagantPraechtigerWal@muellemail.com" << std::endl;
    ofs << "GravierendWundervollerFalke@spam.care" << std::endl;
    ofs << "EntzueckendLebhafterFlamingo@magspam.net" << std::endl;
    ofs << "GeheimSprachloserStorch@existiert.net" << std::endl;
    ofs << "GravierendVitalerOtter@spam.care" << std::endl;
    ofs << "GrenzenlosInteressanterElch@oida.icu" << std::endl;
    ofs << "HellPackenderGoldfisch@muellemail.com" << std::endl;
    ofs << "AbenteuerlichSelbstloserFalke@muell.icu" << std::endl;
    ofs << "EmotionalZaghafterPapagei@magspam.net" << std::endl;
    ofs << "HellLiebenswerterElefant@muellemail.com" << std::endl;
    ofs << "BildlichImponierenderEsel@muellemail.com" << std::endl;
    ofs << "ErstaunlichSprachloserPapagei@ultra.fyi" << std::endl;
    ofs << "BerauschendLebendigerDelfin@ultra.fyi" << std::endl;
    ofs << "GravierendIntensiverHase@oida.icu" << std::endl;
    ofs << "BerauschendUnglaublicherGorilla@oida.icu" << std::endl;
    ofs << "GenuesslichPraechtigerDachs@spam.care" << std::endl;
    }
    ofs.close();
    return 0;
}

size_t AccountManager::getLastAccountNumber()
{
    std::ifstream fStream;
    fStream.open(pathToAccountNumberFile);
    if(!fStream.is_open()) {
        std::ofstream ofs;
        ofs.open(pathToAccountNumberFile);
        ofs << "0" << std::endl;
        ofs.close();
        fStream.open(pathToAccountNumberFile);
        if(!fStream.is_open()) {
            perror(" => Error: Konnte Account Number Datei nicht erstellen");
            exit(34);
        }
    }

    std::string content( (std::istreambuf_iterator<char>(fStream) ), (std::istreambuf_iterator<char>() ) );
    return static_cast<size_t>( atoi(content.c_str()) );
}

int AccountManager::setLastAccountNumber(size_t number)
{
    std::ofstream ofs;
    ofs.open(pathToAccountNumberFile, std::ios::trunc);
    if(!ofs.is_open()) {
        std::cout << " => Error: Account Number Datei ist nicht geöffnet." << std::endl;
        return 110;
    }
    //fStream.clear();
    ofs << number << std::endl;
    return 0;
}

