//
//
// 20210425, hl_dispatch 和 hl_dispatch_B的版本进行同步, 此时调度管理的功能全部完成.后续可能还需要日志记录.
//
//test 2022 10 26


#include <iostream>
#include "./error_code/error_code.h"
//#include "LogFiles.h"
#include <glog/logging.h>
#include <string>
#include <iostream>

#include "./communication/communication_socket_base.h"

#include "./tool/thread_pool.h"
#include "./system/system_communication.h"
#include "./system/system_executor.h"
#include "./rabbitmq/rabbitmq_base.h"

#include "./dispatch/dispatch_manager.h"
#include "./dispatch/dispatch_communication.h"
#include "./dispatch/dispatch_coordinates.h"
#include "./dispatch/database_controller.h"

#include "./tool/common_data.h"
#include "./tool/time_tool.h"

#include "./rabbitmq/ttt.h"

#include <algorithm>    // std::for_each
#include <google/protobuf/text_format.h>

//#include <iostream>
//#include <mysqlx/xdevapi.h>
//
//using namespace mysqlx;

GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
{
	time_t tt;
	time( &tt );
	tt = tt + 8*3600;  // transform the time zone
	tm* t= gmtime( &tt );
	char buf[255]={0};
	sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
			t->tm_year + 1900,
			t->tm_mon + 1,
			t->tm_mday,
			t->tm_hour,
			t->tm_min,
			t->tm_sec);

	FILE* tp_file=fopen(buf,"w");
	fprintf(tp_file,data,strlen(data));
	fclose(tp_file);

}


#include "./network_communication/network_base.h"
#include "./dispatch/dispatch_network.h"


int main(int argc,char* argv[])
{
//	Error_manager e = Dispatch_network::get_instance_references().network_init(1);
//	std::cout << " huli test :::: " << " e = " << e << std::endl;
//
//	while ( 1 )
//	{
//
//	}
//
//
//	return 0;





	Error_manager t_error;

	const char* logPath = "./";
	google::InitGoogleLogging("LidarMeasurement");
	google::SetStderrLogging(google::INFO);
	google::SetLogDestination(0, logPath);
	google::SetLogFilenameExtension("zxlog");
	google::InstallFailureSignalHandler();
	google::InstallFailureWriter(&shut_down_logging);
	FLAGS_colorlogtostderr = true;        // Set log color
	FLAGS_logbufsecs = 0;                // Set log output speed(s)
	FLAGS_max_log_size = 1024;            // Set max log file size(GB)
	FLAGS_stop_logging_if_full_disk = true;


//#define MAIN_TEST 1
#ifdef MAIN_TEST

	int t_id = 0;
	if ( argc == 2 )
	{
		t_id = stoi(argv[1]);
	}

//	t_error = Dispatch_coordinates::get_instance_references().dispatch_coordinates_init();
//	std::cout << " huli test :::: " << " Dispatch_coordinates::get_instance_references().dispatch_coordinates_init() = " << t_error.to_string() << std::endl;

	t_error = Dispatch_communication::get_instance_references().communication_init(t_id);
	std::cout << " huli test :::: " << " Dispatch_communication::get_instance_references().communication_init() = " << t_error.to_string() << std::endl;
	std::cout << "Dispatch_communication = " << Dispatch_communication::get_instance_references().get_status() << std::endl;

	t_error = Dispatch_manager::get_instance_references().dispatch_manager_init(t_id);
	std::cout << " huli test :::: " << " Dispatch_manager::get_instance_references().dispatch_manager_init(0) = " << t_error.to_string() << std::endl;
	std::cout << "Dispatch_manager = " << Dispatch_manager::get_instance_references().get_dispatch_manager_status() << std::endl;

	System_executor::get_instance_references().system_executor_init(10);
	std::cout << "System_executor = " << System_executor::get_instance_references().get_system_executor_status() << std::endl;
	if ( argc == 2 )
	{
		System_communication::get_instance_references().communication_init(t_id);
	}
	else
	{
		System_communication::get_instance_references().communication_init();
	}
	System_communication::get_instance_references().set_encapsulate_cycle_time(1000);

//20210922
	while (1)
	{
		std::string test_string;
		std::cin >> test_string ;

		if ( test_string == "9" )
		{

			message::Dispatch_request_msg dispatch_request_msg;
			dispatch_request_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_request_msg);
			dispatch_request_msg.mutable_base_info()->set_timeout_ms(5000000);
			dispatch_request_msg.mutable_base_info()->set_sender(message::Communicator::eMain);
			dispatch_request_msg.mutable_base_info()->set_receiver(message::Communicator::eDispatch_manager);
			dispatch_request_msg.set_command_key("12345678901234567890123456789012");

			dispatch_request_msg.set_dispatch_motion_direction(message::Dispatch_motion_direction::E_STORE_CAR);
			dispatch_request_msg.mutable_id_struct()->set_terminal_id(4);
			dispatch_request_msg.mutable_id_struct()->set_unit_id(2);
			message::Locate_information t_locate_information;
			t_locate_information.set_locate_x(-1.901);
			t_locate_information.set_locate_y(-5.829);
			t_locate_information.set_locate_angle(2);
			t_locate_information.set_locate_length(4.78);
			t_locate_information.set_locate_width(1.88);
			t_locate_information.set_locate_height(1.65);
			t_locate_information.set_locate_wheel_base(2.78);
			t_locate_information.set_locate_wheel_width(1.88);
			t_locate_information.set_locate_front_theta(0);

			t_locate_information.set_uniformed_car_x(0);
			t_locate_information.set_uniformed_car_y(0);
			t_locate_information.set_locate_correct(true);
			dispatch_request_msg.mutable_locate_information()->CopyFrom(t_locate_information);

			message::Parkspace_info *tp_parkspace_info = dispatch_request_msg.mutable_parkspace_info_ex()->Add();
			tp_parkspace_info->set_parkingspace_index_id(167);
			tp_parkspace_info->set_parkingspace_type(message::Parkspace_type::MID_PARKINGSPACE);
			tp_parkspace_info->set_parkingspace_unit_id(3);
			tp_parkspace_info->set_parkingspace_label_id(11);
			tp_parkspace_info->set_parkingspace_room_id(5);
			tp_parkspace_info->set_parkingspace_direction(message::Direction::eBackward);
			tp_parkspace_info->set_parkingspace_floor_id(3);
			tp_parkspace_info->set_parkingspace_width(0);
			tp_parkspace_info->set_parkingspace_height(0);
			tp_parkspace_info->set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
			tp_parkspace_info->set_car_type(message::Car_type::MID_CAR);
			dispatch_request_msg.set_car_type(message::Car_type::MID_CAR);

			Dispatch_manager::get_instance_references().execute_for_dispatch_request_msg(dispatch_request_msg);

		}

		std::this_thread::sleep_for(std::chrono::seconds(1));
	}
	return 0;
