#include <iostream>
#include <cstring>
#include <conio.h>
#include <time.h>  
//#include "l_polj.h"
#include "l_pok.h"

using namespace std;
igre f[1000];
 static const char abc[] =
"ABCDEFGHI0123456789JKLMNOPQRSTUVWXYZ"
"0123456789"
"abcdefghij0123456789klmnopqrstuvwxyz";
int stL = sizeof(abc) - 1;
struct igre2{
       string sifra;
       string naziv;
       string proizv;
       int zanr;
       string PEGI;
            };
            
igre2 dodavanja[18]={
     {"","Ghost Recon","Ubisoft",1,"5"},
     {"","SOCOM: U.S. Navy SEALs","Ubisoft",1,"5"},
     {"","Tom Clancy Splinter Cell","Ubisoft",1,"5"},
     {"","Ratchet And Clank","Insomniac",2,"5"},
     {"","Mass Effect","Ubisoft",2,"2"},
     {"","Jak & Daxter","Naughty Dog",2,"4"},
     {"","The Elder Scrolls","Bethesda",3,"5"},
     {"","Fallout series","Bethesda",3,"5"},
     {"","Fable","Lionhead Studios",3,"5"},
     {"","The Lord of the Rings Online: Shadows of Angma","Midway",4,"5"},
     {"","Runescape","Jagex",4,"5"},
     {"","World of Warcraft","Blizzard",4,"5"},
     {"","Empire Earth","Microprose",5,"5"},
     {"","Civilization","Microprose",5,"5"},
     {"","Galactic Civilizations","Microprose",5,"5"},
     {"","Stronghold Crusader","Firefly Studios's",6,"5"},
     {"","Red Alert 2","Westwood Studios",6,"5"},
     {"","Warcraft3","Blizzard",6,"5"},
     
     };


string sifrar(){
     string t;
     t=abc[rand() % stL];
     return t;
}


int polje(lista *l){
    int br;
     br=0;
     el pos=Firstl(l);
     while(pos!=Endl(l)){
          f[br]= retrivel(pos,l);
          br++;
          pos=Nextl(pos,l);
     }
    return br;
}
     
     
void dodaj(lista *l){
     igre igra;
     int zanrr;
     int brojac=0;
     srand(time(NULL)); 
     
      do{
        string result;
        for(int i=0;i<5;i++){
               result+=sifrar();
                }    
       igra.sifra=result;
       igra.naziv=dodavanja[brojac].naziv;
       zanrr=dodavanja[brojac].zanr;
        if(zanrr==1){
                    igra.zanr.zanr1="Shooters";
                    igra.zanr.podzanr1="TPS";
                    igra.zanr.podzanr2="";
                    }
        if(zanrr==2){
                    igra.zanr.zanr1="Shooters";
                    igra.zanr.podzanr2="Tactical Shooter";
                    igra.zanr.podzanr1="";
                    }
        if(zanrr==3){
                    igra.zanr.zanr2="RPG";
                    igra.zanr.podzanr1="MMORPG";
                    igra.zanr.podzanr2="";
                    }
        if(zanrr==4){
                    igra.zanr.zanr2="RPG";
                    igra.zanr.podzanr2="Open World";
                    igra.zanr.podzanr1="";
                    }
        if(zanrr==5){
                    igra.zanr.zanr3="strateska";
                    igra.zanr.podzanr1="RTS";
                    igra.zanr.podzanr2="";
                    }
        if(zanrr==6){
                    igra.zanr.zanr3="strateska";
                    igra.zanr.podzanr2="TBS";
                    igra.zanr.podzanr1="";
                    }
                    
         
         igra.proizv=dodavanja[brojac].proizv;
         igra.PEGI=dodavanja[brojac].PEGI;
         insertl(igra, Endl(l), l);
         brojac++;
    }while(brojac!=18);
            }
            
            
int dodaj_novo(lista *l){
    
    igre igra;
    int zanrr;
    srand(time(NULL)); 
    string result;
    for(int i=0;i<5;i++){
           result+=sifrar();
               }    
   igra.sifra=result;
     cout << "Sifra: ";
     cout<<igra.sifra<<endl;
     cout << "Naziv: ";
     cin>>igra.naziv;
     cout << "zanr, mozete odabrati izmedu: "<<endl;
     cout << "   zanr1- podzanr1(1), podzanr2(2)"<<endl;
     cout << "   zanr2- podzanr1(3), podzanr2(4)"<<endl;
     cout << "   zanr3- podzanr1(5), podzanr2(6)"<<endl;
     cin>>zanrr;
   if(zanrr==1){
                igra.zanr.zanr1="Shooters";
                igra.zanr.podzanr1="TPS";
                igra.zanr.podzanr2="";
                }
    if(zanrr==2){
                igra.zanr.zanr1="Shooters";
                igra.zanr.podzanr2="Tactical Shooter";
                igra.zanr.podzanr1="";
                }
    if(zanrr==3){
                igra.zanr.zanr2="RPG";
                igra.zanr.podzanr1="MMORPG";
                igra.zanr.podzanr2="";
                }
    if(zanrr==4){
                igra.zanr.zanr2="RPG";
                igra.zanr.podzanr2="Open World";
                igra.zanr.podzanr1="";
                }
    if(zanrr==5){
                igra.zanr.zanr3="strateska";
                igra.zanr.podzanr1="RTS";
                igra.zanr.podzanr2="";
                }
    if(zanrr==6){
                igra.zanr.zanr3="strateska";
                igra.zanr.podzanr2="TBS";
                igra.zanr.podzanr1="";
                }
     cout << "Proizvodac: ";
     cin>>igra.proizv;
     cout << "PEGI: ";
     cin>>igra.PEGI;
     insertl(igra, Endl(l), l);
 
 }  
 
        
