/// <summary>
				/// Covenvert profiles string (see example) to int code
				/// </summary>
				/// <param name="codeString"><example>Left Stick XPositive,DPAD_Left,X,A,B</example></param>
				/// <param name="profile"></param>
				/// <returns></returns>
				internal static int toCode (string codeString, DeviceProfile profile)
				{

						int inx;

						

						//CHECK  POSITION IN AXIS 
						var positions = (JoystickPosition[])Enum.GetValues (typeof(JoystickPosition));


						foreach (var position in positions) {

								if (codeString.Contains (position.ToString ())) {
										codeString = codeString.Replace (position.ToString (), "");
										inx = Array.IndexOf (profile.axisNaming, codeString);

										if (inx < 0)
												Debug.LogError ("Wrong profile naming " + codeString + " in profile " + profile.Name);

										return InputCode.toCode (Joysticks.Joystick, (JoystickAxis)inx, position);
								}



						}

						//CHECK POV POSITION IN AXIS or BUTTONS
						var povPositions = (JoystickPovPosition[])Enum.GetValues (typeof(JoystickPovPosition));

						foreach (var position in povPositions) {

								if (codeString.Contains (position.ToString ())) {
										//check if the pov is represented as buttons or "Left Bumper" or "DPAD_Left" contains Left
										inx = Array.IndexOf (profile.buttonNaming, codeString);

										if (inx > -1)
												return InputCode.toCode (Joysticks.Joystick, JoystickAxis.None, inx);


										// code = position.ToString(); 


										if (position == JoystickPovPosition.Backward || position == JoystickPovPosition.Forward)
												return InputCode.toCode (Joysticks.Joystick, JoystickAxis.AxisPovY, position);

										if (position == JoystickPovPosition.Left || position == JoystickPovPosition.Right)
												return InputCode.toCode (Joysticks.Joystick, JoystickAxis.AxisPovX, position);

										if (inx < 0)
												Debug.LogError ("Wrong profile naming " + codeString + " in profile " + profile.Name);


								}



						}


						//check any other button
						inx = Array.IndexOf (profile.buttonNaming, codeString);

						if (inx < 0)
								Debug.LogError ("Wrong profile naming " + codeString + " in profile " + profile.Name);

						return InputCode.toCode (Joysticks.Joystick, JoystickAxis.None, inx);


				}
				/// <summary>
				/// onGUI
				/// </summary>
				/////////////////    onGUI   ////////////
				void OnGUI ()
				{



						///////////////      DEVICES      ///////////////
						if (_profiles != null) {
								List<IDevice> devicesList = InputManager.GetDevices<IDevice> ();

                               

								if (devicesList.Count > 0) {
										_displayOptions = devicesList.Select (item => item.Name).ToArray ();

										_deviceDisplayIndex = EditorGUILayout.Popup ("Devices:", _deviceDisplayIndex, _displayOptions);

										_deviceSelected = devicesList [_deviceDisplayIndex];


                                        if (!String.IsNullOrEmpty(_profileNameSelected))
                                        {

                                            EditorGUILayout.BeginHorizontal();
                                            if (GUILayout.Button("Assign Profile"))
                                            {

                                                string pidVidKey = _deviceSelected.VID.ToString("X4") + "#" + _deviceSelected.PID.ToString("X4");
                                                _profiles.vidpidProfileNameDict[pidVidKey] = _profileNameSelected;

                                                EditorUtility.SetDirty(_profiles);
                                                AssetDatabase.SaveAssets();
                                            }

                                            if (GUILayout.Button("Remove From Profile"))
                                            {

                                                string pidVidKey = _deviceSelected.VID.ToString("X4") + "#" + _deviceSelected.PID.ToString("X4");

                                                _profiles.vidpidProfileNameDict.Remove(pidVidKey);

                                                EditorUtility.SetDirty(_profiles);
                                                AssetDatabase.SaveAssets();

                                            }

                                            EditorGUILayout.EndHorizontal();

                                        }

                                      
								} else {

										EditorGUILayout.LabelField ("Devices: No attached devices");
								}



                                if (_deviceCount != devicesList.Count)
                                    this.Repaint();

                                _deviceCount = devicesList.Count;
						}



                     


//				if(GUILayout.Button("Proifle2DeviceProfile")){
//						char splitChar = '|';
//
//						string fileBase;
//
//						foreach(var kvp in _profiles.runtimePlatformDeviceProfileDict){
//
//						fileBase=kvp.Key;
//
//						DeviceProfile profile;
//
//						profile=kvp.Value[RuntimePlatform.WindowsPlayer]=new DeviceProfile();
//						profile.Name=kvp.Key;
//						
//						using (StreamReader reader = new StreamReader(Path.Combine(Application.streamingAssetsPath, fileBase + "_win.txt")))
//						{
//							
//							if (!reader.EndOfStream)
//								profile.buttonNaming = reader.ReadLine().Split(splitChar);
//							
//							if (!reader.EndOfStream)
//								profile.axisNaming = reader.ReadLine().Split(splitChar);
//							
//							//rest in future
//							
//							
//							
//						}
//
//						profile=kvp.Value[RuntimePlatform.OSXPlayer]=new DeviceProfile();
//						profile.Name=kvp.Key;
//
//						using (StreamReader reader = new StreamReader(Path.Combine(Application.streamingAssetsPath, fileBase + "_osx.txt")))
//						{
//							
//							if (!reader.EndOfStream)
//								profile.buttonNaming = reader.ReadLine().Split(splitChar);
//							
//							if (!reader.EndOfStream)
//								profile.axisNaming = reader.ReadLine().Split(splitChar);
//							
//							//rest in future
//							
//							
//							
//						}
//
//
//					}
//
//					EditorUtility.SetDirty (_profiles);
//					AssetDatabase.SaveAssets ();
//
//				}


//								if (GUILayout.Button ("Profiles.txt2DeviceProfiles.asset")) {
//										string[] deviceNameProfilePair;
//										char splitChar = '|';
//
//										using (StreamReader reader = new StreamReader(Path.Combine(Application.streamingAssetsPath, "profiles.txt"))) {
//					
//					
//												while (!reader.EndOfStream) {
//						
//														deviceNameProfilePair = reader.ReadLine ().Split (splitChar);
//														if (!_profiles.vidpidProfileNameDict.ContainsKey (deviceNameProfilePair [0]))
//																_profiles.vidpidProfileNameDict [deviceNameProfilePair [0]] = deviceNameProfilePair [1];
//
//														if (!_profiles.runtimePlatformDeviceProfileDict.ContainsKey (deviceNameProfilePair [1])) {
//
//																_profiles.runtimePlatformDeviceProfileDict [deviceNameProfilePair [1]] = new Dictionary<RuntimePlatform, DeviceProfile> ();
//														}
//												}
//
//												EditorUtility.SetDirty (_profiles);
//												AssetDatabase.SaveAssets ();
//					
//										}
//								}

							
						
						

						EditorGUILayout.Separator ();



						///////////////  PROFILES /////////////

						_profiles = EditorGUILayout.ObjectField (_profiles, typeof(DeviceProfiles), false) as DeviceProfiles;

						if (_deviceSelected != null && _profiles != null) {
								

								_displayOptions = _profiles.runtimePlatformDeviceProfileDict.Keys.ToArray ();

								if (_displayOptions.Length > 0) {
										_profileIndexSelected = EditorGUILayout.Popup ("Profiles:", _profileIndexSelected, _displayOptions);
										_profileNameSelected = _displayOptions [_profileIndexSelected];
								}

								EditorGUILayout.BeginHorizontal ();

								_profileName = EditorGUILayout.TextField ("Name", _profileName);

								if (GUILayout.Button ("Add profile") && !String.IsNullOrEmpty (_profileName)) {
										

										if (!_profiles.runtimePlatformDeviceProfileDict.ContainsKey (_profileName)) {

											
												_profiles.runtimePlatformDeviceProfileDict [_profileName] = new Dictionary<RuntimePlatform, DeviceProfile> ();
										} else {

												Debug.LogWarning ("Already contain such profile");
										}

										_profileName = String.Empty;

										EditorUtility.SetDirty (_profiles);
										AssetDatabase.SaveAssets ();

								}


								


								EditorGUILayout.EndHorizontal ();

								EditorGUILayout.Separator ();
				

								///////////////// NAMING(buttons,axis..) //////////////
								string actionCodeString = "Click button or Move stick";
								DeviceProfile profile = null;
								JoystickAxis axis = JoystickAxis.None;
								string nameGivenCurrent = "No Name";
			
								if (!String.IsNullOrEmpty (_profileNameSelected)) {
										///// CURRENT ACTION /////
										/// 
										/// 
										EditorGUILayout.BeginHorizontal ();
										EditorGUILayout.LabelField ("GENERIC NAME:");

										

										if (_actionSelected != null) {


												actionCodeString = _actionSelected.codeString;
												//find axis button number
												axis = InputCode.toAxis (_actionSelected.getCode (_deviceSelected));

												_platform = Application.platform == RuntimePlatform.OSXEditor ? RuntimePlatform.OSXPlayer : RuntimePlatform.WindowsPlayer;
											
											
											
											
												if (_profiles.runtimePlatformDeviceProfileDict [_profileNameSelected].ContainsKey (_platform)) {
														profile = _profiles.runtimePlatformDeviceProfileDict [_profileNameSelected] [_platform];
												
												
												
												
														if (axis == JoystickAxis.None) {
																nameGivenCurrent = profile.buttonNaming [InputCode.toData (_actionSelected.getCode (_deviceSelected))];
														} else {
																nameGivenCurrent = profile.axisNaming [(int)axis];
														}
												}
										}

										EditorGUILayout.LabelField (actionCodeString);

										EditorGUILayout.EndHorizontal ();

										EditorGUILayout.Separator ();

										///// GIVE NAME ////			
										EditorGUILayout.BeginHorizontal ();



                                        if (String.IsNullOrEmpty(nameGivenCurrent))
                                        {
                                            nameGivenCurrent = "No Name [Click to Edit]";
                                            _nameGiven = String.Empty;
                                        }
                                        else
                                            _nameGiven = nameGivenCurrent;

										if (_actionSelected != null)
										if (!_nameGiveEdit
												&& GUILayout.Button (nameGivenCurrent))
												_nameGiveEdit = true;


										if (_nameGiveEdit) {
												_nameGiven = EditorGUILayout.TextField ("Name", _nameGiven);
				

									

												if (GUILayout.Button ("Give") || (Event.current.isKey && Event.current.keyCode == KeyCode.Return))
												if (!String.IsNullOrEmpty (_profileNameSelected) && !String.IsNullOrEmpty (_nameGiven)) {


									
							
									
														if (profile == null) {
																profile = new DeviceProfile ();
																profile.Name = _profileNameSelected;
																_profiles.runtimePlatformDeviceProfileDict [_profileNameSelected] [_platform] = profile;
										
														}

														if (axis == JoystickAxis.None) {
																profile.buttonNaming [InputCode.toData (_actionSelected.getCode (_deviceSelected))] = _nameGiven;
														} else {
																profile.axisNaming [(int)axis] = _nameGiven;
														}
											
									
														_actionSelected = null;
														_nameGiven = String.Empty;

														EditorUtility.SetDirty (_profiles);

														AssetDatabase.SaveAssets ();


														_nameGiveEdit = false;

														this.Repaint ();
										
												}



										}


										EditorGUILayout	.EndHorizontal ();
								}
						}
						

						///////////////         CREATE ASSET       ////////////////
						if (GUILayout.Button ("Create Assets/Resources/DeviceProfiles.asset")) {

								if (!Directory.Exists (Path.Combine (Application.dataPath, "Resources")))
										AssetDatabase.CreateFolder ("Assets", "Resources");
								
								if (File.Exists (Path.Combine (Path.Combine (Application.dataPath, "Resources"), "DeviceProfiles.asset"))) {
					
										if (EditorUtility.DisplayDialog ("DeviceProfiles Asset Exists!",
					                            "Are you sure you overwrite?", "Yes", "Cancel")) {
												AssetDatabase.CreateAsset (ScriptableObject.CreateInstance<DeviceProfiles> (), "Assets/Resources/DeviceProfiles.asset");
										}
								} else {
										AssetDatabase.CreateAsset (ScriptableObject.CreateInstance<DeviceProfiles> (), "Assets/Resources/DeviceProfiles.asset");
								}
				
				
						}






						//if event is of key or mouse
						if (Event.current.isKey) {
									
								if (Event.current.keyCode == KeyCode.Escape) {
										_nameGiveEdit = false;
										_instance.Repaint ();
								}		
						}
			
			
				}