//
// Created by huli on 2020/9/8.
//

#include "common_data.h"

void Common_data::copy_data(Car_measure_information& car_measure_information_out, const message::Locate_information& locate_information_in)
{
	if ( locate_information_in.has_locate_x() )
	{
		car_measure_information_out.car_center_x = locate_information_in.locate_x();
	}
	else
	{
		car_measure_information_out.car_center_x = 0;
	}
	if ( locate_information_in.has_locate_y() )
	{
		car_measure_information_out.car_center_y = locate_information_in.locate_y();
	}
	else
	{
		car_measure_information_out.car_center_y = 0;
	}
	if ( locate_information_in.has_locate_angle() )
	{
		car_measure_information_out.car_angle = locate_information_in.locate_angle();
	}
	else
	{
		car_measure_information_out.car_angle = 0;
	}
	if ( locate_information_in.has_locate_length() )
	{
		car_measure_information_out.car_length = locate_information_in.locate_length();
	}
	else
	{
		car_measure_information_out.car_length = 0;
	}
	if ( locate_information_in.has_locate_width() )
	{
		car_measure_information_out.car_width = locate_information_in.locate_width();
	}
	else
	{
		car_measure_information_out.car_width = 0;
	}
	if ( locate_information_in.has_locate_height() )
	{
		car_measure_information_out.car_height = locate_information_in.locate_height();
	}
	else
	{
		car_measure_information_out.car_height = 0;
	}
	if ( locate_information_in.has_locate_wheel_base() )
	{
		car_measure_information_out.car_wheel_base = locate_information_in.locate_wheel_base();
	}
	else
	{
		car_measure_information_out.car_wheel_base = 0;
	}
	if ( locate_information_in.has_locate_wheel_width() )
	{
		car_measure_information_out.car_wheel_width = locate_information_in.locate_wheel_width();
	}
	else
	{
		car_measure_information_out.car_wheel_width = 0;
	}
	if ( locate_information_in.has_locate_front_theta() )
	{
		car_measure_information_out.car_front_theta = locate_information_in.locate_front_theta();
	}
	else
	{
		car_measure_information_out.car_front_theta = 0;
	}
	if ( locate_information_in.has_locate_correct() )
	{
		car_measure_information_out.correctness = locate_information_in.locate_correct();
	}
	else
	{
		car_measure_information_out.correctness = false;
	}
}
void Common_data::copy_data(message::Locate_information& locate_information_out, const Car_measure_information& car_measure_information_in)
{
	locate_information_out.set_locate_x(car_measure_information_in.car_center_x);
	locate_information_out.set_locate_y(car_measure_information_in.car_center_y);
	locate_information_out.set_locate_angle(car_measure_information_in.car_angle);
	locate_information_out.set_locate_length(car_measure_information_in.car_length);
	locate_information_out.set_locate_width(car_measure_information_in.car_width);
	locate_information_out.set_locate_height(car_measure_information_in.car_height);
	locate_information_out.set_locate_wheel_base(car_measure_information_in.car_wheel_base);
	locate_information_out.set_locate_wheel_width(car_measure_information_in.car_wheel_width);
	locate_information_out.set_locate_front_theta(car_measure_information_in.car_front_theta);
	locate_information_out.set_locate_correct(car_measure_information_in.correctness);
}

void Common_data::copy_data(Car_information& car_information_out, const message::Car_info& car_info_in)
{
	if ( car_info_in.has_license() )
	{
		car_information_out.license = car_info_in.license();
	}
	else
	{
		car_information_out.license.clear();
	}
	if ( car_info_in.has_car_length() )
	{
		car_information_out.car_length = car_info_in.car_length();
	}
	else
	{
		car_information_out.car_length = 0;
	}
	if ( car_info_in.has_car_width() )
	{
		car_information_out.car_width = car_info_in.car_width();
	}
	else
	{
		car_information_out.car_width = 0;
	}
	if ( car_info_in.has_car_height() )
	{
		car_information_out.car_height = car_info_in.car_height();
	}
	else
	{
		car_information_out.car_height = 0;
	}
	if ( car_info_in.has_car_wheel_base() )
	{
		car_information_out.car_wheel_base = car_info_in.car_wheel_base();
	}
	else
	{
		car_information_out.car_wheel_base = 0;
	}
	if ( car_info_in.has_car_wheel_width() )
	{
		car_information_out.car_wheel_width = car_info_in.car_wheel_width();
	}
	else
	{
		car_information_out.car_wheel_width = 0;
	}
}
void Common_data::copy_data(message::Car_info& car_info_out, const Car_information& car_information_in)
{
	car_info_out.set_license(car_information_in.license);
	car_info_out.set_car_length(car_information_in.car_length);
	car_info_out.set_car_width(car_information_in.car_width);
	car_info_out.set_car_height(car_information_in.car_height);
	car_info_out.set_car_wheel_base(car_information_in.car_wheel_base);
	car_info_out.set_car_wheel_width(car_information_in.car_wheel_width);
}

