package main;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import bullithell.images.Images;
import net.java.games.input.Component;
import net.java.games.input.Component.Identifier;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

/**
 *
 * Joystick Test with JInput
 *
 *
 * @author TheUzo007 
 *         http://theuzo007.wordpress.com
 *
 * Created 22 Oct 2013
 *
 */
public class Joystick implements Runnable{
    private int xAxisPercentage, yAxisPercentage;
    private ArrayList<Controller> foundControllers;

    public Joystick() {
        foundControllers = new ArrayList<>();
        searchForControllers();
		if(!foundControllers.isEmpty())
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
		            readData(foundControllers.get(0));
				}
			}, 0, 100);
        	
    }

    private void searchForControllers() {
        Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
        for(int i = 0; i < controllers.length; i++){
            Controller controller = controllers[i];
            if (
                    controller.getType() == Controller.Type.STICK || 
                    controller.getType() == Controller.Type.GAMEPAD || 
                    controller.getType() == Controller.Type.WHEEL ||
                    controller.getType() == Controller.Type.FINGERSTICK
               )
            {
                // Add new controller to the list of all controllers.
                foundControllers.add(controller);
                
            }
        }
    }
    
    private void readData(Controller controller){
        // Pull controller for current data, and break while loop if controller is disconnected.
        if( !controller.poll() ){
            return;
        }
        
        // Go trough all components of the controller.
        Component[] components = controller.getComponents();
        for(int i=0; i < components.length; i++)
        {
            Component component = components[i];
            Identifier componentIdentifier = component.getIdentifier();
            
            
            // Axes
            if(component.isAnalog()){
                float axisValue = component.getPollData();
                int axisValueInPercentage = getAxisValueInPercentage(axisValue);
                
                // X axis
                if(componentIdentifier == Component.Identifier.Axis.X){
                    xAxisPercentage = axisValueInPercentage;
                    continue; // Go to next component.
                }
                // Y axis
                if(componentIdentifier == Component.Identifier.Axis.Y){
                    yAxisPercentage = axisValueInPercentage;
                    continue; // Go to next component.
                }
            }
        }
    }
    private int getAxisValueInPercentage(float axisValue)
    {
        return (int)(((2 - (1 - axisValue)) * 100) / 2);
    }

	public int getxAxisPercentage() {
		return xAxisPercentage;
	}

	public int getyAxisPercentage() {
		return yAxisPercentage;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}


    
}
