#include <iostream>
#include <ctime>
#include <cstdlib>

//#include "pokazivaci.h"
#include "polje.h"

using namespace std;

bool unosOsobe(lista L) {
    ZaposlenaOsoba osoba1;
    for(int i=0; i<11; i++) osoba1.OIB[i] = rand()%10 + '0';
    osoba1.OIB[11] = '\0';
    cout << " OIB: " << osoba1.OIB << endl;
    
    cout << " Prezime: ";
    cin >> osoba1.prezime;
    cout << " Ime: ";
    cin >> osoba1.ime;
    
    cout << " Datum rodjenja (DD.MM.YYYY): ";
    cin >> osoba1.datum;
    cout << " Radno mjesto: ";
    cin >> osoba1.rad_mj;
    cout << endl;
    
    return InsertL(osoba1, EndL(L), L); }

element ListaN(int n, lista L) {
    int i = 0;
    element el = FirstL(L);
    for(int i=0; i<n; i++) {
        el = NextL(el, L); }
    return el; }
void QSort(lista L, int i1, int j1) {
    if(i1>=j1) {
        return; }
    int i=i1+1, k=i1;
    char oib[12];
    element elem1 = ListaN(k, L);
    strcpy(oib, RetreiveL(elem1, L).OIB);
    while(i<j1) {
        element el1 = ListaN(i, L);
        ZaposlenaOsoba zo1 = RetreiveL(el1, L);
        if(strcmp(zo1.OIB, oib) > 0) {
            DeleteL(el1, L);
            element zo1_el = ListaN(k, L);
            InsertL(zo1, zo1_el, L);
            k++; }
        i++; }

    QSort(L, i1, k);
    QSort(L, k+1, j1); }

void Sortiranje(lista L) {
    int n=0;
    for(element el=FirstL(L); el != EndL(L); el=NextL(el, L) ) {
        n++; }

    QSort(L, 0, n);

    element el = FirstL(L);
    while(el != EndL(L) ) {
        ZaposlenaOsoba osoba1;
        osoba1 = RetreiveL(el, L);
        cout << " OIB: " << osoba1.OIB << endl;
        cout << " Prezime: " << osoba1.prezime << endl;
        cout << " Ime: " << osoba1.ime << endl;
        cout << " Datum rodjenja: " << osoba1.datum << endl;
        cout << " Radno mjesto: " << osoba1.rad_mj << endl << endl;
        el = NextL(el, L); } }

int datumi[] = {321, 420, 521, 621, 723, 823, 923, 1024, 1122, 1222, 120, 219 };
string horo_znakovi[] = {"ovan", "bik", "blizanci", "rak", "lav", "djevica", "vaga", "skorpion", "strijelac", "jarac", "vodenjak", "ribe" };

void Pretrazivanje(lista L) {
    cout << endl << "Horoskop" << endl;
    element el = FirstL(L), ja_element = EndL(L);
    ZaposlenaOsoba ja;
    while(el != EndL(L) ) {
        ZaposlenaOsoba osoba1;
        osoba1 = RetreiveL(el, L);
        if(strcmp(osoba1.prezime, "Greguric") == 0 && strcmp(osoba1.ime, "Luka") == 0) {
            ja = osoba1;
            ja_element = LocateL(ja, L);
            break; }
        el = NextL(el, L); }
	
	if(ja_element==EndL(L)) {
		cout << "Nema mojih podataka u listi." << endl;
		return; }

    int trazeni;
    int mjesec_dan = (ja.datum[0]-'0')*10 + ja.datum[1]-'0' + (ja.datum[3]-'0')*1000 + (ja.datum[4]-'0')*100;

    for(int i=0; i<(sizeof(datumi)/sizeof(int))-1; i++) {
        if(datumi[i]<=mjesec_dan && mjesec_dan<datumi[i+1]) {
            trazeni = i;
            cout << "Moj horoskopski znak: " << horo_znakovi[i] << endl << endl;
            break; } }

    int brojac = 0;

    el = FirstL(L);
    while(el != EndL(L) ) {
        ZaposlenaOsoba osoba1;
        osoba1 = RetreiveL(el, L);
        if(el != ja_element) {
            int mjesec_dan = (osoba1.datum[0]-'0')*10 + osoba1.datum[1]-'0' + (osoba1.datum[3]-'0')*1000 + (osoba1.datum[4]-'0')*100;
            if(datumi[trazeni]<=mjesec_dan && mjesec_dan<datumi[trazeni+1]) {
                cout << " OIB: " << osoba1.OIB << endl;
                cout << " Prezime: " << osoba1.prezime << endl;
                cout << " Ime: " << osoba1.ime << endl;
                cout << " Datum rodjenja: " << osoba1.datum << endl;
                cout << " Radno mjesto: " << osoba1.rad_mj << endl << endl;
                brojac++; } }
        el = NextL(el, L); }

    cout << "Ukupan broj pronadjenih: " << brojac << endl; }

bool Brisanje1(lista L) {
    char OIB[12];
    cout << "Unesite OIB: "; cin >> OIB;
    bool testOk = 0;
    element el = FirstL(L);
    while(el != EndL(L) ) {
        ZaposlenaOsoba osoba1;
        osoba1 = RetreiveL(el, L);
        if(strcmp(osoba1.OIB, OIB) == 0) {
            testOk = DeleteL(LocateL(osoba1, L), L);
            break; }
        el = NextL(el, L); }

    return testOk; }

bool Brisanje2(lista L) {
    char rad_mj[40];
    cout << "Unesite radno mjesto: ";  cin >> rad_mj;
    bool testOk = 0;
    element el = FirstL(L);
    while(el != EndL(L) ) {
        ZaposlenaOsoba osoba1 = RetreiveL(el, L);
        if(strcmp(osoba1.rad_mj, rad_mj) == 0) {
            testOk = DeleteL(el, L);
            el = FirstL(L);
            if(testOk == 0)
                return 0; }
        else
            el = NextL(el, L); }

    return testOk; }

void m4Brisanje(lista L) {
    int brisiX;
    cout << "Odaberite:  " << endl;
    cout << "  1 - OIB" << endl;
    cout << "  2 - radno mjesto" << endl;
    cin >> brisiX;

    switch(brisiX) {
    case 1: {
        bool testOk = Brisanje1(L);
        if(testOk == 1)
            cout << "Izbrisan." << endl;
        else
            cout << "Ne postoji." << endl;
        break; }
    case 2: {
        bool testOk = Brisanje2(L);
        if(testOk == 1)
            cout << "Izbrisani." << endl;
        else
            cout << "Ne postoji." << endl;
        break; }
    default:
        break; } }

void IspisiIzbornik() {
    cout << endl << endl << "------------------" << endl;
    cout << "  1 - Unos" << endl;
    cout << "  2 - Sortiranje" << endl;
    cout << "  3 - Pretrazivanje" << endl;
    cout << "  4 - Brisanje" << endl;
    cout << "  9 - Izlaz" << endl; }

int main() {
    int un;

    srand(time(0));
    lista L = InitL(L);

    do {

        IspisiIzbornik();
        cin >> un;

        switch(un) {
        case 1: {
            bool testOk = unosOsobe(L);
            if(testOk == 1) {
                cout << "Zapis dodan." << endl; }
            else {
                cout << "Greska kod dodavanja zapisa." << endl; }
            break; }

        case 2: Sortiranje(L); break;
        case 3: Pretrazivanje(L); break;
        case 4: m4Brisanje(L); break;

        default: break; } }
    while(un != 9);

    return 0; }