#include <iostream>
#include <vector>

using namespace std;

#include "person.h"

void display_list_of_friends(vector<person> & p) {
    for(unsigned int i = 0; i < p.size(); i++) {
        cout << p[i].name << endl;
        cout << "\t" << p[i].fav_food << endl;
        cout << "\tlist of friends: " << endl;
        for(unsigned int j = 0; j < p[i].friends.size(); j++) {
            cout << "\t\t" << p[i].friends[j] << endl;;
        }
    }
}


int get_person(vector<person> & p, string name) {
    int position = -1;
    for(unsigned int i = 0; i < p.size(); i++) {
        if(p[i].name == name) {
            position = i;
            break;
        }
    }
    return position;
}

int main() {
    vector<person> people;
    string selection;
    while(1) {
        cout << "1)add person\n2)display all people\n3)add friend\n4)remove friend" << endl;
        cout << "5)modify food\n6)display friends/food" << endl;
        cin >> selection;
        if(selection[0] == '1') { //optimized
            string name, food;
            cout << "what is the persons name followed by favorite food on a new line " << endl;
            cin.ignore();
            getline(cin, name);
            getline(cin, food);
            person p = person(name, food);
            int p1 = get_person(people, p.name);
            if(p1 == -1) {
                people.push_back(p);
            }
        }
        else if(selection[0] == '2') { //optimized
            display_list_of_friends(people);
        }
        else if(selection[0] == '3') { //optimized
            cout << "which relationship do you want to start (enter two names, seperated by newlines)?" << endl;
            string first, second;
            cin.ignore();
            getline(cin, first);
            getline(cin, second);
            int p1 = get_person(people, first);
            int p2 = get_person(people, second);
            if(p1 != -1 and p2 != -1 and first!= second) {
                people[p1].add_friend(second);
                people[p2].add_friend(first);
            }
        }
        else if(selection[0] == '4') { //optimized
            cout << "which relationship do you want to end (enter two names, seperated by newlines)?" << endl;
            string first, second;
            cin.ignore();
            getline(cin, first);
            getline(cin, second);
            int p1 = get_person(people, first);
            int p2 = get_person(people, second);
            if (p1 != -1 and p2 != -1) {
                people[p1].delete_friend(people[p2].name);
                people[p2].delete_friend(people[p1].name);
            }
        }
        else if(selection[0] == '5') { //optimized
            string name;
            cout << "what is the name of the person to whom you would like to modify fav food?" << endl;
            cin.ignore();
            getline(cin, name);
            int p1 = get_person(people, name);
            if(p1 != -1) {
                string new_food;
                cout << "what food do you want to add?" << endl;
                getline(cin, new_food);
                people[p1].modify_food(new_food);
            }
        }
        else if(selection[0] == '6') {
            cout << "What is the name of the person to whom you would like to view friends with same food?" << endl;
            string name;
            cin.ignore();
            getline(cin, name);
            int p1 = get_person(people, name);
            if(p1 != -1) {
                people[p1].display_person(people);
            }
        }
        else if(selection[0] == 'q') {
            break;
        }
    }
}
