#include "CBookItems.h"


void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1)
{
	CreateNewItem(sFirstName, sLastName, sPhoneNumber1, "");
}

void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2)
{
	BookItem<string>* spaceItem = new BookItem<string>;
	spaceItem->_sFirstName = sFirstName;
	spaceItem->_sLastName = sLastName;
	spaceItem->_sPhoneNumber1 = sPhoneNumber1;
	spaceItem->_sPhoneNumber2 = sPhoneNumber2;

	_aItems.push_back(*spaceItem);

	//delete spaceItem;
}

int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber1, int nNumOfItem)
{
	SetNewValueToItem(sFirstName, sLastName, sPhoneNumber1, "", nNumOfItem);
}

int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2, int nNumOfItem)
{
	nNumOfItem--;
	if (!(nNumOfItem > _aItems.size()))
	{
		for (int i = 0; i < _aItems.size(); i++)
		{
			if (nNumOfItem == i)
			{
				BookItem<string> spaceItem;
				spaceItem._sFirstName = sFirstName;
				spaceItem._sLastName = sLastName;
				spaceItem._sPhoneNumber1 = sPhoneNumber1;
				spaceItem._sPhoneNumber2 = sPhoneNumber2;

				_aItems.at(nNumOfItem) = spaceItem;
				return 0;
			}
		}
	}
	else
	{
		return 1;
	}
}

int CBookItems::GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber1)
{
	string sPhoneNumber2;
	GetItem(nNumOfItem, sFirstName, sLastName, sPhoneNumber1, sPhoneNumber2);
}

int CBookItems::GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber1, string& sPhoneNumber2)
{
	if (!(nNumOfItem > _aItems.size()))
	{
		sFirstName = _aItems.at(nNumOfItem)._sFirstName;
		sLastName = _aItems.at(nNumOfItem)._sLastName;
		sPhoneNumber1 = _aItems.at(nNumOfItem)._sPhoneNumber1;
		sPhoneNumber2 = _aItems.at(nNumOfItem)._sPhoneNumber2;
		return 0;
	}
	else
	{
		return 1;
	}
}

const CBookItems::BookItem<string>& CBookItems::GetItem(int nNumOfItem)
{
  if (nNumOfItem < 0 || nNumOfItem > _aItems.size()) return CBookItems::BookItem<string>();
  return _aItems[nNumOfItem];
}


int CBookItems::GetSize()
{
	return _aItems.size();
}

int CBookItems::SortItems(int nSortingType)
{
	if (nSortingType == 0)
	{
		CBookItems spaceItem;
		for (int i = 0; i < _aItems.size(); i++)
		{
			for (int j = i; j < _aItems.size(); j++)
			{
				if (_aItems.at(i)._sFirstName[0] > _aItems.at(j)._sFirstName[0])
				{
					string sFirstName = "";
					string sLastName = "";
					string sPhoneNumber1 = "";
					string sPhoneNumber2 = "";

					sFirstName = _aItems.at(i)._sFirstName;
					sLastName = _aItems.at(i)._sLastName;
					sPhoneNumber1 = _aItems.at(i)._sPhoneNumber1;
					sPhoneNumber2 = _aItems.at(i)._sPhoneNumber2;

					_aItems.at(i)._sFirstName   = _aItems.at(j)._sFirstName;
					_aItems.at(i)._sLastName = _aItems.at(j)._sLastName;
					_aItems.at(i)._sPhoneNumber1 = _aItems.at(j)._sPhoneNumber1;
					_aItems.at(i)._sPhoneNumber2 = _aItems.at(j)._sPhoneNumber2;

					_aItems.at(j)._sFirstName = sFirstName;
					_aItems.at(j)._sLastName = sLastName;
					_aItems.at(j)._sPhoneNumber1 = sPhoneNumber1;
					_aItems.at(j)._sPhoneNumber2 = sPhoneNumber2;
				}	
			}		
		}
		return 0;
	}
	else if (nSortingType == 1)
	{
		CBookItems spaceItem;
		for (int i = 0; i < _aItems.size(); i++)
		{
			for (int j = i; j < _aItems.size(); j++)
			{
				if (_aItems.at(i)._sLastName[0] > _aItems.at(j)._sLastName[0])
				{
					string sFirstName = "";
					string sLastName = "";
					string sPhoneNumber1 = "";
					string sPhoneNumber2 = "";

					sFirstName = _aItems.at(i)._sFirstName;
					sLastName = _aItems.at(i)._sLastName;
					sPhoneNumber1 = _aItems.at(i)._sPhoneNumber1;
					sPhoneNumber2 = _aItems.at(i)._sPhoneNumber2;

					_aItems.at(i)._sFirstName = _aItems.at(j)._sFirstName;
					_aItems.at(i)._sLastName = _aItems.at(j)._sLastName;
					_aItems.at(i)._sPhoneNumber1 = _aItems.at(j)._sPhoneNumber1;
					_aItems.at(i)._sPhoneNumber2 = _aItems.at(j)._sPhoneNumber2;

					_aItems.at(j)._sFirstName = sFirstName;
					_aItems.at(j)._sLastName = sLastName;
					_aItems.at(j)._sPhoneNumber1 = sPhoneNumber1;
					_aItems.at(j)._sPhoneNumber2 = sPhoneNumber2;
				}
			}
		}
		return 0;
	}
	else
	{
		return 1;
	}
}

