#include "externals.hh"
#include "utilities.hh"


void disableButton( )
{
	ImGui::PushStyleColor( ImGuiCol_Button , ImVec4( .3 , .3 , .3 , 1 ) );
	ImGui::PushStyleColor( ImGuiCol_ButtonHovered , ImVec4( .3 , .3 , .3 , 1 ) );
	ImGui::PushStyleColor( ImGuiCol_ButtonActive , ImVec4( .3 , .3 , .3 , 1 ) );
}

/*----------------------------------------------------------------------------*/

void updateAngle(
		__rw__	float& initial ,
		__rd__	const float delta
	)
{
	initial = fmod( initial + delta + 540 , 360 ) - 180;
}

void anglesToMatrix(
		__rd__	float const* angles ,
		__wr__	float* matrix )
{
	float c[3] , s[3];
	for ( int i = 0 ; i < 3 ; i ++ ) {
		const float a = M_PI * angles[ i ] / 180;
		c[i] = cos( a );
		s[i] = sin( a );
	}
	matrix[0] = c[1]*c[2];
	matrix[1] = s[0]*s[1]*c[2] - c[0]*s[2];
	matrix[2] = s[0]*s[2] + c[0]*s[1]*c[2];
	matrix[3] = c[1]*s[2];
	matrix[4] = c[0]*c[2] + s[0]*s[1]*s[2];
	matrix[5] = c[0]*s[1]*s[2] - s[0]*c[2];
	matrix[6] = -s[1];
	matrix[7] = s[0]*c[1];
	matrix[8] = c[0]*c[1];
}


/*= JSON =====================================================================*/

template void jsonAdd< double >(
		__rw__ picojson::value::object& object ,
		__rd__ std::string const& key ,
		__rd__ double const& v );
template void jsonAdd< picojson::value::object >(
		__rw__ picojson::value::object& object ,
		__rd__ std::string const& key ,
		__rd__ picojson::value::object const& v );
template void jsonAdd< picojson::value::array >(
		__rw__ picojson::value::object& object ,
		__rd__ std::string const& key ,
		__rd__ picojson::value::array const& v );
template void jsonAdd< std::string >(
		__rw__ picojson::value::object& object ,
		__rd__ std::string const& key ,
		__rd__ std::string const& v );

template< >
void jsonAdd< picojson::value >(
		__rw__ picojson::value::object& object ,
		__rd__ std::string const& key ,
		__rd__ picojson::value const& v )
{
	using T_Entry = std::pair< std::string , picojson::value >;
	object.insert( T_Entry( key , v ) );
}

template< >
void jsonAdd< int >(
		__rw__ picojson::value::object& object ,
		__rd__ std::string const& key ,
		__rd__ int const& v )
{
	using T_Entry = std::pair< std::string , picojson::value >;
	object.insert( T_Entry( key , picojson::value( double( v ) ) ) );
}

template< >
void jsonAdd< unsigned >(
		__rw__ picojson::value::object& object ,
		__rd__ std::string const& key ,
		__rd__ unsigned const& v )
{
	using T_Entry = std::pair< std::string , picojson::value >;
	object.insert( T_Entry( key , picojson::value( double( v ) ) ) );
}

/*----------------------------------------------------------------------------*/

picojson::value jsonVector( float const* vector , const int nComponents )
{
	using namespace picojson;
	value::array a;
	for ( auto i = 0 ; i < nComponents ; i ++ ) {
		a.push_back( value( vector[ i ] ) );
	}
	return value( a );
}

/*----------------------------------------------------------------------------*/

void jsonGetVectorOpt(
		__wr__ float* vector ,
		__rd__ const size_t size ,
		__rd__ picojson::value::object const& object ,
		__rd__ char const* key )
{
	using namespace picojson;
	auto const* ptr( jsonGetOpt< array >( object , key ) );
	if ( !ptr ) {
		return;
	}

	auto const& a( *ptr );
	if ( a.size( ) != size ) {
		throw X_JsonGetFailed( );
	}

	int i = 0;
	for ( auto const& av : a ) {
		if ( !av.is< double >( ) ) {
			throw X_JsonGetFailed( );
		}
		vector[ i ++ ] = float( av.get< double >( ) );
	}
}