#endif


	int t_dispatch_id = 1;
//	std::cin >> t_dispatch_id ;
	if ( argc == 2 )
	{
		std::cout << " huli test :::: " << " argv[1] = " << argv[1] << std::endl;
		t_dispatch_id = atoi(argv[1]);
	}
	std::cout << "+++++++++++++huli test :::: " << " t_dispatch_id = " << t_dispatch_id << std::endl;

	Database_controller::get_instance_references().database_controller_init();
	std::cout << "++++++++++++++++++++ Database_controller = " << Database_controller::get_instance_references().get_database_controller_status() << std::endl;


	Dispatch_manager::get_instance_references().dispatch_manager_init(t_dispatch_id);
	std::cout << "++++++++++++++++++++Dispatch_manager = " << Dispatch_manager::get_instance_references().get_dispatch_manager_status() << std::endl;

	Dispatch_communication::get_instance_references().communication_init(t_dispatch_id);
	std::cout << "+++++++++++++Dispatch_manager = " << Dispatch_manager::get_instance_references().get_dispatch_manager_status() << std::endl;

	System_executor::get_instance_references().system_executor_init(8);
	std::cout << "++++++++++++++++System_executor = " << System_executor::get_instance_references().get_system_executor_status() << std::endl;

	t_error = System_communication::get_instance_references().communication_init(t_dispatch_id);
	std::cout << "++++++++++++++++++huli test System_communication:::: " << " t_error = " << t_error << std::endl;

	t_error = Dispatch_network::get_instance_references().network_init(t_dispatch_id);
	std::cout << "++++++++++++++++++huli test System_communication:::: " << " t_error = " << t_error << std::endl;

	while ( 1 )
	{
	    std::this_thread::sleep_for(std::chrono::seconds(1));

	}




	char ch ;
	std::cin >> ch ;

	System_communication::get_instance_references().rabbitmq_uninit();
	System_executor::get_instance_references().system_executor_uninit();
	Dispatch_communication::get_instance_references().communication_uninit();
	Dispatch_manager::get_instance_references().dispatch_manager_uninit();


	return 0;
}