int CBookItems::FindItem(string sDesiredValue, int nSortingType, vector<int>& nNumOfDesirvedItems)
{
	nSortingType--;
	bool bCheckRightValue = false;
	if (nSortingType == 0)
	{
		for (int i = 0; i < _aItems.size(); i++)
		{
			for (int j = 0; j < sDesiredValue.size() & j < _aItems.at(i)._sFirstName.size(); j++)
			{
				if (sDesiredValue[j] == _aItems.at(i)._sFirstName[j])
				{
					bCheckRightValue = true;
				}
				else
				{
					bCheckRightValue = false;
				}
			}

			if (bCheckRightValue == true)
			{
				nNumOfDesirvedItems.push_back(i);
			}
		}
		return 0;
	}
	else if (nSortingType == 1)
	{
		for (int i = 0; i < _aItems.size(); i++)
		{
			for (int j = 0; j < sDesiredValue.size() & j < _aItems.at(i)._sLastName.size(); j++)
			{
				if (sDesiredValue[j] == _aItems.at(i)._sLastName[j])
				{
					bCheckRightValue = true;
				}
				else
				{
					bCheckRightValue = false;
				}
			}

			if (bCheckRightValue == true)
			{
				nNumOfDesirvedItems.push_back(i);
			}
		}
		return 0;
	}
	else if (nSortingType == 2)
	{
		for (int i = 0; i < _aItems.size(); i++)
		{
			string key, phone1, phone2;
			key = sDesiredValue;
			phone1 = _aItems.at(i)._sPhoneNumber1;
			phone2 = _aItems.at(i)._sPhoneNumber2;

			if (phone1 != "" && key.find(phone1) != std::string::npos
			    || phone2 != "" && key.find(phone2) != std::string::npos
			    || phone1 != "" && phone1.find(key) != std::string::npos
			    || phone2 != "" && phone2.find(key) != std::string::npos
			) {
				nNumOfDesirvedItems.push_back(i);
			}
		}
		return 0;
	}
	else
	{
		return 1;
	}

}

int CBookItems::SaveItems(string sNameOfFile)
{
	ofstream fSave;
	fSave.open(sNameOfFile);
	
	if (!fSave.is_open())
	{
		fSave.close();
		return 1;
	}
	else
	{
		for (int i = 0; i < _aItems.size(); i++)
		{
			fSave << _aItems.at(i)._sFirstName << " " << _aItems.at(i)._sLastName << " "
			      << _aItems.at(i)._sPhoneNumber1 << " " << _aItems.at(i)._sPhoneNumber2 << " " << endl;
		}
		fSave.close();
		return 0;
	}
}

int CBookItems::LoadItems(string sNameOfFile)
{
	_aItems.clear();
	ifstream fLoad;
	fLoad.open(sNameOfFile);

	if (!fLoad.is_open())
	{
		fLoad.close();
		return 1;
	}
	else
	{
		string sFirstName = "", sLastName = "", sPhoneNum1 = "", sPhoneNum2 = "";
		string sItemData = "";
		while (getline(fLoad, sItemData))
		{
			int j = 0, k = 0;
			while (sItemData.at(j) != ' ')
			{
				sFirstName += sItemData[j];
				j++, k++;
			}
			k = 0;
			j++;
			while (sItemData.at(j) != ' ')
			{
				sLastName += sItemData[j];
				j++, k++;
			}
			j++;
			while (j < sItemData.size() && sItemData[j] != '\r' && sItemData[j] != ' ')
			{
				sPhoneNum1 += sItemData[j];
				j++;
			}
			j++;
			while (j < sItemData.size() && sItemData[j] != '\r' && sItemData[j] != ' ')
			{
				sPhoneNum2 += sItemData[j];
				j++, k++;
			}
			
			CreateNewItem(sFirstName, sLastName, sPhoneNum1, sPhoneNum2);
			sFirstName = "", sLastName = "", sPhoneNum1 = "", sPhoneNum2 = "", sItemData = "";
		}
		fLoad.close();
		return 0;
	}

	
}

std::ostream& operator<<(std::ostream& s, const CBookItems::BookItem<string>& item)
{
  s << item._sFirstName << " " << item._sLastName << ": " << item._sPhoneNumber1;
  if (item._sPhoneNumber2 != "")
    s << ", " << item._sPhoneNumber2;

	return s;
}

std::istream& operator>>(std::istream& s, CBookItems::BookItem<string>& item)
{
  s >> item._sFirstName >> item._sLastName >> item._sPhoneNumber1 >> item._sPhoneNumber2;
	return s;
}