-- Keyboard entries
function Bind_WASD_Numpad(iPad)
    BindAxis(kINPUT_KeyD, STICK_Left,  STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_KeyA, STICK_Left,  STICKAXIS_X, true,  iPad)
    BindAxis(kINPUT_KeyW, STICK_Left,  STICKAXIS_Y, false, iPad)
    BindAxis(kINPUT_KeyS, STICK_Left,  STICKAXIS_Y, true,  iPad)

    BindAxis(kINPUT_KeyNumpad6, STICK_Right, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_KeyNumpad4, STICK_Right, STICKAXIS_X, true,  iPad)
    BindAxis(kINPUT_KeyNumpad8, STICK_Right, STICKAXIS_Y, false, iPad)
    BindAxis(kINPUT_KeyNumpad5, STICK_Right, STICKAXIS_Y, true,  iPad)

    BindButton(kINPUT_KeySpace, BUTTON_A, 	  iPad)
    BindButton(kINPUT_KeyQ, 	BUTTON_Y, 	  iPad)
    BindButton(kINPUT_KeyE, 	BUTTON_ShoulderRight, iPad)
    BindButton(kINPUT_KeyZ, 	BUTTON_X, 	  iPad)
    BindButton(kINPUT_KeyC, 	BUTTON_B, 	  iPad)
    BindButton(kINPUT_KeyBackslash, BUTTON_Start, iPad)
    BindButton(kINPUT_KeyEscape, BUTTON_Back, iPad)
	BindButton(kINPUT_KeyReturn, BUTTON_A, iPad)
end

function Bind_WASD(iPad)
    BindAxis(kINPUT_KeyD, STICK_Left, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_KeyA, STICK_Left, STICKAXIS_X, true,  iPad)
    BindAxis(kINPUT_KeyW, STICK_Left, STICKAXIS_Y, false, iPad)
    BindAxis(kINPUT_KeyS, STICK_Left, STICKAXIS_Y, true,  iPad)
    BindButton(kINPUT_KeyEscape, BUTTON_Back, iPad)
	BindButton(kINPUT_KeyReturn, BUTTON_A, iPad)
    BindButton(kINPUT_KeySpace, BUTTON_A, iPad)
    BindButton(kINPUT_KeyQ, BUTTON_Y, iPad)
    BindButton(kINPUT_KeyE, BUTTON_ShoulderRight, iPad)
    BindButton(kINPUT_KeyZ, BUTTON_X, iPad)
    BindButton(kINPUT_KeyC, BUTTON_B, iPad)
end

function Bind_IJKL(iPad)
    BindAxis(kINPUT_KeyL,  STICK_Left, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_KeyJ,  STICK_Left, STICKAXIS_X, true,  iPad)
    BindAxis(kINPUT_KeyI,  STICK_Left, STICKAXIS_Y, false, iPad)
    BindAxis(kINPUT_KeyK,  STICK_Left, STICKAXIS_Y, true,  iPad)
    BindButton(kINPUT_KeyComma, BUTTON_A, iPad)
    BindButton(kINPUT_KeyU, BUTTON_Y,     iPad)
    BindButton(kINPUT_KeyO, BUTTON_ShoulderRight, iPad)
    BindButton(kINPUT_KeyM, BUTTON_X,     iPad)
    BindButton(kINPUT_KeyPeriod, BUTTON_B,iPad)
end

function Bind_Arrows(iPad)
    BindAxis(kINPUT_KeyRight, STICK_Left, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_KeyLeft,  STICK_Left, STICKAXIS_X, true,  iPad)
    BindAxis(kINPUT_KeyUp,    STICK_Left, STICKAXIS_Y, false, iPad)
    BindAxis(kINPUT_KeyDown,  STICK_Left, STICKAXIS_Y, true,  iPad)

    BindButton(kINPUT_KeyRControl, BUTTON_A, 	 iPad)
    BindButton(kINPUT_KeyInsert,   BUTTON_Y, 	 iPad)
    BindButton(kINPUT_KeyHome, 	   BUTTON_ShoulderRight, iPad)
    BindButton(kINPUT_KeyRShift,   BUTTON_X, 	 iPad)
    BindButton(kINPUT_KeyEnd, 	   BUTTON_B, 	 iPad)
end

