//Adam Kwong
//TEAM ALPAXA
//1-21-17

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using XInputDotNetPure; // added for rumble support

public class Controller : MonoBehaviour {

    // Goal: map controls to controller

    // joysticks,dpad,trigger inputs
    public int playernum = 1; // input manager updated with to indicate player one
    public float h_axis; //   left joystick x axis
    public float v_axis; //   left joystick y axis
    public float rh_axis; //  right joystick x axis
    public float rv_axis; //  right joystick y axis
    public float dh_axis; //  dpad x axis
    public float dv_axis; //  dpadjoystick y axis
    public float triggers; // R and L triggers
    // button input p1
    public bool a_button; // a button
    public bool b_button; // b button
    public bool x_button; // x button
    public bool y_button; // y button
    public bool rb_button; // right bumper
    public bool lb_button;// left bumper
    public bool rs_button; // right stick click
    public bool ls_button; // left stick click
    public bool bk_button; // back button
    public bool st_button; // start button

    public float moveForce = 1; // speed value

    public static readonly float MediumVibrationFactor = .5f;
    public static readonly float HighVibrationFactor = 1.0f;
    public static readonly float LowVibrationFactor = .25f;

    //new code for rumble?
    /*
    private Dictionary<int, PlayerIndex> GamepadNumbers = new Dictionary<int, PlayerIndex>()
    {
        { 1, PlayerIndex.One },
        { 2, PlayerIndex.One},
        { 3, PlayerIndex.One},
        { 4, PlayerIndex.One},
    };
    public PlayerIndex GamepadNumber
    {
        get {
            if(!this.GamepadNumbers.ContainsKey(this.playernum))
            {
                return PlayerIndex.One;
            }

            return this.GamepadNumbers[this.playernum];
        }
    }
    */

    int dInput(string control)//detects input, string being axes name
    {
        if (Input.GetAxisRaw(control) > 0) 
        {
            return 1; // return 1 for any value greater than 0
        }
        else if (Input.GetAxisRaw(control) < 0)
        {
            return -1; // return 1 for any value greater than 0
        }
        else
        {
            return 0;
        }
    }
    // Use this for initialization
    void Start () {
        
	}

    public void Vibrate(float lowFreqStrength, float highFreqStrength, float duration = 1)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.OSXPlayer) {
            StartCoroutine(DoVibrate(lowFreqStrength, highFreqStrength, duration));
        }
    }
    private IEnumerator DoVibrate(float lowFreqStrength, float highFreqStrength, float duration)
    {
        //GamePad.SetVibration(GamepadNumber, lowFreqStrength, highFreqStrength);
        {
            yield return new WaitForSeconds(duration);
            // Disabled because it doesn't work on OSX.
            //GamePad.SetVibration(GamepadNumber, 0f, 0f);
        }
    }
	
	// Update is called once per frame
	void Update () {
        // Player 1
        h_axis = Input.GetAxisRaw("lHorizontal" + playernum);// * moveForce; // left stick analog X value applied with force
        v_axis = Input.GetAxisRaw("lVertical" + playernum) * -1;// * moveForce *-1; //left stick analog Y value applied with force

        if (Mathf.Abs(h_axis) < 0.00001)
        {
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                h_axis = -1;
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                h_axis = 1;
            }
        }
        ////h_axis = dInput("lHorizontal") * moveForce; //    left stick digital X value applied with force
        //// v_axis = dInput("lVertical") * moveForce *-1; //   left stick digital Y value applied with force
        //// note: joystick y axis is inversed (up is -1, down is 1)
        //rh_axis = Input.GetAxisRaw("rHorizontal" + playernum) * moveForce; // right stick shooting X value
        //rv_axis = Input.GetAxisRaw("rVertical" + playernum) * moveForce *-1; // right stick shooting y value
        //// note: joystick y axis is inversed (up is -1, down is 1)
        //dh_axis = Input.GetAxisRaw("dHorizontal" + playernum); // dpad X value applied 
        //dv_axis = Input.GetAxisRaw("dVertical" + playernum); // dpad Y value applied 
        //// note: joystick y axis is inversed (up is -1, down is 1)
        //triggers = Input.GetAxisRaw("triggers" + playernum); //dpad Y value applied 
        //// left trigger increases to 1, right trigger decreases to -1
        //// bool value for all button values
        a_button = Input.GetButton("A" + playernum); // A button
        b_button = Input.GetButton("B" + playernum); // B button
        x_button = Input.GetButton("X" + playernum); // X button
        y_button = Input.GetButton("Y" + playernum); // Y button
        //rb_button = Input.GetButton("rBumper" + playernum); // right bumper button
        //lb_button = Input.GetButton("lBumper" + playernum); // left bumper button
        //rs_button = Input.GetButton("rStickClick" + playernum); // right bumper button
        //ls_button = Input.GetButton("lStickClick" + playernum); // left bumper button
        //bk_button = Input.GetButton("back" + playernum); // right bumper button
        //st_button = Input.GetButton("start" + playernum); // left bumper button
                                                          // tie triggers with input

        //transform.Translate(new Vector2(h_axis, 0) * Time.deltaTime); // moves character on x axis
        //transform.Translate(new Vector2(0,v_axis) * Time.deltaTime); // moves c
        // Update: no longer using transform, using force.

        // Right bumper = fire (hold to increase distance)
        // Right Stick = aiming, twin stick style
    }

}
