using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{
    #region Fields
    [SerializeField] private float _speed = 5.0f;
    [SerializeField] private float _radius = 0.5f;
    [SerializeField] private Vector2 _offset = Vector2.zero;
    [SerializeField] private Animator _animator = null;
    [SerializeField] private GameObject _stepVFXLeft = null;
    [SerializeField] private GameObject _stepVFXRight = null;
    [SerializeField] private SpriteRenderer _spriteRenderer = null;
    [SerializeField] private LayerMask _buttonLayerMask = 0;
    [SerializeField] private AudioSource _playerInteractSFX = null;
    private Rigidbody2D _rigidbody2D = null;
    private Vector2 _input = Vector2.zero;
    private bool _interact = false;
    private bool _lookingLeft = false;

    #endregion Fields

    #region Properties
    #endregion Properties

    #region Methods
    void Start()
    {
        _rigidbody2D = GetComponent<Rigidbody2D>();
    }

    private void OnMove(InputValue inputValue)
    {
        _input.x = inputValue.Get<Vector2>().x;
    }

    private void OnInteract(InputValue inputValue)
    {
        if (GameManager.Instance.IsGamePlaying == false || _interact)
            return;
        float interact = inputValue.Get<float>();
        if (interact > 0.5f)
        {
            _animator.SetTrigger("Interact");
        }
        StartCoroutine(EndInvoke());
        Vector2 offset = _lookingLeft ? _offset * new Vector2(-1.0f, 1.0f) : _offset;
        Collider2D[] colliders = Physics2D.OverlapCircleAll((Vector2)transform.position + offset, _radius, _buttonLayerMask);
        foreach (Collider2D collider in colliders)
        {
            if (collider.TryGetComponent(out BunkerStation bunkerStation))
            {
                bunkerStation.Press();
				_playerInteractSFX.Play();
            }
        }
        Debug.DrawLine((Vector2)transform.position + offset, (Vector2)transform.position + offset + Vector2.one * _radius, Color.blue, 0.5f);
        Debug.DrawLine((Vector2)transform.position + offset, (Vector2)transform.position + offset + Vector2.one * _radius * -1.0f, Color.blue, 0.5f);
    }

    private IEnumerator EndInvoke()
    {
        _interact = true;
        yield return new WaitForSeconds(0.5f);
        _interact = false;
    }

    void FixedUpdate()
    {
        if (GameManager.Instance.IsGamePlaying == false || _interact)
        {
            _rigidbody2D.velocity = Vector2.zero;
        }
        else
        {
            _rigidbody2D.velocity = _input * _speed;
        }
        bool run = _rigidbody2D.velocity.sqrMagnitude > 0.0f;
        _animator.SetBool("Run", run);
		
        if (run)
        {
            _lookingLeft = _rigidbody2D.velocity.x < 0.0f;
            _spriteRenderer.flipX = _lookingLeft;
			
            _stepVFXLeft.SetActive(_lookingLeft);
            _stepVFXRight.SetActive(!_lookingLeft);
        } 
		else 
		{
			_stepVFXLeft.SetActive(false);
            _stepVFXRight.SetActive(false);
		}
    }
    #endregion Methods
}