void Common_data::copy_data(Parkspace_information& parkspace_information_out, const message::Parkspace_info& parkspace_info_in)
{

		parkspace_information_out.parkingspace_index_id = parkspace_info_in.parkingspace_index_id();
		parkspace_information_out.parkingspace_type = (Common_data::Parkspace_type)parkspace_info_in.parkingspace_type();

		parkspace_information_out.parkingspace_unit_id = parkspace_info_in.parkingspace_unit_id();
		parkspace_information_out.parkingspace_floor_id = parkspace_info_in.parkingspace_floor_id();
		parkspace_information_out.parkingspace_room_id = parkspace_info_in.parkingspace_room_id();
		parkspace_information_out.parkingspace_direction = (Common_data::Direction)parkspace_info_in.parkingspace_direction();

		parkspace_information_out.parkingspace_width = parkspace_info_in.parkingspace_width();
		parkspace_information_out.parkingspace_height = parkspace_info_in.parkingspace_height();
		parkspace_information_out.parkingspace_status = (Common_data::Parkspace_status)parkspace_info_in.parkingspace_status();

		copy_data(parkspace_information_out.car_information, (message::Car_info&) parkspace_info_in.car_info());
		parkspace_information_out.car_entry_time = parkspace_info_in.entry_time();
		parkspace_information_out.car_leave_time = parkspace_info_in.leave_time();

		parkspace_information_out.parkspace_path = (Common_data::Parkspace_path)parkspace_info_in.parkspace_path();
		parkspace_information_out.path_estimate_time = parkspace_info_in.path_estimate_time();
		parkspace_information_out.parkspace_status_target = (Common_data::Parkspace_status)parkspace_info_in.parkspace_status_target();

}
void Common_data::copy_data(message::Parkspace_info& parkspace_info_out, const Parkspace_information& parkspace_information_in)
{
	parkspace_info_out.set_parkingspace_index_id(parkspace_information_in.parkingspace_index_id);
	parkspace_info_out.set_parkingspace_type((message::Parkspace_type)parkspace_information_in.parkingspace_type);
	parkspace_info_out.set_parkingspace_unit_id(parkspace_information_in.parkingspace_unit_id);
	parkspace_info_out.set_parkingspace_room_id(parkspace_information_in.parkingspace_room_id);
	parkspace_info_out.set_parkingspace_direction((message::Direction)parkspace_information_in.parkingspace_direction);
	parkspace_info_out.set_parkingspace_floor_id(parkspace_information_in.parkingspace_floor_id);
	parkspace_info_out.set_parkingspace_width(parkspace_information_in.parkingspace_width);
	parkspace_info_out.set_parkingspace_height(parkspace_information_in.parkingspace_height);
	parkspace_info_out.set_parkingspace_status((message::Parkspace_status)parkspace_information_in.parkingspace_status);
	copy_data(*parkspace_info_out.mutable_car_info(), parkspace_information_in.car_information);
	parkspace_info_out.set_entry_time(parkspace_information_in.car_entry_time);
	parkspace_info_out.set_leave_time(parkspace_information_in.car_leave_time);
	parkspace_info_out.set_parkspace_path((message::Parkspace_path)parkspace_information_in.parkspace_path);
	parkspace_info_out.set_path_estimate_time(parkspace_information_in.path_estimate_time);
	parkspace_info_out.set_parkspace_status_target((message::Parkspace_status)parkspace_information_in.parkspace_status_target);
}









