import pathlib
import os.path

cwd = pathlib.Path(__file__).parent.absolute()
cwd = os.path.abspath(cwd)

fp = os.path.join(cwd, 'ProjectSettings', 'InputManager.asset')

def output_unity_axis(f, joy_idx, name, button='', axis_type=0, joy_axis=0):
    f.write('  - serializedVersion: 3\n')
    f.write('    m_Name: joystick {} {}\n'.format(joy_idx, name))
    f.write('    descriptiveName: Reconfigurable gamepad input\n')
    f.write('    descriptiveNegativeName: \n')
    f.write('    negativeButton: \n')
    f.write('    positiveButton: {}\n'.format(button))
    f.write('    altNegativeButton: \n')
    f.write('    altPositiveButton: \n')
    f.write('    gravity: 3\n')
    f.write('    dead: 0.01\n')
    f.write('    sensitivity: 3\n')
    f.write('    snap: 1\n')
    f.write('    invert: 0\n')
    f.write('    type: {}\n'.format(axis_type))
    f.write('    axis: {}\n'.format(joy_axis))
    f.write('    joyNum: {}\n'.format(joy_idx))

def output_axis(f, joy_idx, joy_axis):
    name = 'axis {}'.format(joy_axis)
    output_unity_axis(f, joy_idx, name, axis_type=2, joy_axis=joy_axis)

def output_button(f, joy_idx, button):
    name = 'button {}'.format(button)
    output_unity_axis(f, joy_idx, name, button='joystick button {}'.format(button))

joystick_count = 9
joystick_axis_count = 10
joystick_button_count = 20
with open(fp, 'wt') as f:
    f.write('%YAML 1.1\n')
    f.write('%TAG !u! tag:unity3d.com,2011:\n')
    f.write('--- !u!13 &1\n')
    f.write('InputManager:\n')
    f.write('  m_ObjectHideFlags: 0\n')
    f.write('  serializedVersion: 2\n')
    f.write('  m_Axes:\n')

    # Default values, required by the UI
    f.write('  - serializedVersion: 3\n')
    f.write('    m_Name: Horizontal\n')
    f.write('    descriptiveName: UI Horizontal\n')
    f.write('    descriptiveNegativeName: \n')
    f.write('    negativeButton: left\n')
    f.write('    positiveButton: right\n')
    f.write('    altNegativeButton: a\n')
    f.write('    altPositiveButton: d\n')
    f.write('    gravity: 3\n')
    f.write('    dead: 0.001\n')
    f.write('    sensitivity: 3\n')
    f.write('    snap: 1\n')
    f.write('    invert: 0\n')
    f.write('    type: 0\n')
    f.write('    axis: 0\n')
    f.write('    joyNum: 0\n')
    f.write('  - serializedVersion: 3\n')
    f.write('    m_Name: Vertical\n')
    f.write('    descriptiveName: UI Vertical\n')
    f.write('    descriptiveNegativeName: \n')
    f.write('    negativeButton: down\n')
    f.write('    positiveButton: up\n')
    f.write('    altNegativeButton: s\n')
    f.write('    altPositiveButton: w\n')
    f.write('    gravity: 3\n')
    f.write('    dead: 0.001\n')
    f.write('    sensitivity: 3\n')
    f.write('    snap: 1\n')
    f.write('    invert: 0\n')
    f.write('    type: 0\n')
    f.write('    axis: 0\n')
    f.write('    joyNum: 0\n')
    f.write('  - serializedVersion: 3\n')
    f.write('    m_Name: Horizontal\n')
    f.write('    descriptiveName: UI Horizontal (gamepad)\n')
    f.write('    descriptiveNegativeName: \n')
    f.write('    negativeButton: \n')
    f.write('    positiveButton: \n')
    f.write('    altNegativeButton: \n')
    f.write('    altPositiveButton: \n')
    f.write('    gravity: 3\n')
    f.write('    dead: 0.01\n')
    f.write('    sensitivity: 3\n')
    f.write('    snap: 1\n')
    f.write('    invert: 0\n')
    f.write('    type: 2\n')
    f.write('    axis: 0\n')
    f.write('    joyNum: 0\n')
    f.write('  - serializedVersion: 3\n')
    f.write('    m_Name: Vertical\n')
    f.write('    descriptiveName: UI Vertical (gamepad)\n')
    f.write('    descriptiveNegativeName: \n')
    f.write('    negativeButton: \n')
    f.write('    positiveButton: \n')
    f.write('    altNegativeButton: \n')
    f.write('    altPositiveButton: \n')
    f.write('    gravity: 3\n')
    f.write('    dead: 0.01\n')
    f.write('    sensitivity: 3\n')
    f.write('    snap: 1\n')
    f.write('    invert: 1\n')
    f.write('    type: 2\n')
    f.write('    axis: 1\n')
    f.write('    joyNum: 0\n')
    f.write('  - serializedVersion: 3\n')
    f.write('    m_Name: Submit\n')
    f.write('    descriptiveName: Unity UI...\n')
    f.write('    descriptiveNegativeName: \n')
    f.write('    negativeButton: \n')
    f.write('    positiveButton: enter\n')
    f.write('    altNegativeButton: \n')
    f.write('    altPositiveButton: joystick button 0\n')
    f.write('    gravity: 3\n')
    f.write('    dead: 0.01\n')
    f.write('    sensitivity: 3\n')
    f.write('    snap: 1\n')
    f.write('    invert: 0\n')
    f.write('    type: 0\n')
    f.write('    axis: 1\n')
    f.write('    joyNum: 0\n')
    f.write('  - serializedVersion: 3\n')
    f.write('    m_Name: Cancel\n')
    f.write('    descriptiveName: Unity UI...\n')
    f.write('    descriptiveNegativeName: \n')
    f.write('    negativeButton: \n')
    f.write('    positiveButton: escape\n')
    f.write('    altNegativeButton: \n')
    f.write('    altPositiveButton: joystick button 1\n')
    f.write('    gravity: 3\n')
    f.write('    dead: 0.01\n')
    f.write('    sensitivity: 3\n')
    f.write('    snap: 1\n')
    f.write('    invert: 0\n')
    f.write('    type: 0\n')
    f.write('    axis: 1\n')
    f.write('    joyNum: 0\n')

    for joy_idx in range(joystick_count):
        for joy_axis in range(joystick_axis_count):
            output_axis(f, joy_idx, joy_axis)
        for joy_bt in range(joystick_button_count):
            output_button(f, joy_idx, joy_bt)
