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

public class PoopSpawner : MonoBehaviour
{
    [SerializeField] int tickSteps;
    [SerializeField] Sprite negativeStatImage;
    [SerializeField] ActionSO foodOverFlow;
    [SerializeField] ActionSO drinkOverFlow;
    [SerializeField] Sprite poop;
    [SerializeField] Sprite corpse;


    private int poopSpawnRate;
    public Sprite NegativeStatImage { get => negativeStatImage; }
    public int TickSteps { get => tickSteps; }

    private void Start()
    {
        poopSpawnRate = TickSteps;
    }

    private void OnEnable()
    {
        EventManager.Instance.onTick += PoopCountdown;
        foodOverFlow.onAction += PoopSpawn;
        drinkOverFlow.onAction += CorpseSpawn;
    }

    private void OnDisable()
    {
        EventManager.Instance.onTick = PoopCountdown;
        foodOverFlow.onAction -= PoopSpawn;
        drinkOverFlow.onAction -= CorpseSpawn;

    }

    private void PoopCountdown()
    {
        poopSpawnRate--;
        if(poopSpawnRate <= 0)
        {
            PoopSpawn();
            poopSpawnRate = TickSteps;
        }
    }

    private void CorpseSpawn()
    {
        GameObject obj = Pooler.Instance.GetPooledObject();

        if (obj != null)
        {
            obj.transform.position = Randomizers.RandomVector(GameManager.Instance.LevelLimits.LeftLimit, GameManager.Instance.LevelLimits.RightLimit, GameManager.Instance.LevelLimits.UpLimit + 1);
            obj.GetComponent<SpriteRenderer>().sprite = corpse;
            obj.GetComponent<Poop>().CurrentTrash = Trash.Corpse;
            obj.SetActive(true);
        }
        else
        {
            EventManager.Instance.Stress(negativeStatImage);
        }
    }

    private void PoopSpawn()
    {
        GameObject obj = Pooler.Instance.GetPooledObject();
        
        if(obj != null)
        {
            obj.transform.position = Randomizers.RandomVector(GameManager.Instance.LevelLimits.LeftLimit, GameManager.Instance.LevelLimits.RightLimit, GameManager.Instance.LevelLimits.UpLimit +1 );
            obj.GetComponent<SpriteRenderer>().sprite = poop;
            obj.GetComponent<Poop>().CurrentTrash = Trash.Poop;
            obj.SetActive(true);
        }
        else
        {
            EventManager.Instance.Stress(negativeStatImage);
        }

    }


}