void Common_data::transform_data(Car_information& car_information_out, const Car_measure_information& car_measure_information_in)
{
	car_information_out.license.clear();
	car_information_out.car_length = car_measure_information_in.car_length;
	car_information_out.car_width = car_measure_information_in.car_width;
	car_information_out.car_height = car_measure_information_in.car_height;
	car_information_out.car_wheel_base = car_measure_information_in.car_wheel_base;
	car_information_out.car_wheel_width = car_measure_information_in.car_wheel_width;
}
void Common_data::transform_data(Car_measure_information& car_measure_information_out, const Car_information& car_informatio_in)
{
	car_measure_information_out.car_center_x = 0;
	car_measure_information_out.car_center_y = 0;
	car_measure_information_out.car_angle = 0;
	car_measure_information_out.car_length = car_informatio_in.car_length;
	car_measure_information_out.car_width = car_informatio_in.car_width;
	car_measure_information_out.car_height = car_informatio_in.car_height;
	car_measure_information_out.car_wheel_base = car_informatio_in.car_wheel_base;
	car_measure_information_out.car_wheel_width = car_informatio_in.car_wheel_width;
	car_measure_information_out.car_front_theta = 0;
	car_measure_information_out.correctness = false;


}

void Common_data::transform_data(Car_wheel_information& car_wheel_information_out, const Car_measure_information& car_measure_information_in)
{
	car_wheel_information_out.car_center_x = car_measure_information_in.car_center_x;
	car_wheel_information_out.car_center_y = car_measure_information_in.car_center_y;
	car_wheel_information_out.car_angle = car_measure_information_in.car_angle;

	car_wheel_information_out.car_wheel_base = car_measure_information_in.car_wheel_base;
	car_wheel_information_out.car_wheel_width = car_measure_information_in.car_wheel_width;
	car_wheel_information_out.car_front_theta = car_measure_information_in.car_front_theta;
	car_wheel_information_out.correctness = car_measure_information_in.correctness;
}
void Common_data::transform_data(Car_measure_information& car_measure_information_out, const Car_wheel_information& car_wheel_information_in)
{
	car_measure_information_out.car_center_x = car_wheel_information_in.car_center_x;
	car_measure_information_out.car_center_y = car_wheel_information_in.car_center_y;
	car_measure_information_out.car_angle = car_wheel_information_in.car_angle;
	car_measure_information_out.car_length = 0;
	car_measure_information_out.car_width = 0;
	car_measure_information_out.car_height = 0;
	car_measure_information_out.car_wheel_base = car_wheel_information_in.car_wheel_base;
	car_measure_information_out.car_wheel_width = car_wheel_information_in.car_wheel_width;
	car_measure_information_out.car_front_theta = car_wheel_information_in.car_front_theta;
	car_measure_information_out.correctness = car_wheel_information_in.correctness;
}


void Common_data::scaling(Car_measure_information& car_measure_information, float rate)
{
	car_measure_information.car_center_x *= rate;
	car_measure_information.car_center_y *= rate;
	car_measure_information.car_length *= rate;
	car_measure_information.car_width *= rate;
	car_measure_information.car_height *= rate;
	car_measure_information.car_wheel_base *= rate;
	car_measure_information.car_wheel_width *= rate;
}
void Common_data::scaling(Car_information& car_information, float rate)
{
	car_information.car_length *= rate;
	car_information.car_width *= rate;
	car_information.car_height *= rate;
	car_information.car_wheel_base *= rate;
	car_information.car_wheel_width *= rate;

}
void Common_data::scaling(Parkspace_information& parkspace_information, float rate)
{
	parkspace_information.parkingspace_width *= rate;
	parkspace_information.parkingspace_height *= rate;
	scaling(parkspace_information.car_information, rate);
}

bool Common_data::approximate_rate(float a, float b, float rate)
{
	if ( a >= b*(1-rate) && a< b*(1+rate))
	{
	    return true;
	}
	else
	{
	    return false;
	}
}

bool Common_data::approximate_difference(float a, float b, float difference)
{
	if ( a >= (b-difference) && a< (b+difference) )
	{
		return true;
	}
	else
	{
		return false;
	}
}




