#include <iostream>
#include <cstring>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include "polje_lista.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;
using namespace polje_lista;

static string izvor = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

bool dodajIgru(lista &P){
	string str;
	igra I;
	for(int i = 0; i < 8; i++) I.sifra[i] = izvor[rand()%62];
    I.sifra[8] = '\0';
	cout << "Naziv: ";
	cin.ignore();
	cin.getline(I.naziv, 29);
	cout << "Proizvodac: ";
	cin.getline(I.proizvodac, 29);
	cout << "Zanr: "; 
	cin.getline(I.zanr, 19);
	cout << "Podzanr: ";
	cin.getline(I.podzanr, 19);
	do {
		cout << "PEGI: ";
		cin.getline(I.pegi, 2);
	} while (strcmp(I.pegi, "3") && strcmp(I.pegi, "7") && strcmp(I.pegi, "12")  && strcmp(I.pegi, "16")  && strcmp(I.pegi, "18")  && cout << "Pogresan unos!\n");
	
	insertL(I, 0, P);  	
	return true;
}

bool brisiSifra (lista &P, char sifra[9]){
	for(int i = 0; i < endL(P); i++) {
		if (!strcmp(P.popis[i].sifra,sifra)) {
		cout << "Brisem: " << P.popis[i].naziv << endl;
		deleteL(i, P);
		return true;
		}
	}
	cout << "===Sifra nije nadena!==="<< endl;
	return false;
}
bool brisiPEGI (lista &P, char pegi[3]){
	bool flag = false;
	for(int i = 0; i < endL(P); i++) {
		if (!strcmp(P.popis[i].pegi,pegi)) {
		cout << "Brisem: " << P.popis[i].naziv << endl;
		deleteL(i, P);
		flag = true;
		}
	}
	if(!flag) cout << "===Sifra nije nadena!===" << endl;
	return flag;
}

bool binarno(lista &P, char kljuc[30], int n, int i=0) {
	int brojac = 0;
	int k = (i+n)/2;
	if(i>=n) return false;
	if(!strcmp(P.popis[k].zanr, kljuc)) {
	    cout << "===Pronadeno!===" << endl;
        cout << "Sifra: " << P.popis[k].sifra << endl;
		cout << "Naziv: " << P.popis[k].naziv << endl;
		cout << "Zanr: " << P.popis[k].zanr << endl;
		cout << "Podzanr: " << P.popis[k].podzanr << endl;
		cout << "PEGI: " << P.popis[k].pegi << endl;
		cout << "----------------------------" << endl;
		brojac++;
		return brojac;
	}
	else if(strcmp(P.popis[k].zanr, kljuc) > 0)
		return binarno(P, kljuc, k, i);
	else if(strcmp(P.popis[k].zanr, kljuc) < 0)
		return binarno(P, kljuc, n, k+1);
}

void spoji(lista &P,int i,int k,int j) {
     int I=i, J=k+1, K=0;
     igra *B = new igra [j-i+1];
     while (I<=k && J<=j)
        if (strcmp(P.popis[I].sifra, P.popis[J].sifra) < 0)  //A[I]<=A[J]
           B[K++]=P.popis[I++];
        else
		   B[K++]=P.popis[J++];
  		if (I>k)
 	       while (J<=j)
              B[K++] = P.popis[J++];
    	else
        	while (I<=k)
               B[K++] = P.popis[I++];
     	for (int I=0;I<=j-i;I++) 
        	P.popis[i+I]=B[I];
	delete []B;
}

void mSort(lista &P,int i, int j) {
     if (i<j) {
        int k=(i+j)/2;
        mSort(P,i,k);
        mSort(P,k+1,j);
        spoji(P,i,k,j);
     }
}

