using System;
using System.ComponentModel.Design;
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float maxFallSpeed = -20f;
    public float maxFallSpeedDamge = -10f;
    private float maxFallSpeedCurrent = 0f;
    Vector2 moveInput;

    Rigidbody2D rb;
    Animator animator;
    Animator animator_rotor;
    public GameObject rotorGO;
    TouchingDirections touchingDirections;
    Damageable health;

    [SerializeField]
    private bool _isMoving;
    public bool IsMoving
    {
        get { return _isMoving; }
        private set
        {
            _isMoving = value;
            animator.SetBool(AnimationStrings.Player.isMoving, value);
        }
    }

    [SerializeField]
    private bool _isFlying;
    public bool IsFlying
    {
        get { return _isFlying; }
        private set
        {
            _isFlying = value;
            animator.SetBool(AnimationStrings.Player.isFlying, value);
            animator_rotor.SetBool(AnimationStrings.Player.isFlying, value);
        }
    }

    private bool _isFacingRight = true;

    public bool IsFacingRight
    {
        get { return _isFacingRight; }
        set
        {
            if (_isFacingRight != value)
            {
                transform.localScale *= new Vector2(-1, 1);
            }
            _isFacingRight = value;
        }
    }

    public bool CanMove
    {
        get
        {
            return animator.GetBool(AnimationStrings.canMove);
        }
    }



    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        animator_rotor = rotorGO.GetComponent<Animator>();
        touchingDirections = GetComponent<TouchingDirections>();
        health = GetComponent<Damageable>();
    }

    private void FixedUpdate()
    {
        if (moveInput.y == 0)
        {
            if (rb.velocity.y <= maxFallSpeed)
            {
                // max fall speed, dont accelerate more
                rb.velocity = new Vector2(moveInput.x * moveSpeed, maxFallSpeed);
            }
            else
            {
                // normal fall
                rb.velocity = new Vector2(moveInput.x * moveSpeed, rb.velocity.y);
            }
        }
        else
        {
            if (rb.velocity.y < 0 && moveInput.y > 0)
            {
                // falling but moving upwards
                rb.velocity = new Vector2(moveInput.x * moveSpeed, (moveInput.y * moveSpeed) + rb.velocity.y);
            }
            else
            {
                // moving upwards no falling
                rb.velocity = new Vector2(moveInput.x * moveSpeed, (moveInput.y * moveSpeed));
            }
        }
        if (touchingDirections.IsGrounded)
        {
            if (maxFallSpeedCurrent < maxFallSpeedDamge)
            {
                TakeFallDamage(Math.Abs((int)maxFallSpeedCurrent));
                maxFallSpeedCurrent = 0;
            }
        }
        else if (IsFlying)
        {
            maxFallSpeedCurrent = rb.velocity.y;
        }
        else
        {
            if (maxFallSpeedCurrent > rb.velocity.y)
            {
                maxFallSpeedCurrent = rb.velocity.y;
            }
        }
    }

    public void OnMove(InputAction.CallbackContext context)
    {
        if (CanMove)
        {
            moveInput = context.ReadValue<Vector2>();

            IsMoving = moveInput.x != 0;

            IsFlying = (moveInput.y != 0);

            SetFacingDirection(moveInput);
        }
        else
        {
            IsMoving = false;
            IsFlying = false;
            moveInput = Vector2.zero;
        }
    }

    private void SetFacingDirection(Vector2 moveInput)
    {
        if (moveInput.x > 0 && !IsFacingRight)
        {
            IsFacingRight = true;
        }
        else if (moveInput.x < 0 && IsFacingRight)
        {
            IsFacingRight = false;
        }
    }

    public void TakeFallDamage(int fallDamage)
    {
        health.Hit(fallDamage);
    }

    //public void OnRun(InputAction.CallbackContext context)
    //{
    //    if (context.started) // Button pressed
    //    {
    //        _isRunning = true;
    //    } 
    //    else if (context.canceled) // Button released
    //    {
    //        _isRunning = false;
    //    }

    //}
}