function Bind_Numpad(iPad)
    BindAxis(kINPUT_KeyNumpad6, STICK_Left, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_KeyNumpad4, STICK_Left, STICKAXIS_X, true,  iPad)
    BindAxis(kINPUT_KeyNumpad8, STICK_Left, STICKAXIS_Y, false, iPad)
    BindAxis(kINPUT_KeyNumpad5, STICK_Left, STICKAXIS_Y, true,  iPad)
    BindButton(kINPUT_KeyNumpad0, BUTTON_A, iPad)
    BindButton(kINPUT_KeyNumpad7, BUTTON_Y, iPad)
end

bind_funcs = {}

-- Old school xbox gamepad
bind_funcs["Interact Gaming Device"] = function(iPad)
    BindButton(kINPUT_GamepadButton1 , BUTTON_X,             iPad)
    BindButton(kINPUT_GamepadButton2 , BUTTON_Y,             iPad)
    BindButton(kINPUT_GamepadButton3 , BUTTON_ShoulderLeft,         iPad)
    BindButton(kINPUT_GamepadButton4 , BUTTON_A,             iPad)
    BindButton(kINPUT_GamepadButton5 , BUTTON_B,             iPad)
    BindButton(kINPUT_GamepadButton6 , BUTTON_ShoulderRight,         iPad)
    BindButton(kINPUT_GamepadButton7 , BUTTON_StickLeft,     iPad)
    BindButton(kINPUT_GamepadButton8 , BUTTON_StickRight,    iPad)
    BindButton(kINPUT_GamepadButton9 , BUTTON_TriggerLeft,  iPad)
    BindButton(kINPUT_GamepadButton10, BUTTON_TriggerRight, iPad)
    BindButton(kINPUT_GamepadButton11, BUTTON_Back,          iPad)
    BindButton(kINPUT_GamepadButton12, BUTTON_Start,         iPad)

    BindButton(kINPUT_DPadUp,    BUTTON_DPadUp,    iPad)
    BindButton(kINPUT_DPadRight, BUTTON_DPadRight, iPad)
    BindButton(kINPUT_DPadDown,  BUTTON_DPadDown,  iPad)
    BindButton(kINPUT_DPadLeft,  BUTTON_DPadLeft,  iPad)

    BindAxis(kINPUT_GamepadLeftStickX,  STICK_Left,  STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadLeftStickY,  STICK_Left,  STICKAXIS_Y, true,  iPad)
    BindAxis(kINPUT_GamepadLeftStickZ,  STICK_Right, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadRightStickZ, STICK_Right, STICKAXIS_Y, true,  iPad)
end

-- The real xbox controller using the XBCD driver happens to have the same layout
-- as the prototype.  Well, not any more; it changed with the new 1.07 driver.
bind_funcs["XBCD XBox Gamepad"] = function(iPad)
    BindButton(kINPUT_GamepadButton1 ,   BUTTON_A,             iPad)
    BindButton(kINPUT_GamepadButton2 ,   BUTTON_B,             iPad)
    BindButton(kINPUT_GamepadButton3 ,   BUTTON_X,             iPad)
    BindButton(kINPUT_GamepadButton4 ,   BUTTON_Y,             iPad)
    BindButton(kINPUT_GamepadButton5 ,   BUTTON_ShoulderRight,         iPad)
    BindButton(kINPUT_GamepadButton6 ,   BUTTON_ShoulderLeft,         iPad)
    BindButton(kINPUT_GamepadButton7 ,   BUTTON_Start,         iPad)
    BindButton(kINPUT_GamepadButton8 ,   BUTTON_Back,          iPad)
    BindButton(kINPUT_GamepadButton9 ,   BUTTON_StickLeft,     iPad)
    BindButton(kINPUT_GamepadButton10,   BUTTON_StickRight,    iPad)
    BindButton(kINPUT_GamepadButton11,   BUTTON_TriggerLeft,  iPad)
    BindButton(kINPUT_GamepadButton12,   BUTTON_TriggerRight, iPad)

    BindButton(kINPUT_DPadUp,           BUTTON_DPadUp,    iPad)
    BindButton(kINPUT_DPadRight,        BUTTON_DPadRight, iPad)
    BindButton(kINPUT_DPadDown,         BUTTON_DPadDown,  iPad)
    BindButton(kINPUT_DPadLeft,         BUTTON_DPadLeft,  iPad)

    BindAxis(kINPUT_GamepadLeftStickX,  STICK_Left,  STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadLeftStickY,  STICK_Left,  STICKAXIS_Y, true,  iPad)
    BindAxis(kINPUT_GamepadRightStickX, STICK_Right, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadRightStickY, STICK_Right, STICKAXIS_Y, true,  iPad)
