using UnityEngine;

/// <summary>
/// This class is a simple example of how to build a controller that interacts with PlatformerMotor2D.
/// </summary>
[RequireComponent(typeof(PlatformerMotor2D))]
public class PlayerController2D : MonoBehaviour
{
    private PlatformerMotor2D _motor;
    private bool _restored = true;
    private bool _enableOneWayPlatforms;
    private bool _oneWayPlatformsAreWalls;

    // Use this for initialization
    void Start()
    {
        _motor = GetComponent<PlatformerMotor2D>();
    }

    // before enter en freedom state for ladders
    void FreedomStateSave(PlatformerMotor2D motor)
    {
        if (!_restored) // do not enter twice
            return;

        _restored = false;
        _enableOneWayPlatforms = _motor.enableOneWayPlatforms;
        _oneWayPlatformsAreWalls = _motor.oneWayPlatformsAreWalls;
    }
    // after leave freedom state for ladders
    void FreedomStateRestore(PlatformerMotor2D motor)
    {
        if (_restored) // do not enter twice
            return;

        _restored = true;
        _motor.enableOneWayPlatforms = _enableOneWayPlatforms;
        _motor.oneWayPlatformsAreWalls = _oneWayPlatformsAreWalls;
    }

    // Update is called once per frame
    void Update()
    {
        // use last state to restore some ladder specific values
        if (_motor.motorState != PlatformerMotor2D.MotorState.FreedomState)
        {
            // try to restore, sometimes states are a bit messy because change too much in one frame
            FreedomStateRestore(_motor);
        }

        // Jump?
        // If you want to jump in ladders, leave it here, otherwise move it down
        if (Input.GetButtonDown(PC2D.Input.JUMP))
        {
            _motor.Jump();
            _motor.DisableRestrictedArea();
        }

        _motor.jumpingHeld = Input.GetButton(PC2D.Input.JUMP);

        if (Input.GetKey(KeyCode.D))
        {
            if (!Character.Instance.IsFacingRight)
            {
                Character.Instance.IsFacingRight = true;
                transform.localScale = new Vector2(1, 1);
            }
            else
            {
                _motor.normalizedXMovement = 1;
            }
        }
        else if (Input.GetKey(KeyCode.A))
        {
            if (Character.Instance.IsFacingRight)
            {
                Character.Instance.IsFacingRight = false;
                transform.localScale = new Vector2(-1, 1);
            }
            else
            {
                _motor.normalizedXMovement = -1;
            }
        }
        else
        {
            _motor.normalizedXMovement = -0;
        }

//        // XY freedom movement
//        if (_motor.motorState == PlatformerMotor2D.MotorState.FreedomState)
//        {
//            _motor.normalizedXMovement = Input.GetAxis(PC2D.Input.HORIZONTAL);
//
//            return; // do nothing more
//        }
//
//        // X axis movement
//        if (Mathf.Abs(Input.GetAxis(PC2D.Input.HORIZONTAL)) > PC2D.Globals.INPUT_THRESHOLD)
//        {
//            float h = Input.GetAxis(PC2D.Input.HORIZONTAL);
//
//            if (h < 0 && Character.Instance.IsFacingRight)
//            {
//                Character.Instance.IsFacingRight = false;
//                transform.localScale = new Vector2(-1, 1);
//            }
//            else if (h > 0 && !Character.Instance.IsFacingRight)
//            {
//                Character.Instance.IsFacingRight = true;
//                transform.localScale = new Vector2(1, 1);
//            }
//            else
//            {
//                _motor.normalizedXMovement = h;
//            }
//        }
//        else
//        {
//            _motor.normalizedXMovement = 0;
//        }



    }
}
