using System.Collections;
using System.Collections.Generic;
using CodesmithWorkshop.Useful;
using TMPro;
using UnityEngine;
using VirtuoseReality.Extension.AudioManager;

public class Sheep : MonoBehaviour
{
	#region Number

    [SerializeField]
    private GameplayChannel _gameplayChannel = null;

    private float _value = 0f;

    public float value
    {
        get { return _value; }
        set
        {
            _value = value;
            _textValue.text = Mathf.FloorToInt(value).ToString();
        }
    }

    [SerializeField]
    private GameObject _starParticles = null;

    [SerializeField]
    private GameObject _heartParticles = null;

    [SerializeField]
    private Transform _particlesContainer = null;

    public TextMeshProUGUI _textValue = null;

    public Animator animatorController = null;

    private const string PARAM_STATE = "state";

    public SheepState state = SheepState.Spawn;

	#endregion

	#region Behaviour

    private void Start()
    {
        StartCoroutine("IdleSoundBehaviour");
    }

    public void Strike(WeaponType weapon)
    {
        switch (weapon)
        {
            case WeaponType.SquareRoot:
                SquareRootValue();
                break;
        }
        PlayRandomHurt();
        ChangeState(SheepState.Hit);
        _gameplayChannel.onSheepStruck.Invoke();
        Instantiate(_starParticles, _particlesContainer);
    }

    private void SquareRootValue()
    {
        if (value > 2)
        {
            value = Mathf.Sqrt(value);
        }
    }

    public void MakeHappy()
    {
        Instantiate(_heartParticles, _particlesContainer);
    }

	#endregion

    #region Animation

    public void ChangeState(SheepState newState)
    {
        if (animatorController.GetInteger(PARAM_STATE) != (int)newState)
        {
            state = newState;
            animatorController.SetInteger(PARAM_STATE, (int)state);
        }
    }
    #endregion

    #region Random Sound

    private List<string> _idleSounds = new List<string>();
    private List<string> _hurtSounds = new List<string>();

    private void Awake()
    {
        _idleSounds = new List<string>()
        {
            SoundDataIDStatic.SHEEP_IDLE01,
            SoundDataIDStatic.SHEEP_IDLE02,
            SoundDataIDStatic.SHEEP_IDLE03,
            SoundDataIDStatic.SHEEP_IDLE04,
        };

        _hurtSounds = new List<string>()
        {
            SoundDataIDStatic.SHEEP_HURT01,
            SoundDataIDStatic.SHEEP_HURT02,
            SoundDataIDStatic.SHEEP_HURT03,
            SoundDataIDStatic.SHEEP_HURT04
        };
    }

    private void PlayRandomIdle()
    {
        string rndID = UtilsClass.PickRandomInList<string>(_idleSounds);
        SoundManager.PlaySound(rndID);
    }

    private void PlayRandomHurt()
    {
        string rndID = UtilsClass.PickRandomInList<string>(_hurtSounds);
        SoundManager.PlaySound(rndID);
    }

    private IEnumerator IdleSoundBehaviour()
    {
        while (true)
        {
            int delay = Random.Range(15, 50);
            yield return new WaitForSeconds(delay);
            PlayRandomIdle();
        }
    }

    #endregion
}
