/*
File name: funcs.cpp
Author: Bo Bayles
E-mail address: bmb3h6@umr.edu
Description: This is an implementation file containing the function
  definitions used by hw7.cpp.
*/

#include "hw8.h"
#include <iostream>
#include <fstream>
using namespace std;
//Include the necessary headers.

char DisplayMenu()
{
  char cMenu;

  cout << endl << "\t \t MENU" << endl
       << "\t 1) Load Flights" << endl
       << "\t 2) Display Flights (sorted by city of departure)" << endl
       << "\t 3) Display Flights (sorted by city of destination)" << endl
       << "\t 4) Display Flights (sorted by price per mile)" << endl
       << "\t 5) Find direct flight" << endl
       << "\t 6) Quit" << endl;

  do
  {
    cout << "Your choice (1 - 6): ";
    cin >> cMenu;
    cin.ignore(1);
    //Kill the \n cin leaves in the input stream.

    if ((cMenu > '6') || (cMenu <= '0'))
      cout << "Invalid entry!" << endl;
  }
  while ((cMenu > '6') || (cMenu <= '0'));

  return cMenu;
}
//Display the menu, then get the user's choice, making sure they don't
//  enter anything too crazy.

void AddFlight(Flight flights[], int & iFlights, istream &inFile)
{
  while(!inFile.eof())
  {
    //cout << endl << "Enter the flight number: ";
    inFile >> flights[iFlights].flightNumber;
    inFile.ignore(2);
    //Kill the comma and the newline left in the input stream.

    //cout << "Enter the name of the airline: ";
    inFile.getline(flights[iFlights].airlineName, 50, ',');
    inFile.ignore(1);
    //Kill the comma left in the input stream.

    //cout << "Enter the city of departure: ";
    inFile.getline(flights[iFlights].cityDepart, 50, ',');
    inFile.ignore(1);

    //cout << "Enter the destination city: ";
    inFile.getline(flights[iFlights].cityDest, 50, ',');
    inFile.ignore(1);

    //cout << "Enter the price of the flight: ";
    inFile >> flights[iFlights].fPrice;
    inFile.ignore(1);

    //cout << "Enter the flight distance: ";
    inFile >> flights[iFlights].fDistance;
    inFile.ignore(1);
  
   if (inFile.eof())
     break;
   //Break out of the loop and don't incrememnt if the EOF is reached.
  
    iFlights++;
  }
  //Loop if the file is not at the end.
  
  return;
}

void DispFlights(const Flight flights[], const int iFlights, ostream &outFile)
{
  if (iFlights == 0)
  {
    outFile << endl << "No flights available." << endl;
  }
  //Let the user know if there's nothing there.

  for (int i = 0; i < iFlights; i++)
  {
    outFile << endl << (i+1)<< ")"
            << " Flight Number: \t \t" << flights[i].flightNumber << endl
            << "   Airline: \t \t \t" << flights[i].airlineName << endl
            << "   City of Departure: \t \t" << flights[i].cityDepart << endl
            << "   City of Destination: \t" << flights[i].cityDest << endl
            << "   Ticket price:  \t \t" << flights[i].fPrice << endl
            << "   Travel distance: \t \t" << flights[i].fDistance;
  }

  return;
}
//Display each member of each struct that has data to whichever output
//  stream is specified by the calling function.

int findSmallest(const Flight flights[], const int iSize, const int iStart,
                 const int iSortWhat)
{
  int smallestPos = -1;
  int iCompare = 0;

  if (iStart < iSize)
  {
    smallestPos = iStart;
    for (int i = (iStart + 1); i < iSize; i++)
    {
      switch (iSortWhat)
      {
        case 2:
              iCompare = strcmp(flights[i].cityDepart,
                                flights[smallestPos].cityDepart);
              break;
        case 3:
              iCompare = strcmp(flights[i].cityDest,
                                flights[smallestPos].cityDest);
              break;
        case 4:
              if ( (flights[i].fPrice / flights[i].fDistance) <
              (flights[smallestPos].fPrice / flights[smallestPos].fDistance) )
                smallestPos = i;
              break;
      }

      if (iCompare < 0)
        smallestPos = i;
    }
  }

  return smallestPos;
}
//Find the smallest value based on what the user selected. Return the
//  smallest position.

void swap(Flight flights[], const int iSize, const int iPos1, const int iPos2)
{
  Flight temp;

  if (iPos1 < iSize && iPos2 < iSize && iPos2 >= 0 && iPos2 >= 0)
  {
    temp = flights[iPos1];
    flights[iPos1] = flights[iPos2];
    flights[iPos2] = temp;
  }

  return;
}
//Swap the structs that need swapping for the sort.

void selectSort(Flight flights[], const int iSize, const int iSortWhat)
{
  int smallestPos;

  for (int i = 0; i < iSize; i++)
  {
    smallestPos = findSmallest(flights,iSize, i, iSortWhat);
    swap(flights, iSize, i, smallestPos);
  }

  return;
}
//Call the smallestPos and swap functions, passing on how the user
//  wants to sort.

void findDirect(const Flight flights[], const int iFlights)
{
  Flight temp;
  bool bDepartMatch = false;
  bool bDestMatch = false;
  int iMatchPos = 0;

  cout << endl << "Enter the city of departure: ";
  cin.getline(temp.cityDepart, 50);

  cout << "Enter the destination city: ";
  cin.getline(temp.cityDest, 50);

  for (int i = 0; i < iFlights; i++)
  {
    if ( strcmp(temp.cityDepart, flights[i].cityDepart) == 0 )
      bDepartMatch = true;
    else
      bDepartMatch = false;

    if ( strcmp(temp.cityDest, flights[i].cityDest) == 0 )
      bDestMatch = true;
    else
      bDestMatch = false;

    if( (bDepartMatch == true) && (bDestMatch == true) )
    {
      iMatchPos = i;
      break;
    }
  }

  if( (bDepartMatch == true) && (bDestMatch == true) )
  {
    cout << endl << "\t \t Match found: " << endl
        << "Flight Number: \t \t \t" << flights[iMatchPos].flightNumber << endl
        << "Airline: \t \t \t" << flights[iMatchPos].airlineName << endl
        << "City of Departure: \t \t" << flights[iMatchPos].cityDepart << endl
        << "City of Destination: \t \t" << flights[iMatchPos].cityDest << endl
        << "Ticket price: \t \t \t" << flights[iMatchPos].fPrice << endl
        << "Travel distance: \t \t" << flights[iMatchPos].fDistance;
  }
  else
    cout << endl << "No flights match." << endl;

  return;
}
//Get the user's choice for source and destination, then compare those with
//  the structs in the array. If there's a match, display it.