void merge(igre *a, int low, int high, int mid)
{
    int i, j, k;
    igre c[50];
    i = low;
    k = low;
    j = mid + 1;
    while (i <= mid && j <= high)
    {
        if (a[i].sifra < a[j].sifra)
        {
            c[k] = a[i];
            k++;
            i++;
        }
        else
        {
            c[k] = a[j];
            k++;
            j++;
        }
    }
    while (i <= mid)
    {
        c[k] = a[i];
        k++;
        i++;
    }
    while (j <= high)
    {
        c[k] = a[j];
        k++;
        j++;
    }
    for (i = low; i < k; i++)
    {
        a[i] = c[i];
    }
}
void mergesort(igre *a, int low, int high){
    int mid;
    if (low < high)
    {
        mid=(low+high)/2;
        mergesort(a,low,mid);
        mergesort(a,mid+1,high);
        merge(a,low,high,mid);
    }
    return;
}


int pretrazivanje(igre *polje, int n, string strat){
    int i, nadjen=0;
    for(i=0; i<n; i++)
    {
    if (polje[i].zanr.zanr3==strat)
    {
           cout << "Sifra: " << polje[i].sifra << endl;
                 cout << "Naziv: " << polje[i].naziv << endl;
                 cout << "proizvodac: " << polje[i].proizv << endl;
                 cout << "zanr: " << polje[i].zanr.zanr3<< "."<<endl;
                 cout << "PEGI: " << polje[i].PEGI << endl;
                 cout <<"---------------"<<endl;   
                nadjen++; 
             
             }
            
    }
    return nadjen;
}


void ispis(lista *l,int br){
     
    mergesort(f,0,br-1);
    int o;
    o=0;
    while(o<br){
                 cout << "Sifra: " << f[o].sifra << endl;
                 cout << "Naziv: " << f[o].naziv << endl;
                 cout << "proizvodac: " << f[o].proizv << endl;
                 cout << "zanr: " << f[o].zanr.podzanr1<< f[o].zanr.podzanr2<< "."<<endl;
                 cout << "PEGI: " << f[o].PEGI << endl;
                 cout <<"---------------"<<endl;
                 o++;
         }

     }           
   

int brisi(lista *l){
    int f;
     lista *temp=l;
     string sifr;
     el br;
     cout<<"koju sifru da obrisem?"<<endl;
     cin>>sifr;
      br=Locatel(sifr,l);
  
      el pos=Firstl(l);
      while(pos!=Endl(l)){
       if(br==pos) {                  
       deletel(pos,l);
       break;
         }
       pos=Nextl(pos,l);
    }
    f=polje(temp);
    return f;
 }
        
                 
int brisi_peg(lista *l,int n){
     int u;
     int broj;
     broj=n;
     lista *temp=l;
     string peg;
     el br;
     string sifr;
     cout<<"koji PEGI da obrisem?"<<endl;
     cin>>peg;

     for(int i=0; i<n; i++){
        
        if (f[i].PEGI==peg)
           {
           sifr= f[i].sifra;
           i++;
           br=Locatel(sifr,l);
           el pos=Firstl(l);
           while(pos!=Endl(l)){
           if(br==pos) {                  
           deletel(pos,l);
            break;
            }

        pos=Nextl(pos,l);
       }
     u=polje(temp);
        
       }
     }   
    return u;
 }
 
                 
void ispis_zanr(lista *l,int br){
    
    string x="strateska";
    mergesort(f,0,br-1);
    cout<<"SVE STR: "<<endl;
    int d;
    d=pretrazivanje(f,br,x); 
    cout<<"Ovoliko ima strat:"<<d<<endl;

 
}
     

int main(){
    igre y;
    int dod;
    lista *l=Initl(l);
    lista *temp=l;
    dodaj(l);
    dod=polje(l);
    int izbor;
    
      
        do{
                 cout << "Za sortirani ispis igrica unesi 1"<< endl;
                 cout << "Za ispis svih strateskih igrica unesi 2"<< endl;
                 cout << "Za brisanje pomocu sifre unesi 3"<<endl;
                 cout << "Za brisanje pomocu PEGI unesi 4"<<endl;
                 cout << "Za unosenje nove igre stisnite 5" << endl;
                 cout << "Zaizlazak odaberi 9" << endl;
                 cin>>izbor;
         switch(izbor){
                       case 1:
                            ispis(l,dod);
                            break;
                       case 2:
                            ispis_zanr(l,dod);
                            break;
                       case 3:
                            dod=brisi(l);
                            break;
                        case 4:
                            dod=brisi_peg(l,dod);
                            break;
                       case 5:
                            dodaj_novo(l);
                            dod=polje(l);
                            break;
                       
                       
                       }
    }while(izbor!=9);
   

    system("pause");
    return 0;
}