// THIS IS THE PROVIDED CODE FOR DSA, PROGRAM #3 (originally Fall 2026)

#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

// A simple class; each object holds two public fields
class Data {
public:
  unsigned int u;
  string s;
};

// Load the data from a specified input file
void loadDataList(list<Data *> &l, const string &filename) {

  ifstream input(filename);
  if (!input) {
    cerr << "Error: could not open " << filename << "\n";
    exit(1);
  }

  // The first line indicates the size
  string line;
  getline(input, line);
  stringstream ss(line);
  int size;
  ss >> size;

  // Load the data
  for (int i = 0; i < size; i++) {
    getline(input, line);
    stringstream ss2(line);
    Data *pData = new Data();
    ss2 >> pData->u >> pData->s;
    l.push_back(pData);
  }

  input.close();
}

// Output the data to a specified output file
void writeDataList(const list<Data *> &l, const string &filename) {

  ofstream output(filename);
  if (!output) {
    cerr << "Error: could not open " << filename << "\n";
    exit(1);
  }

  // Write the size first
  int size = l.size();
  output << size << "\n";

  // Write the data
  for (auto pData:l) {
    output << pData->u << " " 
	   << pData->s << "\n";
  }

  output.close();
}

// Sort the data according to a specified field
// (The implementation of this function will be later in this file)
void sortDataList(list<Data *> &, int);

// The main function calls routines to load the data, sort the data,
// and output the data. The sort is timed according to CPU time.
int main() {

  // Get the input filename from the user and load the data
  string filename;
  cout << "Enter name of input file: ";
  cin >> filename;
  list<Data *> theList;
  loadDataList(theList, filename);
  cout << "Data loaded.\n";

  // Ask about the criteria
  cout << "Which criterion is being used for the sort (1-4)? ";
    int criterion = 0;
  cin >> criterion;
  if (criterion < 1 || criterion > 4) {
    cerr << "Invalid criterion\n";
    exit(1);
  }
  
  // Sort the data, and output the CPU time used by the sort
  cout << "Executing sort...\n";
  clock_t t1 = clock();
  sortDataList(theList, criterion);
  clock_t t2 = clock();
  double timeDiff = ((double) (t2 - t1)) / CLOCKS_PER_SEC;
  cout << "Sort finished. CPU time was " << timeDiff << " seconds.\n";

  // Get the output filename from the user and write the sorted data
  cout << "Enter name of output file: ";
  cin >> filename;
  writeDataList(theList, filename);

  return 0;
}

// -------------------------------------------------
// YOU MAY NOT MODIFY OR ADD ANY CODE ABOVE HERE !!!
// -------------------------------------------------

// You may add global variables, functions, and/or class defintions
// here if you wish.

void sortDataList(list<Data *> &l, int criteria) {
  // Fill this in

}
