/// Joystick.cpp
///
/// (c) 2001-2011 by Bastian Spiegel <bs@tkscript.de>
///     - Distributed under terms of the Lesser GNU General Public License (LGPL).
///       See COPYING and <http://www.gnu.org/licenses/licenses.html#LGPL> for further information.
///

#include <stdlib.h>

#define YAC_BIGSTRING defined
#include <yac.h>

#ifdef YAC_WIN32
#include <windows.h>
#include <ddraw.h>
#endif
#include "HAL.h"
#include "Joystick.h"

extern YAC_Host *yac_host;
extern sUI clid_Joystick;

#include "sdl_includes.h"


_Joystick::_Joystick(void) {
	sdl_joystick=0;
	name.class_ID=YAC_CLID_STRING;
}

_Joystick::~_Joystick() {
	close();
}

void _Joystick::open(sSI _idx) {
	const char*c = SDL_JoystickName(_idx);
	name.copy((sChar*)c);

	sdl_joystick = ::SDL_JoystickOpen(_idx);

	if(yac_host->yacGetDebugLevel() > 15)
   {
		yac_host->printf("[...] Joystick[%i]=\"%s\".\n", _idx, c);
   }
}

void _Joystick::close() {
	if(sdl_joystick)
	{
		::SDL_JoystickClose((SDL_Joystick*)sdl_joystick);

		sdl_joystick = NULL;
	}
}


void YAC_VCALL _Joystick::yacOperator(sSI _cmd, YAC_Object *_o, YAC_Value *_r) {
	// switch(_cmd)
	// {
	// // /**case YAC_OP_ASSIGN:
	// // 	if(YAC_CHK(_o, class_ID))
	// // 	{
	// // 		_Joystick*o=(_Joystick*)_o;
	// // 		close();
	// // 		if(o->sdl_joystick)
	// // 		{
	// // 			// ---- snatch SDL_Joystick* structure ----
	// // 			sdl_joystick=o->sdl_joystick;
	// // 			name.yacCopy(&o->name);
	// // 			o->name.free();
	// // 			o->sdl_joystick=0;
	// // 		}
	// // 	}
	// // 	break;**/
   // //default:
		YAC_Object::yacOperator(_cmd, _o, _r);
      //	break;
      //}
}

sSI _Joystick::_getAxis(sSI _i) {
	if(sdl_joystick)
   {
		return SDL_JoystickGetAxis((SDL_Joystick*)sdl_joystick, _i);
   }
	else
   {
		return 0;
   }
}

sSI _Joystick::_getBall(sSI _i, YAC_Object *_iv) {
	if(sdl_joystick)
	{
		if(YAC_VALID(_iv))
		{
         if(_iv->yacArrayGetMaxElements() >= 2)
         {
            int dx;
            int dy;

            sSI r = ::SDL_JoystickGetBall((SDL_Joystick*)sdl_joystick, _i, &dx, &dy);

            YAC_Value v;
            v.initInt(dx);
            _iv->yacArraySet(NULL, 0, &v);

            v.initInt(dy);
            _iv->yacArraySet(NULL, 1, &v);

            return r;
         }
		}
	}
	return 0;
}

sSI _Joystick::_getButton(sSI _i) {
   if(sdl_joystick)
   {
		return ::SDL_JoystickGetButton((SDL_Joystick*)sdl_joystick, _i);
   }
	else
   {
		return 0;
   }
}

sSI _Joystick::_getHat(sSI _i) {
   if(sdl_joystick)
   {
		return ::SDL_JoystickGetHat((SDL_Joystick*)sdl_joystick, _i);
   }
	else
   {
		return 0;
   }
}

sSI _Joystick::_getIndex(void) {
   if(sdl_joystick)
   {
		return ::SDL_JoystickIndex((SDL_Joystick*)sdl_joystick);
   }
	else
   {
		return 0;
   }
}

sSI _Joystick::_getNumAxes(void) {
   if(sdl_joystick)
   {
		return ::SDL_JoystickNumAxes((SDL_Joystick*)sdl_joystick);
   }
	else
   {
		return 0;
   }
}

sSI _Joystick::_getNumBalls(void) {
   if(sdl_joystick)
   {
		return ::SDL_JoystickNumBalls((SDL_Joystick*)sdl_joystick);
   }
	else
   {
		return 0;
   }
}

sSI _Joystick::_getNumButtons(void) {
   if(sdl_joystick)
   {
		return ::SDL_JoystickNumButtons((SDL_Joystick*)sdl_joystick);
   }
	else
   {
		return 0;
   }
}

sSI _Joystick::_getNumHats(void) {
   if(sdl_joystick)
   {
		return ::SDL_JoystickNumHats((SDL_Joystick*)sdl_joystick);
   }
	else
   {
		return 0;
   }
}

void _Joystick::_getName(YAC_Value *_r) {
   YAC_String *s =  YAC_New_String();
   s->yacCopy(&name);
   _r->initString(s, 1);
}
