//
// Készítette: Toldi Balázs Ádám
// Dátum:  2020. 04. 07.
//

#include "Naptar.h"

void Naptar::exportCSV(const char* path) {
  std::ofstream of(path);
  for(iterator it = begin(); it != end();++it){
    of << *(it) << ';' << it->getMegjegyzes() <<std::endl;           
  }
  of.close();
}

Naptar::Naptar(Datum* n) {
    db = 1;
    napok = new Datum*[1];
    napok[0] = n;
}

Naptar::Naptar() {
    db = 0;
    napok = NULL;
}

Naptar::Naptar(const char* filename){

}

Naptar::~Naptar() {
    urit();
}

size_t Naptar::size() const {
    return db;
}

bool Naptar::foglalt(const Datum nap) {
    for (iterator it = begin();it != end();++it) {
        if (*it == nap) return true;
    }
    return false;
}

bool Naptar::foglalt(const int ev, const int honap, const int nap) {
    return foglalt(Datum(ev, honap, nap));
}

void Naptar::hozzaad(Datum* n) {
    if (foglalt(*n)) throw "Ez a nap már foglalt!";
    Datum **np = new Datum*[db + 1];
    if(napok != NULL){
      
      for (size_t i = 0; i < db; ++i) {
            np[i] = napok[i];
        }
        delete[] napok;
    }
    np[db++] = n;
    napok = np;
    std::sort(napok, napok + db);
}

void Naptar::hozzaad(int ev, int honap, int nap) {
    hozzaad(new Datum(ev, honap, nap));
}

void Naptar::felszabadit(const Datum &nap) {
    bool van = false;
    Datum **np = new Datum*[db - 1];
    for (size_t i = 0,j=0; i<db;++i){
        if (*napok[i] == nap) {
	  van = true;
	  delete napok[i];
	  continue;
        }
        np[j++] = napok[i];
    }
    if(van){
      delete[] napok;
      db--;
      napok = np;
    }else{
      delete[] np;
    }
}

void Naptar::felszabadit(int ev, int honap, int nap) {
    felszabadit(Datum(ev, honap, nap));
}

void Naptar::urit() {
  for(size_t i = 0; i < db;++i){
    delete napok[i];
  }
  if(napok != NULL)
    delete[] napok;
  napok = NULL;
  db = 0;
}

Naptar::iterator Naptar::begin() {
    return iterator(*this);
}

Naptar::iterator Naptar::end() {
    return iterator(*this, db);
}

Naptar::iterator Naptar::at(size_t id) {
    if (id > db) throw std::out_of_range("at()");
    return Naptar::iterator(*this, id);
}

std::ostream &operator<<(std::ostream &os, const Datum &d) {
    return os << d.getEv() << "." << d.getHonap() << "." << d.getNap() << ".";
}

std::ostream &operator<<(std::ostream &os, Naptar &np) {
    for (Naptar::iterator mp = np.begin(); mp != np.end(); ++mp) {
        std::cout << *mp << std::endl;
    }
    return os;
}

Datum& Naptar::iterator::operator*() const {
    if (akt == 0 || akt == veg)
        throw std::out_of_range("*iterator");
    return **akt;
}

Naptar::iterator Naptar::iterator::operator++(int) {
    iterator tmp = *this;
    if (akt == 0 || akt == veg)
        throw std::out_of_range("++iterator");
    akt++;
    return tmp;
}

Naptar::iterator Naptar::iterator::operator++() {
    if (akt == 0 || akt == veg)
        throw std::out_of_range("iterator++");
    akt++;
    return *this;
}

Datum* Naptar::iterator::operator->() {
    if (akt == 0 || akt == veg)
        throw std::out_of_range("iterator++");
    return *akt;
}

bool Naptar::iterator::operator==(const Naptar::iterator &it) {
    return akt == it.akt;
}

bool Naptar::iterator::operator!=(const Naptar::iterator &it) {
    return akt != it.akt;
}

int Naptar::iterator::operator-(const iterator &it) {
    return akt - it.akt;
}

void Naptar::printTable(int ev,std::ostream& os,char spacer){
  for (iterator it = begin();it != end() && it->getEv() <= ev;++it){
    if(it->getEv() != ev) continue;
    os << *(it) << spacer << it->getMegjegyzes() <<std::endl;
  }
}

void Naptar::printTable(int ev, int honap,std::ostream& os,char spacer){ 
  for (iterator it = begin();it != end() && it->getEv() <= ev;++it){
    if(it->getEv() != ev && it->getHonap() != honap) continue;
    os << *(it) << spacer << it->getMegjegyzes() <<std::endl;
  }
}
