using System.Collections;
using UnityEngine;
/// <summary>
/// Manages resource production, and serves as a countdown to leave a resource on the ground
/// </summary>
public class ResourceMaker : MonoBehaviour
{
    [SerializeField] 
    private Enum.ResourceChoice _resourceChoice;
    private ResourceManager _resourceManagerInstance;
    [SerializeField]
    private bool _animate = true;
    private GameObject _destination;
    private Vector2 _startPosition;
    private Rigidbody2D _rigidbody;
    private float _desiredTime = 1.5f;
    private float _timePassed = 0f;
    private bool _isPlaying = false;

    public Enum.ResourceChoice ResourceChoice => _resourceChoice;

    private void Start()
    {
        _resourceManagerInstance = ResourceManager.Instance;
        if (!_animate) return;

        _startPosition = transform.position;
        transform.position = new Vector2(transform.position.x, transform.position.y + 0.5f);
        _rigidbody = GetComponent<Rigidbody2D>();
        _rigidbody.AddForce(transform.up * 20.0f);
        _rigidbody.gravityScale = 0.1f;
        switch (_resourceChoice)
        {
            case Enum.ResourceChoice.Rock:
                _destination = GameObject.FindWithTag("Rock");
                break;
            case Enum.ResourceChoice.Wood:
                _destination = GameObject.FindWithTag("Wood");
                break;
            case Enum.ResourceChoice.Food:
                _destination = GameObject.FindWithTag("Food");
                break;
            default:
                _destination = GameObject.FindWithTag("Food");
                break;
        }

        // Auto collect resource
        StartCoroutine(AutoCollect());
    }

    private IEnumerator AutoCollect()
    {
        yield return new WaitForSeconds(Random.Range(1f, 1.3f));
        if(!_isPlaying)
            ClickOnResource();
    }

    /// <summary>
    /// Depending on the resource choice, chooses the prefab it should instantiate
    /// </summary>
    private void Update()
    {
        if (!_animate) return;
        if (Vector2.Distance(transform.position, _startPosition) < 0.01f)
        {
            _rigidbody.gravityScale = 0.0f;
            _rigidbody.velocity = Vector2.zero;
        }

        if (_isPlaying)
        {
            _timePassed += Time.deltaTime;
            float duration = _timePassed / _desiredTime;
            duration = duration * duration * (3.0f - 2.0f * duration);
            transform.position = Vector2.Lerp(transform.position, _destination.transform.position, duration);
            if (Vector2.Distance(transform.position, _destination.transform.position) < 0.001f)
            {
                _isPlaying = false;
                Destroy(gameObject);
            }
        }
    }

    void OnDestroy()
    {
        AddResourceToInventory(GlobalConfig.Instance.Current.harvestAmount);
    }

    public void AddResourceToInventory(int amount)
    {
        switch (_resourceChoice)
        {
            case Enum.ResourceChoice.Rock:
                _resourceManagerInstance.RockAmount += amount;
                break;
            case Enum.ResourceChoice.Wood:
                _resourceManagerInstance.WoodAmount += amount;
                break;
            case Enum.ResourceChoice.Food:
                _resourceManagerInstance.FoodAmount += amount;
                break;
        }
    }

    public void ClickOnResource()
    {
        if (!_rigidbody) return;
        _isPlaying = true;
        _rigidbody.gravityScale = 0.0f;
        _rigidbody.velocity = Vector2.zero;
    }
}
