using System.Collections.Generic;
using UnityEngine;
using static Extensions;

[CreateAssetMenu(menuName = project_name + "/" + nameof(ResourceTile))]
public class ResourceTile : LevelTile
{
    [SerializeField]
    [Tooltip("the prefab of the currency that will be spawned when mining this resource")]
    private GameObject _yieldPrefab;
    [SerializeField]
    private Enum.ResourceNodeType _resourceNodeType;

    public Enum.ResourceNodeType ResourceNodeType => _resourceNodeType;
    public GameObject YieldPrefab => _yieldPrefab;
    private string YieldPrefabName => _yieldPrefab.name;

    [SerializeField]
    [Range(0.0f, 5.0f)]
    private float _randomPositionConfig = 0.5f;
    private float _yieldCounter = 0;

    private float _yieldSpeedMultiplier = 1.0f;

    public override void LevelStart()
    {
        base.LevelStart();
        ResetYieldDuration();
    }
    public override void LevelUpdate()
    {
        //check if there is an harvester unit on top
        var harvester = LevelManager.Instance.Get<Harvester>(x => x.Position.IsContainedIn(Position));
        if (!harvester) return;

        _yieldCounter -= Time.deltaTime * harvester.GatherRate;
        if (_yieldCounter > 0) return;

        ResetYieldDuration();

        if (_yieldPrefab != null)
        {
            YieldResource();
        }
    }
    private void YieldResource()
    {
        float rangeConfig = 0.5f + _randomPositionConfig;
        Vector3 yieldPosition = new Vector3(Position.x + Random.Range(-rangeConfig, rangeConfig), Position.y, Position.z);
        var yielded = Instantiate(_yieldPrefab, yieldPosition, Quaternion.identity);
        yielded.transform.SetParent(LevelManager.Instance.LevelTransform);
    }
    private void ResetYieldDuration()
    {
        _yieldCounter = 0;
        var config = GlobalConfig.Instance.Current;
        if (!config) return;

        if (config.useRandomHarvestDuration)
        {
            float min = config.randomHarvestDurationMinimum,
                  max = config.randomHarvestDurationMaximum;
            _yieldCounter = Random.Range(min, max) / _yieldSpeedMultiplier;
        }
        else
        {
            _yieldCounter = config.harvestDuration / _yieldSpeedMultiplier;
        }
    }
    public override bool Equals(ILevelObject other)
    {
        return other is ResourceTile otherRes
            && base.Equals(otherRes)
            && _yieldPrefab == otherRes._yieldPrefab;
    }
    public override Dictionary<string, object> ToDictionary()
    {
        var dict = base.ToDictionary();

        dict[nameof(YieldPrefabName)] = YieldPrefabName;
        return dict;
    }
    public override void LoadDictionary(Dictionary<string, object> dict)
    {
        base.LoadDictionary(dict);
        var prefabName = dict[nameof(YieldPrefabName)].ToString();
        _yieldPrefab = Database.Instance.Prefabs[prefabName];
    }

    // GETTERS AND SETTERS

    public float YieldSpeedMultiplier
    {
        get { return _yieldSpeedMultiplier; }
        set { _yieldSpeedMultiplier = value; }
    }
}