#include "people.h"

people::~people() {
    for(auto i: persons) {
        delete i;
    }
}

bool people::contains(string name) {
    for(auto i: persons) {
        if(i->name == name) {
            return true;
        }
    }
    return false;
}

void people::add_person(string name, string food) {
    if(not contains(name)) {
        person * tmp = new person(name, food);
        persons.push_back(tmp);
    }
}

void people::connect(unsigned int a, unsigned int b) {
    cout << *this << endl;
    person * p1 = persons[a];
    person * p2 = persons[b];
    p1->friends.insert(p2);
    p2->friends.insert(p1);
}

void people::unfriend(unsigned int a, unsigned int b) {
    cout << *this << endl;
    person * p1 = persons[a];
    person * p2 = persons[b];

    auto p2it = p1->friends.find(p2);
    if(p2it != p1->friends.end())
        p1->friends.erase(p2it);

    auto p1it = p2->friends.find(p1);
    if(p1it != p2->friends.end())
        p2->friends.erase(p1it);
}

void people::food_update(unsigned int a, string new_food) {
    persons[a]->update_food(new_food);
}

unsigned int people::size() {
    return persons.size();
}

person * people::operator[](unsigned int i) {
    return persons[i];
}

ostream & operator<<(ostream & os, people & p) {
    os << "people (" << p.persons.size() << "):" << endl;
    for(unsigned int i=0; i < p.persons.size(); i++) {
        os << i << " : " << *p.persons[i] << endl;
    }
    return os;
}
