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

public class CrowdMinigameManager : MonoBehaviour
{
    public static CrowdMinigameManager Instance;


    public enum State { None, Meeting, Bridge, Elevator, Party}
    private State currentState;
    [Header("Settings")]
    [SerializeField] private int characterCount = 3;
    private List<CharacterRandomizer.Character> characters = new List<CharacterRandomizer.Character>();


    [Header("Setup")]
    [SerializeField] private CharacterRandomizer randomizer;


    [Header("Meeting Minigame")]
    [SerializeField] private RunMashManager runMash;


    [Header("Pizza Minigame")]
    [SerializeField] private PizzaMinigameHandler pizzaMinigame;


    [Header("Elevator")]
    [SerializeField] private ElevatorScript elevatorScript;

    [SerializeField] private Party party;

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

    private void Start()
    {
        for (int i = 0; i < characterCount; i++)
        {
            characters.Add(randomizer.GetCharacter());
        }

        runMash.SetPlayerVisuals(characters[0], characters[1], characters[2]);
        pizzaMinigame.SetPlayerVisuals(characters[0], characters[1], characters[2]);
        elevatorScript.SetPlayerVisuals(characters[0], characters[1], characters[2]);
        party.SetPlayerVisuals(characters[0], characters[1], characters[2]);
        runMash.gameObject.SetActive(false);
        pizzaMinigame.gameObject.SetActive(false);
        elevatorScript.gameObject.SetActive(false);
        party.gameObject.SetActive(false);

        // Set the initial state here.
        SetState(State.Meeting);
    }


    public void InitializeRunningMinigame()
    {
        runMash.gameObject.SetActive(true);
    }


    public void InitializePizzaMinigame()
    {
        pizzaMinigame.gameObject.SetActive(true);
    }

    public void SetState(State state)
    {
        if((int)state <= (int)currentState)
        {
            return;
        }
        OnStateExit(currentState);
        OnStateEnter(state);
    }

    private void OnStateExit(State state)
    {
        switch (state)
        {
            case State.None:
                break;
            case State.Meeting:
                runMash.gameObject.SetActive(false);
                break;
            case State.Bridge:
                pizzaMinigame.gameObject.SetActive(false);
                break;
            case State.Elevator:
                elevatorScript.gameObject.SetActive(false);
                break;
            case State.Party:
                party.gameObject.SetActive(true);
                break;
            default:
                break;
        }
    }

    private void OnStateEnter(State state)
    {
        switch (state)
        {
            case State.None:
                break;
            case State.Meeting:
                InitializeRunningMinigame();
                break;
            case State.Bridge:
                InitializePizzaMinigame();
                break;
            case State.Elevator:
                elevatorScript.gameObject.SetActive(true);
                elevatorScript.Initiate();
                break;
            case State.Party:
                party.gameObject.SetActive(true);
                party.Initiate();
                break;
            default:
                break;
        }
    }

}
