#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import ConfigParser
import recalboxFiles

sys.path.append(
    os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))

import settings.unixSettings as unixSettings
import recalboxFiles


reicastMapping = { 'a' :             {'button': 'btn_b'},
                   'b' :             {'button': 'btn_a'},
                   'x' :             {'button': 'btn_y'},
                   'y' :             {'button': 'btn_x'},
                   'start' :         {'button': 'btn_start'},
                   'hotkey' :        {'button': 'btn_escape'},
                   'pageup' :        {'axis': 'axis_trigger_left',  'button': 'btn_trigger_left'},
                   'pagedown' :      {'axis': 'axis_trigger_right', 'button': 'btn_trigger_right'},
                   'joystick1left' : {'axis': 'axis_x'},
                   'joystick1up' :   {'axis': 'axis_y'},
                   # The DPAD can be an axis (for gpio sticks for example) or a hat
                   'left' :          {'hat': 'axis_dpad1_x', 'axis': 'axis_x', 'button': 'btn_dpad1_left'},
                   'up' :            {'hat': 'axis_dpad1_y', 'axis': 'axis_y', 'button': 'btn_dpad1_up'},
                   'right' :         {'button': 'btn_dpad1_right'},
                   'down' :          {'button': 'btn_dpad1_down'},
# We are only interested in L2/R2 if they are axis, to have real dreamcasttriggers
                   'r2' :            {'axis':  'axis_trigger_right'},
                   'l2' :            {'axis': 'axis_trigger_left'}
}

sections = { 'emulator' : ['mapping_name', 'btn_escape'],
             'dreamcast' : ['btn_a', 'btn_b', 'btn_x', 'btn_y', 'btn_start', 'axis_x', 'axis_y', 'axis_trigget_left', 'axis_trigget_right'],
             'compat' : ['axis_dpad1_x', 'axis_dpad1_y', 'btn_trigger_left', 'btn_trigger_right']

}


# Create the controller configuration file
# returns its name
def generateControllerConfig(controller):
	# Set config file name
    configFileName = "{}/controllerP{}.cfg".format(recalboxFiles.reicastCustom,controller.player)
    Config = ConfigParser.ConfigParser()
    cfgfile = open(configFileName,'w+')
    
    # create ini sections
    for section in sections:
        Config.add_section(section)

    # Add controller name
    Config.set("emulator", "mapping_name", controller.realName)
    
    # Parse controller inputs
    for index in controller.inputs:
		input = controller.inputs[index]
		if input.name not in reicastMapping:
			continue
		if input.type not in reicastMapping[input.name]:
			continue
		var = reicastMapping[input.name][input.type]
		for i in sections:
			if var in sections[i]:
				section = i
				break
		
		# Sadly, we don't get the right axis code for Y hats. So, dirty hack time
		code = input.code
		if input.type == 'hat':
		    if input.name == 'up':
    			code = int(input.code) + 1
		    else:
    			code = input.code
		
		Config.set(section, var, code)

    Config.write(cfgfile)
    cfgfile.close()
    return configFileName


@@ -1688,16 +1688,43 @@
 'btn_b',
+ 'btn_c', 'btn_d', 'btn_z',
  'btn_x'
@@ -1779,17 +1779,17 @@
 s_trigge
-t
+r
 _left', 
@@ -1804,114 +1804,387 @@
 igge
-t_right'%5D,%0A             'compat' : %5B'axis_dpad1_x', 'axis_dpad1_y', 'btn_trigger_left', 'btn_trigger_right
+r_right', 'btn_dpad1_left', 'btn_dpad1_right', 'btn_dpad1_up', 'btn_dpad1_down', 'btn_dpad2_left', 'btn_dpad2_right', 'btn_dpad2_up', 'btn_dpad2_down'%5D,%0A             'compat' : %5B'axis_dpad1_x', 'axis_dpad1_y', 'btn_trigger_left', 'btn_trigger_right', 'axis_dpad2_x', 'axis_dpad2_y', 'axis_x_inverted', 'axis_y_inverted', 'axis_trigger_left_inverted', 'axis_trigger_right_inverted
 '%5D%0A%0A

add all values in the available configuration list