end

-- Logitech Dual Action
bind_funcs["Logitech Dual Action"] = function(iPad)
    BindButton(kINPUT_GamepadButton1 , BUTTON_X,             iPad)
    BindButton(kINPUT_GamepadButton2 , BUTTON_A,             iPad)
    BindButton(kINPUT_GamepadButton3 , BUTTON_B,             iPad)
    BindButton(kINPUT_GamepadButton4 , BUTTON_Y,             iPad)
    BindButton(kINPUT_GamepadButton5 , BUTTON_ShoulderLeft,         iPad)
    BindButton(kINPUT_GamepadButton6 , BUTTON_ShoulderRight,         iPad)
    BindButton(kINPUT_GamepadButton7 , BUTTON_TriggerLeft,  iPad)
    BindButton(kINPUT_GamepadButton8 , BUTTON_TriggerRight, iPad)
    BindButton(kINPUT_GamepadButton9 , BUTTON_Back,          iPad)
    BindButton(kINPUT_GamepadButton10, BUTTON_Start,         iPad)
    BindButton(kINPUT_GamepadButton11, BUTTON_StickLeft,     iPad)
    BindButton(kINPUT_GamepadButton12, BUTTON_StickRight,    iPad)

    BindButton(kINPUT_DPadUp,    BUTTON_DPadUp,    iPad)
    BindButton(kINPUT_DPadRight, BUTTON_DPadRight, iPad)
    BindButton(kINPUT_DPadDown,  BUTTON_DPadDown,  iPad)
    BindButton(kINPUT_DPadLeft,  BUTTON_DPadLeft,  iPad)

    BindAxis(kINPUT_GamepadLeftStickX,  STICK_Left,  STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadLeftStickY,  STICK_Left,  STICKAXIS_Y, true,  iPad)
    BindAxis(kINPUT_GamepadLeftStickZ,  STICK_Right, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadRightStickZ, STICK_Right, STICKAXIS_Y, true,  iPad)
end

-- PS3 Controller
bind_funcs["PS3GP"] = function(iPad)
    BindButton(kINPUT_GamepadButton1 , BUTTON_A,            iPad)
    BindButton(kINPUT_GamepadButton2 , BUTTON_B,            iPad)
    BindButton(kINPUT_GamepadButton3 , BUTTON_X,            iPad)
    BindButton(kINPUT_GamepadButton4 , BUTTON_Y,            iPad)
    BindButton(kINPUT_GamepadButton5 , BUTTON_ShoulderLeft, iPad)
    BindButton(kINPUT_GamepadButton6 , BUTTON_ShoulderRight,iPad)
    BindButton(kINPUT_GamepadButton7 , BUTTON_Back,         iPad)
    BindButton(kINPUT_GamepadButton8 , BUTTON_Start,        iPad)
    BindButton(kINPUT_GamepadButton9 , BUTTON_StickLeft,    iPad)
    BindButton(kINPUT_GamepadButton10, BUTTON_StickRight,   iPad)
    BindButton(kINPUT_GamepadButton11, BUTTON_TriggerLeft,  iPad)
    BindButton(kINPUT_GamepadButton12, BUTTON_TriggerRight, iPad)

    BindButton(kINPUT_DPadUp,    BUTTON_DPadUp,    iPad)
    BindButton(kINPUT_DPadRight, BUTTON_DPadRight, iPad)
    BindButton(kINPUT_DPadDown,  BUTTON_DPadDown,  iPad)
    BindButton(kINPUT_DPadLeft,  BUTTON_DPadLeft,  iPad)

    BindAxis(kINPUT_GamepadLeftStickX,  STICK_Left,  STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadLeftStickY,  STICK_Left,  STICKAXIS_Y, true,  iPad)
    BindAxis(kINPUT_GamepadRightStickX, STICK_Right, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadRightStickY, STICK_Right, STICKAXIS_Y, true,  iPad)
end


