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

[RequireComponent(typeof(Rigidbody))]
public class Bug : MonoBehaviour, DeathPlaneReciever, IPokeable
{
    public ParticleSystem emitter;
    public SpriteRenderer render;
    public float moveSpeed = 5f;
    public float jitteryTime = 2f;
    public Animator anim;
    public float upForce = 1;
    public string yeetSound;
    public string walkSound;
    public string bugPain;


    private Rigidbody rigid;

    private Coroutine moveRoutine;
    private MassSoundSubscriber sound;

    // Start is called before the first frame update
    void Start()
    {
        rigid = GetComponent<Rigidbody>();
        if (emitter == null)
        {
            emitter = GetComponent<ParticleSystem>();
        }

        if (render == null)
        {
            render = GetComponent<SpriteRenderer>();
        }

        if (anim == null)
        {
            anim = GetComponent<Animator>();
        }

        if(sound == null)
        {
            sound = GetComponent<MassSoundSubscriber>();
        }

        moveRoutine = StartCoroutine(dirTimer(jitteryTime, moveSpeed));
    }


    public void sendMessage(string message)
    {
        if (message == "grabbed")
        {
            rigid.isKinematic = false;
            rigid.AddForce(new Vector3(0, upForce, 0), ForceMode.Impulse);
            if (anim != null)
            {
                anim.SetBool("is_scare", true);
            }

            if (sound != null)
            {
                sound.playSound(this.transform.position, yeetSound);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (anim != null)
        {
            anim.SetFloat("speed", rigid.velocity.magnitude);
        }
    }

    //animation called
    public void playWalkNoise()
    {
        MaybePlaySound(walkSound);
    }

    private void MaybePlaySound(string soundName)
    {
        if (sound != null)
        {
            sound.playSound(this.transform.position, soundName);
        }
    }

    public void DeadByPlane()
    {
        StartCoroutine(deathTimer(2, this.gameObject));
        emitter.Play();
        render.enabled = false;
    }

    public void OnDestroy()
    {
        StopCoroutine(moveRoutine);
    }

    IEnumerator deathTimer(float startTime, GameObject doomed)
    {
        float timer = 0;
        MaybePlaySound(bugPain);
        ScoreKeeper.maybeSetScore(1);

        while (timer < startTime)
        {
            timer += Time.deltaTime;
            yield return 0;
        }

        Destroy(doomed);
    }

    IEnumerator dirTimer(float startTime, float force)
    {

        while (true)
        {
            float timer = 0;

            Vector2 dir = Random.insideUnitCircle * force;

            while (timer < startTime)
            {
                timer += Time.deltaTime;
                rigid.AddForce(new Vector3(dir.x, 0, dir.y), ForceMode.Impulse);
                yield return 0;
            }
        }

    }
}
