using UnityEngine;
using UnityEngine.UI;
using XInputDotNetPure;

namespace Assets.Scripts.GamepadInterface
{
    public class TestScript : MonoBehaviour
    {
        public Text Display;

        private PlayerIndex _player;
        private bool _gamepadActive;

        void Start ()
        {
            FindPlayer();
        }

        void FindPlayer()
        {
            var test = Controller.FindController();
            if (test == null) return;
            _player = (PlayerIndex) test;
            _gamepadActive = true;
        }

        void Update ()
        {
            UpdateGamepad();
        }

        void UpdateGamepad()    //This could also go in FixedUpdate
        {
            if (!_gamepadActive) return;

            var state = Controller.GetState(_player);
            if (!state.IsConnected) return;

            var output = "";
            output += string.Format("({0:n2},{1:n2}), ({2:n2},{3:n2}), A: {4}, B: {5}, X: {6}, Y: {7}", state.Left.X, state.Left.Y, state.Right.X, state.Right.Y, state.A.Hold, state.B.Hold, state.X.Hold, state.Y.Hold);
            output += string.Format("\nBack: {0}, Start: {1}", state.Back.Hold, state.Start.Hold);
            output += string.Format("\nLB: {0}, RB: {1}, LeftClick: {2}, RightClick: {3}", state.LeftBumper.Hold, state.RightBumper.Hold, state.Left.Click.Hold, state.Right.Click.Hold);
            output += string.Format("\nLT: {0}, RT: {1}", state.LeftTrigger.Value, state.RightTrigger.Value);
            output += string.Format("\nUp: {0}, Down: {1}, Left: {2}, Right: {3}", state.Dpad.Up.Hold, state.Dpad.Down.Hold, state.Dpad.Left.Hold, state.Dpad.Right.Hold);

            state.SetRumble(_player, state.LeftTrigger.Value, state.RightTrigger.Value);
            Display.text = output;
        }
    }
}