void spojiZaB(lista &P,int i,int k,int j) {
     int I=i, J=k+1, K=0;
     igra *B = new igra [j-i+1];
     while (I<=k && J<=j)
        if (strcmp(P.popis[I].zanr, P.popis[J].zanr) < 0)  //A[I]<=A[J]
           B[K++]=P.popis[I++];
        else
		   B[K++]=P.popis[J++];
  		if (I>k)
 	       while (J<=j)
              B[K++] = P.popis[J++];
    	else
        	while (I<=k)
               B[K++] = P.popis[I++];
     	for (int I=0;I<=j-i;I++) 
        	P.popis[i+I]=B[I];
	delete []B;
}

void mSortZaB(lista &P,int i, int j) {
     if (i<j) {
        int k=(i+j)/2;
        mSortZaB(P,i,k);
        mSortZaB(P,k+1,j);
        spojiZaB(P,i,k,j);
     }
}



int main() {
	srand(time(0));
	lista P;
	initL(P);
	igra I;
	ifstream dat("igre.txt");
    string str; 
    
    while (true){
    	if(dat.eof()) break;
    	for(int i = 0; i < 8; i++) I.sifra[i] = izvor[rand()%62];
        getline(dat, str);
        strcpy(I.naziv, str.c_str());
        getline(dat, str);
       	strcpy(I.proizvodac, str.c_str());
        getline(dat, str);
        strcpy(I.zanr, str.c_str());
        getline(dat, str);
        strcpy(I.podzanr, str.c_str());
        getline(dat, str);
        strcpy(I.pegi, str.c_str());
        insertL(I, 0, P);    
        
    }
    dat.close();
    dat.clear();
    
    int iz;
    
    do {
    	cout << "=====GLAVNI IZBORNIK=====" << endl;
    	cout << "1. Ispis svih igara" << endl;
    	cout << "2. Unos igre u evidenciju" << endl;
    	cout << "3. Sortiranje po sifri" << endl;
    	cout << "4. Binarno pretrazivanje po zanru" << endl;
    	cout << "5. Brisanje po sifri" << endl;
    	cout << "6. Brisanje po PEGI oznaci" << endl;
    	cout << "0. Izlaz" << endl;
    	cout << "=========================" << endl;
    	cin >> iz;
    	switch(iz){
    		case 1: {
    			igra *d = NULL;
    			for (int i = 0; i < endL(P); i++){
					d = retreiveL(i, P);
					cout << "Sifra: " << d->sifra << endl;
					cout << "Naziv: " << d->naziv << endl;
					cout << "Zanr: " << d->zanr << endl;
					cout << "Podzanr: " << d->podzanr << endl;
					cout << "PEGI: " << d->pegi << endl;
					cout << "----------------------------" << endl;	
    			}
    			break;
		}
    		case 2:
    			dodajIgru(P);    			
    			break;
    		case 3:{
    			mSort(P, 0, 17);
    			igra *d = NULL;
    			for (int i = 0; i < endL(P); i++){
					d = retreiveL(i, P);
					cout << "Sifra: " << d->sifra << endl;
					cout << "Naziv: " << d->naziv << endl;
					cout << "Zanr: " << d->zanr << endl;
					cout << "Podzanr: " << d->podzanr << endl;
					cout << "PEGI: " << d->pegi << endl;
					cout << "----------------------------" << endl;	
    			}
    			break;
    		}
    		case 4:{
    			mSortZaB(P, 0, 17);
    			char zanr[30];
    			cout << "Upisi zanr: " << endl;
    			cin >> zanr;
    			int i = binarno(P, zanr, 18);  
    			cout << "Pronadeno igara: " << i << endl;  	    			
    			break;
    		}
		
			case 5:{
			    char sifra[9];
    			cout << "Upisi sifru: " << endl;
    			cin >> sifra;
    			brisiSifra(P, sifra);				
				break;
			}
			case 6:{
				char pegi[3];
    			cout << "Upisi pegi: " << endl;
    			cin >> pegi;
			    brisiPEGI(P, pegi);
				break;
			}
	
    	}
    	
    } while (iz != 0);
    
	return 0;
}