using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerControl : MonoBehaviour
{
    private CharacterController Figure;
    private Vector3 MoveVector = Vector3.zero;
    private InputAction moveAction; // for new Input handling
    private InputAction jumpAction; // for new Input handling

    private SpriteRenderer renderer;
    private float current = 0.0f;

    [Header("Movement")]
    public float Speed = 5.0f;
    public float Jump = 5.0f;
    public float Gravity = 15.0f;

    [Header("Sprites")]
    public Sprite front, back;
    public Sprite right1, right2;
    public Sprite left1, left2;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        Figure = GetComponent<CharacterController>();
        renderer = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        // old Input handling - used in book
        /*
        if (Input.GetKey(KeyCode.LeftArrow))
            Figure.Move(Vector3.left * Time.deltaTime * Speed);
        if (Input.GetKey(KeyCode.RightArrow))
            Figure.Move(Vector3.right * Time.deltaTime * Speed);
        */
        /*
        if (Figure.isGrounded){
            MoveVector.x = Input.GetAxis("Horizontal");
            if(Input.GetButton("Jump")) MoveVector.y = Jump;
        }
        MoveVector.y -= Gravity * Time.deltaTime;
        Figure.Move(MoveVector * Time.deltaTime * Speed);
        */
        /*
        if (MoveVector.x < 0) renderer.sprite = left1;
        else if (MoveVector.x > 0) renderer.sprite = right1;
        else renderer.sprite = front;
        */

        //new Input handling - arrows and wasd
        /*
        if (Keyboard.current.leftArrowKey.isPressed || Keyboard.current.aKey.isPressed)
            Figure.Move(Vector3.left * Time.deltaTime * Speed);
        if (Keyboard.current.rightArrowKey.isPressed || Keyboard.current.dKey.isPressed)
            Figure.Move(Vector3.right * Time.deltaTime * Speed);
        if (Keyboard.current.upArrowKey.isPressed || Keyboard.current.wKey.isPressed)
            Figure.Move(Vector3.up * Time.deltaTime * Speed);
        if (Keyboard.current.downArrowKey.isPressed || Keyboard.current.sKey.isPressed)
            Figure.Move(Vector3.down * Time.deltaTime * Speed);
        */
        
        Vector2 input = moveAction.ReadValue<Vector2>();
        if (Figure.isGrounded){
            MoveVector.x = input.x;
            if (jumpAction.WasPressedThisFrame()) MoveVector.y = Jump;
        }
        MoveVector.y -= Gravity * Time.deltaTime;
        Figure.Move(MoveVector * Time.deltaTime * Speed);

        if (MoveVector.x < 0) goLeft();
        else if (MoveVector.x > 0) goRight();
        else renderer.sprite = front;
    }

    void OnEnable() // for new Input handling
    {
        moveAction = new InputAction(type: InputActionType.Value);
        moveAction.AddCompositeBinding("2DVector")
            .With("Left", "<Keyboard>/a")
            .With("Right", "<Keyboard>/d")
            .With("Up", "<Keyboard>/w")
            .With("Down", "<Keyboard>/s");
        moveAction.AddCompositeBinding("2DVector")
            .With("Left", "<Keyboard>/leftArrow")
            .With("Right", "<Keyboard>/rightArrow")
            .With("Up", "<Keyboard>/upArrow")
            .With("Down", "<Keyboard>/downArrow");

        jumpAction = new InputAction("Jump", InputActionType.Button);
        jumpAction.AddBinding("<Keyboard>/space");
        jumpAction.AddBinding("<Keyboard>/w");
        jumpAction.AddBinding("<Keyboard>/upArrow");

        moveAction.Enable();
        jumpAction.Enable();
    }

    void OnDisable() // for new Input handling
    {
        moveAction.Disable();
        jumpAction.Disable();
    }

    private void goLeft(){
        current += 0.1f;
        if (current < 10.0f) return;
        renderer.sprite = renderer.sprite == left1 ? left2 : left1;
        current = 0.0f;
    }

    private void goRight(){
        current += 0.1f;
        if (current < 10.0f) return;
        renderer.sprite = renderer.sprite == right1 ? right2 : right1;
        current = 0.0f;
    }
}
