#include <iostream>

#include <vector>

#include <new>

#include <string>

#include <fstream>

#include <sstream>

using namespace std;

int authorization(string username,string password)

{

    ifstream file("password.txt");

    if(!file.is_open())

    {

        cerr << "FAILED TO OPEN FILE" <<  endl;

    }

    string s_username,s_password;

    while(file >> s_username >> s_password)

    {

        if(s_username == username && s_password == password)

        {

            cout << "Login Succesfull" <<  endl;

            return 0;

        }          

    }

    cout  << "Invalid credentials" << endl;

            return 1;

}

string a_lowercase(string str)

{

    for (char &c : str)

    {

        c = tolower(c); 

    }

    return str;

}

string a_uppercase(string str)

{

    for (char &c : str)

    {

        c = toupper(c); 

    }

    return str;

}

void save_record()

{

    ofstream outfile("s_record.txt",ios::app);

    if(!outfile)

    {

        cerr << "FAILED TO OPEN FILE" <<  endl;

        return ;

    }

    outfile.seekp(0,ios::end);

    if(outfile.tellp() == 0)

    {

        outfile << "STUDENT NAME\tMAT NO\tCOS201\tCOS212\tMATH231\tGST201\tGPA\n";

        outfile << "----------------------------------------------------------\n";

    }

    string name,matric;

    int cos1,cos2,mth,gst;

    float gpa;

    cout << "\nEnter Student Name\t";

    cin.ignore();

    getline(cin,name);

    name = a_uppercase(name);

    cout << "\nEnter Student's Matric Number\t";

    cin.ignore();

    getline(cin, matric);

    cout << "\nGrades\nCOS 201\t";

    cin  >> cos1;

    cout << "\nCOS 212\t";

    cin  >> cos2;

    cout << "\nMATH231\t";

    cin  >> mth;

    cout << "\nGST201\t";

    cin  >> gst;

    cout << "\nGPA\t";

    cin  >> gpa;

    

    outfile << name << "\t" << matric << "\t" << cos1 << "\t" << cos2 << "\t" << mth << "\t" << gst << "\t" << gpa << "\t";

    outfile.close();

    cout << "DATA ENTERED SUCCESSFULLY\n" << endl;

}

void search_record(string search)

{

    ifstream infile("s_record.txt");

    

    if(!infile)

    {

        cerr << "FAILED TO OPEN FILE" <<  endl;

        return ;

    }

    string line;

    bool found = false;

    while(getline(infile,line))

    {

        if(line.find("STUDENT NAME") != string::npos || line.find("--------") != string::npos)

        {

            continue;

        }

        string name,matric,line;

        int cos1,cos2,mth,gst;

        float gpa;

        stringstream iss(line);

        getline(iss,name,'\t');

        getline(iss,matric,'\t');

        iss >> cos1 >> cos2 >> mth >> gst >> gpa;

        if(search == matric)

        {

            cout << "\nSTUDENT FOUND: \n";

            cout << "Name: " << name << endl;

            cout << "Matric Number: " << matric << endl;

            cout << "COS 201: " << cos1 << endl;

            cout << "COS 212: " << cos2 << endl;

            cout << "MATH 231: " << mth << endl;

            cout << "GST 201: " << gst << endl;

            cout << "GPA: " << gpa << endl;

            found = true;

            break;

        }

    }

    if(!found)

        {

            cout << "NO AVAILABLE MATRIC NUMBER FOUND\n" << endl;

        }

    infile.close();  

}

int main()

{

    int begin;

    int authenticator;

    string username,password,input;

    bool valid = false;

    string search;

    cout << "        PAU RESULT MANAGEMNT SYSTEM         "  << endl;

    

    do

    {

    cout << "Do You Want TO Enter Your Credentials ?(1/2)\n";

    cin  >> begin;

       if(begin == 1)

       {

          do

          {

            cout << "Enter username : ";

            cin  >> username;

            cout << "Enter password for "  << username << ":";

            cin  >> password;

            authenticator = authorization(username,password);

          }while(authenticator != 0);

          

          if(authenticator == 0)

          {

            do

            {

                cout << "\tMAIN MENU\nTO SEARCH FOR STUDENTS RESULT ENTER Q\nTO UPDATE A STUDENTS RESULT ENTER U\nTO SAVE A STUDENT RESULT TO A FILE ENTER P\nTO VIEW ALL RESULT ENTER A\nTO EXIT THE PROGRAM ENTER E\nENTER AN OPTION\n";

                cin  >> input;

                if(input == "p")

                {

                    cout << "PRINT RESULT FILE" << endl;

                    save_record();

                }

                else if(input == "q" || input == "Q")

                {

                    cout << "SEARCH RECORD" << endl;

                    cout << "ENTE STUDENTS MATRIC NUMBER\n";

                    cin.ignore();

                    getline(cin, search);

                    search_record(search);

                }

                else if(input == "e")

                {

                    break;

                }

            }while(input != "e");

          }

       }

       else if(begin == 2)

       {

       cout << "Program is coming to an end " << endl;

       }

       else

       {

       cout << "Wrong character entered\nEnter the proper character" << endl;

       }

    }while(begin != 2);

    

    return 0;

}