#include <iostream>
#include <string.h>
#include "Phonebook.class.hpp"
#include "Contact.class.hpp"
#include <iomanip>


void	ft_search(Phonebook *Phonebook) {
	int			cpt;
	std::string	index;

	cpt = 0;
	std::cout << "     Index|First Nam.| Last Name|  Nickname" << std::endl;
	while (cpt < Phonebook->nb) {
		std::cout << std::setw(10) << cpt << "|";
		if (Phonebook->contact[cpt].first_name.length() > 10)
			std::cout << std::setw(9) << Phonebook->contact[cpt].first_name.substr(0, 9) << ".|";
		else
			std::cout << std::setw(10) << Phonebook->contact[cpt].first_name << "|" ;
		if (Phonebook->contact[cpt].last_name.length() > 10)
			std::cout << std::setw(9) << Phonebook->contact[cpt].last_name.substr(0, 9) << ".|";
		else
			std::cout << std::setw(10) << Phonebook->contact[cpt].last_name << "|" ;
		if (Phonebook->contact[cpt].nickname.length() > 10)
			std::cout << std::setw(9) << Phonebook->contact[cpt].nickname.substr(0, 9) << ".|";
		else
			std::cout << std::setw(10) << Phonebook->contact[cpt].nickname << "|" ;
		std::cout << std::endl;
		cpt++;
	}
	std::cout << "which contact would you like to check ? (1-8) : "; std::getline(std::cin, index);
	if (std::stoi(index) < 8)
		Phonebook->contact[std::stoi(index)].getContact();
}

void	ft_add(Phonebook *Phonebook) {
	std::string	first_name;
	std::string	last_name;
	std::string	nickname;
	std::string	number;
	std::string secret;

	std::cout << "Enter First name : "; std::getline(std::cin, first_name);
	std::cout << "Enter Last name : "; std::getline(std::cin, last_name);
	std::cout << "Enter Nickname : "; std::getline(std::cin, nickname);
	std::cout << "Enter Number : "; std::getline(std::cin, number);
	std::cout << "Enter Secret : "; std::getline(std::cin, secret);

	Phonebook->contact[Phonebook->cpt % 8].setContact(secret, first_name, last_name, nickname, number);
	Phonebook->cpt++;
	Phonebook->nb = Phonebook->nb == 8 ? 8 : Phonebook->nb + 1;
}

int main(void) {

	std::string	cmd;;
	Phonebook	phonebook;

	while (true) {
		std::cout << "enter a command : ";
		std::getline(std::cin, cmd);
		if (cmd == "ADD")
			ft_add(&phonebook);
		else if (cmd == "SEARCH")
			ft_search(&phonebook);
		else if (cmd == "EXIT")
			break ;
	}
	return 0;
}
