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

public class BenMinigameManager : MonoBehaviour
{
    public static BenMinigameManager Instance;
    [Header("Dishing Minigame")]
    [SerializeField] private DishingMinigame dishingMinigame;
    [SerializeField] private GameObject 
        dishingUI,
        dishingParent,
        dishCheckMark,
        dishes,
        dishesTrigger;

    [Header("Vacuum Minigame")]
    [SerializeField] private VacuumMiniGame vacuumMiniGame;
    [SerializeField] GameObject
        vacuumUI,
        vacuumParent,
        vacuumPoints,
        vacuumHolding,
        vacuum,
        vacuumCheckMark;
    int dustLeft;

    [Header("Pick Up Trash Minigame")]
    [SerializeField] GameObject
        trashCheckMark,
        trashParent,
        bensTrashBag,
        stationaryTrashBag;
    Interactible[] trash;
    int trashPickedUp;

    [Header("Setup")]
    [SerializeField] private BenControls ben;
    [SerializeField] private MiniGameDone miniGameDone;
    [SerializeField] GameObject winText;
    int compleatedEvents;

    private void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
        }
        else if(Instance != this)
        {
            Destroy(gameObject);
        }
    }

    // Use this for initialization
    void Start ()
    {
        dishingMinigame.DishesDoneEvent.AddListener(DeactivateDishingMinigame);
        vacuumMiniGame.VacuumDoneEvent.AddListener(DeactivateVacuumMiniGame);
	}
	

    public void ActivateDishingMinigame()
    {
        dishingUI.SetActive(true);
        dishingParent.SetActive(true);
    }

    private void DeactivateDishingMinigame()
    {
        dishingUI.SetActive(false);
        dishingParent.SetActive(false);
        miniGameDone.gameObject.SetActive(true);
        ben.SetState(BenControls.State.Normal);
        dishCheckMark.SetActive(true);
        dishes.SetActive(false);
        compleatedEvents++;
        dishesTrigger.SetActive(false);

    }

    public void ActivateVacuumMiniGame()
    {
        vacuumUI.SetActive(true);
        vacuumParent.SetActive(true);
    }

    public void ActivateMainVacuum()
    {
        vacuumPoints.SetActive(true);
        dustLeft = vacuumPoints.GetComponentsInChildren<ParticleSystem>().Length;
    }

    private void DeactivateVacuumMiniGame()
    {
        vacuumUI.SetActive(false);
        vacuumParent.SetActive(false);
        miniGameDone.gameObject.SetActive(true);
        ben.SetState(BenControls.State.Normal);
        dustLeft--;
        if (dustLeft == 0)
        {
            miniGameDone.gameObject.SetActive(true);
            vacuumHolding.SetActive(false);
            vacuum.SetActive(true);
            vacuum.GetComponent<CircleCollider2D>().enabled = false;
            vacuumCheckMark.SetActive(true);
            compleatedEvents++;
        }
    }

    public void PickUpBag()
    {
        trash = trashParent.GetComponentsInChildren<Interactible>();
        if(trash.Length == 0)
        {
            TrashCleanComplete();
        }
        for (int i = 0; i < trash.Length; i++)
        {
            trash[i].HasBag = true;
        }
    }

    public void PickUpTrash()
    {
        trashPickedUp++;
        Score.Instance.UpdateScore(50);
        if (trashPickedUp >= trash.Length)
        {
            TrashCleanComplete();
        }
    }

    private void TrashCleanComplete()
    {
        trashCheckMark.SetActive(true);
        bensTrashBag.SetActive(false);
        stationaryTrashBag.SetActive(true);
        stationaryTrashBag.GetComponent<CircleCollider2D>().enabled = false;
        miniGameDone.gameObject.SetActive(true);
        compleatedEvents++;
    }

    private void Update()
    {
        if(compleatedEvents >= 3)
        {
            winText.SetActive(true);
        }
    }
}
