//////////////////////////////////////////////////////////////////////////////
//
// Asura_PlatformInput.cpp
//
// Created by Neil Massey.
//
// Copyright (c) 2002 Rebellion. All rights reserved.
//
// Implementation of Asura_UserInput private members
//
//////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"
#include "Input/Asura_PlatformInput.h"
#include "Input/Asura_PS2_Input.h"
#if ASURA_PS2_SUPPORT_KEYBOARD
#include "Input/Asura_PS2_USBKeyboard.h"
#endif
#include <ctype.h>

// G L O B A L S /////////////////////////////////////////////////////////////
// (MP - 01.02.2002) This is just for linkage with tools stuff.
bool UseHardwareMouse = false;

void Asura_UserInput::Platform_Initialise(void)
{
	// NM - Uncomment next line before .Initialise to enable pressure sensitivity
	// Enabling pressure sensitivity incurs a penalty in that it takes a lot
	// longer between plugging in a controller and it becoming available than if pressure is off
	// AW - This should be done is Asura_UserInput::PlatformProject_Initialise()
//	Asura_PS2_Input::SetPressureSensitivity(true);
	Asura_PS2_Input::Initialise(CurrentInput, AxisValue);
}

void Asura_UserInput::Platform_Update(void)
{
	Asura_PS2_Input::Update();

#if ASURA_PS2_SUPPORT_KEYBOARD
        const u_int uBUFFERLEN = 10;    // max number of simultaneously pressed buttons
        static Asura_UnicodeChar ls_wszInput[uBUFFERLEN];

        s_wszInputBuffer = ls_wszInput;

        s_uInputBufferSize = 0;

        if (Asura_PS2_Input::GetUSBKeyboard())
        {
            u_int uKeyStrokeBufferSize = Asura_PS2_Input::GetUSBKeyboard()->GetKeyStrokeBufferSize();
            int* piKeyStrokeBuffer = Asura_PS2_Input::GetUSBKeyboard()->GetKeyStrokeBuffer();
            bool* pbKeyStrokeShiftBuffer = Asura_PS2_Input::GetUSBKeyboard()->GetKeyStrokeShiftBuffer();
            bool* pbKeyStrokeCapsBuffer = Asura_PS2_Input::GetUSBKeyboard()->GetKeyStrokeCapsBuffer();

            for (u_int u = 0; u < uKeyStrokeBufferSize; u++)
            {
                u_int uModifiedKey = piKeyStrokeBuffer[u];
                if (uModifiedKey < ASURA_KEYID_END_OF_KEYBOARD && s_uInputBufferSize < uBUFFERLEN)
                {

                    if( uModifiedKey < ASURA_NUMBER_OF_ASCII_KEYS && isalpha(uModifiedKey) )
                    {
                        if( pbKeyStrokeCapsBuffer[u] != pbKeyStrokeShiftBuffer[u] )
                        {
                            uModifiedKey = toupper( uModifiedKey );
                        }
                    }
                    else if( pbKeyStrokeShiftBuffer[u] )
                    {
                        // Any key IDs outside [0,128) need to be first cast to u_char to
                        // ensure their correct value is preserved when implicitly
                        // casting to u_int below. This only matters on platforms where
                        // char is signed by default but we might as well do it anyway.
                        switch( uModifiedKey )
                        {
                            case ',' : uModifiedKey = '<'; break;
                            case '.' : uModifiedKey = '>'; break;
                            case ';' : uModifiedKey = ':'; break;
                            case '-' : uModifiedKey = '_'; break;
                            case '=' : uModifiedKey = '+'; break;
                            case '[' : uModifiedKey = '{'; break;
                            case ']' : uModifiedKey = '}'; break;
                            case '\'': uModifiedKey = '@'; break;
                            case '\\': uModifiedKey = '|'; break;
                            case '#' : uModifiedKey = '~'; break;
                            case '/' : uModifiedKey = '?'; break;
                            case '1' : uModifiedKey = '!'; break;
                            case '2' : uModifiedKey = '"'; break;
                            case '3' : uModifiedKey = static_cast<u_char>('£'); break;
                            case '4' : uModifiedKey = '$'; break;
                            case '5' : uModifiedKey = '%'; break;
                            case '6' : uModifiedKey = '^'; break;
                            case '7' : uModifiedKey = '&'; break;
                            case '8' : uModifiedKey = '*'; break;
                            case '9' : uModifiedKey = '('; break;
                            case '0' : uModifiedKey = ')'; break;
                        }
                    }
                    ls_wszInput[s_uInputBufferSize++] = static_cast<Asura_UnicodeChar>(uModifiedKey);
                }
            }
        }
#endif
}

void Asura_UserInput::Platform_Shutdown(void)
{
	Asura_PS2_Input::Shutdown();
}

void Asura_UserInput::Platform_Reset(void)
{
}

int Asura_UserInput::Platform_GetNumberOfActiveControllers(bool bConsecutive)
{
    return Asura_PS2_Input::GetNumberOfActiveControllers(bConsecutive);
}

bool Asura_UserInput::Platform_IsControllerActive(int iPlayerNum)
{
    return Asura_PS2_Input::IsControllerActive(iPlayerNum);
}

bool Asura_UserInput::Platform_IsKeyToggled(int iKey)
{
    return Asura_PS2_Input::IsKeyToggled(iKey);
}

void Asura_UserInput::Platform_UpdateInputBuffer( Asura_UnicodeChar* wszBuffer, u_int uMaxChars, u_int& uInputCursorPos,
												  bool (*pfnHandleInputOverride)(ASURA_KEYID eKey), const Asura_UnicodeChar* wszInputBuffer,
												  u_int uInputBufferSize )
{

}
