#include "main.h"

TUserInfo CustomUserInfo;

void OnAutorizationSend(boost::shared_ptr<TClientSocket> c)
{/*
	c->UserInfo.PosX = 37.443195f;
	c->UserInfo.PosY = 55.888869f;
	c->UserInfo.LastName = std::string("Bot");
	c->UserInfo.FirstName = std::string("Mobile");
	c->UserInfo.MiddleName = std::string("Idle");
	c->UserInfo.PhoneNumber = std::string("+75551234567");
	c->UserInfo.Uik = 1;
	c->UserInfo.MapState = 0;
	c->UserInfo.Status = 1;
	*/
	c->SendUserinfoUpdateThreaded();
}



int main(int argc, char *argv[])
{

#ifndef _WIN32
	daemonize();
#endif

	srand(static_cast<unsigned int>(time(0)));


	try
	{
		std::string fileName;

		if (argc < 2)
		{
			fileName = "botdata.txt";
		}
		else
		{
			fileName = std::string(argv[1]);

			if (fileName == "")
			{
				fileName = "botdata.txt";
			}
		}

		std::cout<<"File name is - '"<<fileName<<"'"<<std::endl;

		std::ifstream f;

		f.open(fileName.c_str());

		if (f.is_open())
		{
			std::cout<<"File found"<<std::endl;

			f>>CustomUserInfo.LastName;
			f>>CustomUserInfo.FirstName;
			f>>CustomUserInfo.MiddleName;
			f>>CustomUserInfo.PhoneNumber;
			f>>CustomUserInfo.PosX;
			f>>CustomUserInfo.PosY;
			f>>CustomUserInfo.MapState;
			f>>CustomUserInfo.Uik;
			f>>CustomUserInfo.Status;
		
			f.close();

		}
		else
		{
			std::cout<<"File not found, loading default data :("<<std::endl;

		}


		boost::asio::io_service io_service;

		boost::shared_ptr<TClientSocket> c(new TClientSocket(io_service));

		c->UserInfo = CustomUserInfo;

		c->Open("k-observer.org", "1984");

		boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

		c->ReceiveAutorizationSentSignal.connect(boost::bind(&OnAutorizationSend, c));
		
		t.join();
	}
	catch (std::exception& e)
	{
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 0;
}