/*
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 "hw7.h"
#include <iostream>
using namespace std;
//Include the necessary headers.

char DisplayMenu()
{
  char cMenu;

  cout << endl << "\t \t MENU" << endl
       << "\t 1) Add  Flight" << 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[], const int iFlights)
{
  cout << endl << "Enter the flight number: ";
  cin.getline(flights[iFlights].flightNumber, 10);
  //Get the whole line as a cstring, since the flight number might have
  //  letters, too.
  
  cout << "Enter the name of the airline: ";
  cin.getline(flights[iFlights].airlineName, 50);
  //Restrict the Airline name cstring to 50 49 characters.
  
  cout << "Enter the city of departure: ";
  cin.getline(flights[iFlights].cityDepart, 50);

  cout << "Enter the destination city: ";
  cin.getline(flights[iFlights].cityDest, 50);
  
  do
  {
    cout << "Enter the price of the flight: ";
    cin >> flights[iFlights].fPrice;
    
    if (flights[iFlights].fPrice < 0)
      cout << "Invalid entry." << endl;
  }
  while (flights[iFlights].fPrice < 0);
  
  do
  {
    cout << "Enter the flight distance: ";
    cin >> flights[iFlights].fDistance;
    
    if (flights[iFlights].fDistance < 0)
      cout << "Invalid entry." << endl;
  }
  while (flights[iFlights].fDistance < 0);
  
  return;
}
//Prompt the user for each member of the struct, making sure they
//  don't enter anything crazy for the numerical values.

void DispFlights(const Flight flights[], const int iFlights)
{
  if (iFlights == 0)
    cout << endl << "No flights available." << endl;
  //Let the user know if there's nothing there.
    
  for (int i = 0; i < iFlights; i++)
  {
    cout << 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.

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.