bind_funcs["XBOX 360 For Windows (Controller)"] = function(iPad)
    BindButton(kINPUT_GamepadButton1 ,   BUTTON_A,             iPad)
    BindButton(kINPUT_GamepadButton2 ,   BUTTON_B,             iPad)
    BindButton(kINPUT_GamepadButton3 ,   BUTTON_X,             iPad)
    BindButton(kINPUT_GamepadButton4 ,   BUTTON_Y,             iPad)
    BindButton(kINPUT_GamepadButton5 ,   BUTTON_ShoulderLeft,         iPad)
    BindButton(kINPUT_GamepadButton6 ,   BUTTON_ShoulderRight,         iPad)
    BindButton(kINPUT_GamepadButton7 ,   BUTTON_Back,          iPad)
    BindButton(kINPUT_GamepadButton8 ,   BUTTON_Start,         iPad)
    BindButton(kINPUT_GamepadButton9 ,   BUTTON_StickLeft,     iPad)
    BindButton(kINPUT_GamepadButton10,   BUTTON_StickRight,    iPad)
    -- The triggers with the current driver correspond to -Z and +Z
    -- so we don't have a way to bind them atm
    --BindButton(kINPUT_GamepadButton11,   BUTTON_TriggerLeft,  iPad)
    --BindButton(kINPUT_GamepadButton12,   BUTTON_TriggerRight, iPad)

    BindButton(kINPUT_DPadUp,           BUTTON_DPadUp,    iPad)
    BindButton(kINPUT_DPadRight,        BUTTON_DPadRight, iPad)
    BindButton(kINPUT_DPadDown,         BUTTON_DPadDown,  iPad)
    BindButton(kINPUT_DPadLeft,         BUTTON_DPadLeft,  iPad)

    BindAxis(kINPUT_GamepadLeftStickX,  STICK_Left,  STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadLeftStickY,  STICK_Left,  STICKAXIS_Y, true,  iPad)
    BindAxis(kINPUT_GamepadRightStickX, STICK_Right, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadRightStickY, STICK_Right, STICKAXIS_Y, true,  iPad)
end

-- these bindings need to match the list in DFInputXenon.cpp, but
-- the order was arbitrary. if we can solve the trigger issue,  then
-- could in theory use the same function for both, but until that
-- happens, i will leave the order as is
bind_funcs["Xbox360"] = function(iPad)
    BindButton(kINPUT_GamepadButton1 ,   BUTTON_A,             iPad)
    BindButton(kINPUT_GamepadButton2 ,   BUTTON_B,             iPad)
    BindButton(kINPUT_GamepadButton3 ,   BUTTON_X,             iPad)
    BindButton(kINPUT_GamepadButton4 ,   BUTTON_Y,             iPad)
    BindButton(kINPUT_GamepadButton5 ,   BUTTON_ShoulderLeft,         iPad)
    BindButton(kINPUT_GamepadButton6 ,   BUTTON_ShoulderRight,         iPad)
    BindButton(kINPUT_GamepadButton7 ,   BUTTON_Back,          iPad)
    BindButton(kINPUT_GamepadButton8 ,   BUTTON_Start,         iPad)
    BindButton(kINPUT_GamepadButton9 ,   BUTTON_StickLeft,     iPad)
    BindButton(kINPUT_GamepadButton10,   BUTTON_StickRight,    iPad)
    BindButton(kINPUT_GamepadButton11,   BUTTON_TriggerLeft,  iPad)
    BindButton(kINPUT_GamepadButton12,   BUTTON_TriggerRight, iPad)

    BindButton(kINPUT_DPadUp,           BUTTON_DPadUp,    iPad)
    BindButton(kINPUT_DPadRight,        BUTTON_DPadRight, iPad)
    BindButton(kINPUT_DPadDown,         BUTTON_DPadDown,  iPad)
    BindButton(kINPUT_DPadLeft,         BUTTON_DPadLeft,  iPad)

    BindAxis(kINPUT_GamepadLeftStickX,  STICK_Left,  STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadLeftStickY,  STICK_Left,  STICKAXIS_Y, true,  iPad)
    BindAxis(kINPUT_GamepadRightStickX, STICK_Right, STICKAXIS_X, false, iPad)
    BindAxis(kINPUT_GamepadRightStickY, STICK_Right, STICKAXIS_Y, true,  iPad)
end

function Bind_Gamepad(iPad, id)
    print(string.format("Gamepad %d: %s", iPad, id))

    local func = bind_funcs[id]

	if     iPad	== 0 then Bind_WASD_Numpad(iPad)
	elseif iPad	== 1 then Bind_IJKL(iPad)
	elseif iPad	== 2 then Bind_Arrows(iPad)
	else
		print("Can't find any keyboard bindings for", iPad, id, " -- input only available from gamepad")
	end

    if not func then
        print(string.format("Gamepad %d is unknown type '%s': using default", iPad, id))
        func = bind_funcs["Logitech Dual Action"]
    end

    assert(func)
    func(iPad)
end
