#include <iostream>
#include <cstring>
#include "listy.h"

using namespace std;

void listy::wypisz_liste()
{
    cout << endl << endl;
    if ( s_ptr == nullptr ) cout << "Lista jest pusta" << endl << endl << endl;
    book *temp = s_ptr;
    while( temp )
    {
        cout << endl << "Tytul: \t" << temp -> read_title() << endl;
        cout << endl << "Autor: \t" << temp -> read_imie() << " " << temp -> read_nazw() << endl;
        cout << endl << "Cena: \t" << temp -> read_cena() << " zl" << endl;
        cout << endl << "Wydawnictwo: \t" << temp -> read_wyd() << endl;
        cout << endl << "Rok wyd.: \t" << temp -> read_rok() << endl;
        temp = temp -> nast;
    }
    return;
}


void listy::dodaj_ksiazke()
{
    book *temp = s_ptr;

    if( s_ptr == nullptr )
    {
        s_ptr = new book;
        (*s_ptr).nast = nullptr;
        (*s_ptr).poprz = nullptr;
        temp = s_ptr;
    }
    else
    {
        temp = new book;
        l_ptr -> nast = temp;
        temp -> nast = nullptr;
        temp -> poprz = l_ptr;
    }
    (*temp).nowaKsiazka();
    l_ptr = temp;
    return;

}

void listy::wypisz_ksiazke()
{
    int rok;
    book *temp = s_ptr;
    cout << "Podaj rok wydania szukanej ksiazki: ";
    cin >> rok;
    cout << endl << endl;
    if ( s_ptr == nullptr )
    {
        cout << "Lista jest pusta" << endl << endl << endl;
        return;
    }
    while ( temp )
    {
        if ( temp -> read_rok() == rok )
        {
            cout << endl << "Tytul: \t" << temp -> read_title() << endl;
            cout << endl << "Autor: \t" << temp -> read_imie() << " " << temp -> read_nazw() << endl;
            cout << endl << "Cena: \t" << temp -> read_cena() << " zl" << endl;
            cout << endl << "Wydawnictwo: \t" << temp -> read_wyd() << endl;
            cout << endl << "Rok wyd.: \t" << temp -> read_rok() << endl;
            return;
        }
        temp = temp -> nast;
    }
    cout << "Nie ma takiej ksiazki na liscie." << endl;
    return;
}

void listy::usun_ksiazke()
{
    int rok;
    book *temp = s_ptr;
    cout << "Podaj rok wydania ksiazki do usuniecia: ";
    cin >> rok;
    cout << endl << endl;
    if ( s_ptr == nullptr )
    {
        cout << "Lista jest pusta" << endl << endl << endl;
        return;
    }
    while ( temp )
    {
        if ( temp -> read_rok() == rok )
        {
            if( s_ptr == temp )
            {
                s_ptr = s_ptr -> nast;
                if( s_ptr ) s_ptr -> poprz = nullptr;
                delete temp;
                return;
            }
            (*(*temp).poprz).nast = (*temp).nast;
            if ( (*temp).nast ) (*(*temp).nast).poprz = (*temp).poprz;
            delete temp;
            return;
        }
        temp = temp -> nast;
    }
     cout << "Nie ma takiej ksiazki na liscie." << endl;

}

void listy::usun_liste()
{
    book *temp_ptr = s_ptr, *t_ptr = nullptr;
    while( temp_ptr )
    {
        t_ptr = temp_ptr -> nast;
        delete temp_ptr;
        temp_ptr = t_ptr;
    }
    return;
}

void listy::sortowanie()
{
    int wybor;
    cout << "W jaki sposob chcesz sortowac liste?" << endl;
    cout << "1. Alfabetycznie wzgledem tytulu." << endl;
    cout << "2. wzgledem roku wydania." << endl;
    cin >> wybor;

    switch( wybor )
    {
    case 1:
//        sortowanie_tytul(s_ptr, l_ptr);
        break;
    case 2:
//        sortowanie_rok_wyd(s_ptr, l_ptr);
        break;
    default:
        cout << "Nie ma takiej opcji..." << endl;
    }
    return;
}

int listy::porownaj(char* a, char* b)
{
    int roznica;
    for (int i = 0; i < 1000; i++)
    {
        roznica = a[i] - b[i];
        if (roznica != 0) return roznica;
    }

    return 0;
}

void listy::sortowanie_tytul(book* l, book* p)
{
    book* temp1_ptr = nullptr, * temp2_ptr = nullptr;

    temp1_ptr = s_ptr;
    temp2_ptr = (*temp1_ptr).nast;
    for (int i = 1; i < dl; i++)
    {
        temp1_ptr = s_ptr;
        temp2_ptr = (*temp1_ptr).nast;
        for (int j = 1; j <= dl - i; j++)
        {
            if (porownaj((*temp1_ptr).read_title(), (*temp2_ptr).read_title()) > 0)
            {
                if ((*temp2_ptr).nast == nullptr)
                {
                    (*temp1_ptr).nast = nullptr;
                    l_ptr = temp1_ptr;
                }
                else
                {
                    (*temp1_ptr).nast = (*temp2_ptr).nast;
                    (*(*temp2_ptr).nast).poprz = temp1_ptr;
                }
                if ((*temp1_ptr).poprz == nullptr)
                {
                    s_ptr = temp2_ptr;
                    (*temp2_ptr).poprz = nullptr;
                }
                else
                {
                    (*(*temp1_ptr).poprz).nast = temp2_ptr;
                    (*temp2_ptr).poprz = (*temp1_ptr).poprz;
                }
                (*temp2_ptr).nast = temp1_ptr;
                (*temp1_ptr).poprz = temp2_ptr;
                temp1_ptr = temp2_ptr;
                temp2_ptr = (*temp2_ptr).nast;
            }
            temp1_ptr = temp2_ptr;
            temp2_ptr = (*temp2_ptr).nast;
        }
    }
    return;


}

void listy::sortowanie_rok_wyd(book* l, book* p)
{
    book* temp1_ptr = nullptr, * temp2_ptr = nullptr;

    temp1_ptr = s_ptr;
    temp2_ptr = (*temp1_ptr).nast;
    for (int i = 1; i < dl; i++)
    {
        temp1_ptr = s_ptr;
        temp2_ptr = (*temp1_ptr).nast;
        for (int j = 1; j <= dl - i; j++)
        {
            if ((*temp1_ptr).read_rok() < (*temp2_ptr).read_rok())
            {
                if ((*temp2_ptr).nast == nullptr)
                {
                    (*temp1_ptr).nast = nullptr;
                    l_ptr = temp1_ptr;
                }
                else
                {
                    (*temp1_ptr).nast = (*temp2_ptr).nast;
                    (*(*temp2_ptr).nast).poprz = temp1_ptr;
                }
                if ((*temp1_ptr).poprz == nullptr)
                {
                    s_ptr = temp2_ptr;
                    (*temp2_ptr).poprz = nullptr;
                }
                else
                {
                    (*(*temp1_ptr).poprz).nast = temp2_ptr;
                    (*temp2_ptr).poprz = (*temp1_ptr).poprz;
                }
                (*temp2_ptr).nast = temp1_ptr;
                (*temp1_ptr).poprz = temp2_ptr;
                temp1_ptr = temp2_ptr;
                temp2_ptr = (*temp2_ptr).nast;
            }
            temp1_ptr = temp2_ptr;
            temp2_ptr = (*temp2_ptr).nast;
        }
    }
    return;
}

