#include "CBookItems.h"


void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber)
{
	_BookItem* spaceItem = new _BookItem;
	spaceItem->_sFirstName = sFirstName;
	spaceItem->_sLastName = sLastName;
	spaceItem->_sPhoneNumber = sPhoneNumber;

	_aItems.push_back(*spaceItem);

	//delete spaceItem;
}

int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber, int nNumOfItem)
{
	nNumOfItem--;
	if (!(nNumOfItem > _aItems.size()))
	{
		for (int i = 0; i < _aItems.size(); i++)
		{
			if (nNumOfItem == i)
			{
				_BookItem spaceItem;
				spaceItem._sFirstName = sFirstName;
				spaceItem._sLastName = sLastName;
				spaceItem._sPhoneNumber = sPhoneNumber;

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

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

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 sPhoneNumber = "";

					sFirstName = _aItems.at(i)._sFirstName;
					sLastName = _aItems.at(i)._sLastName;
					sPhoneNumber = _aItems.at(i)._sPhoneNumber;

					_aItems.at(i)._sFirstName   = _aItems.at(j)._sFirstName;
					_aItems.at(i)._sLastName = _aItems.at(j)._sLastName;
					_aItems.at(i)._sPhoneNumber = _aItems.at(j)._sPhoneNumber;

					_aItems.at(j)._sFirstName = sFirstName;
					_aItems.at(j)._sLastName = sLastName;
					_aItems.at(j)._sPhoneNumber = sPhoneNumber;
				}	
			}		
		}
		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 sPhoneNumber = "";

					sFirstName = _aItems.at(i)._sFirstName;
					sLastName = _aItems.at(i)._sLastName;
					sPhoneNumber = _aItems.at(i)._sPhoneNumber;

					_aItems.at(i)._sFirstName = _aItems.at(j)._sFirstName;
					_aItems.at(i)._sLastName = _aItems.at(j)._sLastName;
					_aItems.at(i)._sPhoneNumber = _aItems.at(j)._sPhoneNumber;

					_aItems.at(j)._sFirstName = sFirstName;
					_aItems.at(j)._sLastName = sLastName;
					_aItems.at(j)._sPhoneNumber = sPhoneNumber;
				}
			}
		}
		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++)
		{
			for (int j = 0; j < sDesiredValue.size() & j < _aItems.at(i)._sPhoneNumber.size(); j++)
			{
				if (sDesiredValue[j] == _aItems.at(i)._sPhoneNumber[j])
				{
					bCheckRightValue = true;
				}
				else
				{
					bCheckRightValue = false;
				}
			}

			if (bCheckRightValue == true)
			{
				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)._sPhoneNumber << " " << 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 = "", sPhoneNum = "";
		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++;
			}
			k = 0;
			j++;
			while (j < sItemData.size())
			{
				sPhoneNum += sItemData[j];
				j++, k++;
			}
			
			CreateNewItem(sFirstName, sLastName, sPhoneNum);
			sFirstName = "", sLastName = "", sPhoneNum = "", sItemData = "";
		}
		fLoad.close();
		return 0;
	}

	
}