#include <vector>
#include "circle.h"

ostream & operator<<(ostream & os, vector<string> strings) {
    for(unsigned int i = 0; i < strings.size(); i++)
        os << strings[i] << ", ";
    os << endl;
    return os;
}

vector<string> circle::getNames() {
    vector<string> names; 
    names.push_back("Josephus");
    names.push_back("A");
    names.push_back("B");
    names.push_back("C");
    names.push_back("D");
    names.push_back("E");
    names.push_back("F");
    names.push_back("G");
    names.push_back("H");
    names.push_back("I");
    names.push_back("J");
    names.push_back("K");
    names.push_back("L");
    names.push_back("M");
    names.push_back("N");
    names.push_back("O");
    names.push_back("P");
    names.push_back("Q");
    names.push_back("R");
    names.push_back("S");
    return names;
}
vector<string> circle::playGame(int n, int m) {
    vector<string> names = getNames();
    vector<string> temp;
    if(n < 10 || n > 20){
        names.clear();
        return names;
    }
    if(m < 1){
        names.clear();
        return names;
    }
    for(int i = 0; i < 20 - n; i++) {
        names.pop_back();
    }
    unsigned int count = 0;
    for(int i = 0; i < n; i++) {
        int hits = 0;
        while(hits < m) {
            if(names[count] == ""){
            }
            else {
                hits++;
                if(hits == m) {
                    temp.push_back(names[count]);
                    names[count] = "";
                    hits = 100;
                }
            }
            count++;
            if(count+1 > names.size()) {
                count = 0;
            }
        }
    }
    return temp;
}

int circle::reportSafeIndex(int n, int m) {
    if(m < 1) {
        return -1;
    }
    else if (n < 10 || n > 20){
        return -1;
    }
    vector<string> names = getNames();
    vector<string> kill_list = playGame(n, m);
    string safe_person_name = kill_list[kill_list.size() - 1];
    for(unsigned int i = 0; i < names.size(); i++) {
        if(safe_person_name == names[i]) {
            return i;
        }
    }
    return -1;
}
