using System.Collections;
using System.Collections.Generic;
using ToneTuneToolkit.Common;
using UnityEngine;

public class GameManager : SingletonMaster<GameManager>
{
  [SerializeField] private PuzzlePieceManager ppm00;
  [SerializeField] private PuzzlePieceManager ppm01;
  [SerializeField] private PuzzlePieceManager ppm02;

  // ==================================================

  private void Start() => Init();
  private void OnDestroy() => UnInit();

  // ==================================================

  private void Init()
  {
    ppm00.OnPuzzleComplete += WhenPuzzleComplete;

#if UNITY_WEBGL && !UNITY_EDITOR
    JSCommunicator.PuzzleReady();
#endif
  }

  private void UnInit()
  {
    ppm00.OnPuzzleComplete -= WhenPuzzleComplete;
  }

  public void Reset()
  {
    ppm00.Reset();
    ppm01.Reset();
    ppm02.Reset();
  }

  // ==================================================

  [SerializeField] private bool isGaming = false;

  public void StartGame(string message)
  {
    if (isGaming) { return; }
    isGaming = true;

    switch (message)
    {
      default: break;
      case "0": ppm00.Preset(); break;
      case "1": ppm01.Preset(); break;
      case "2": ppm02.Preset(); break;
    }
    SetBlock(false);

#if UNITY_WEBGL && !UNITY_EDITOR
    JSCommunicator.PuzzleDebug(@$"Puzzle index = {message}");
#endif
  }



  private void WhenPuzzleComplete(int flag)
  {
    isGaming = false;
    SetBlock(true);

#if UNITY_WEBGL && !UNITY_EDITOR
    JSCommunicator.PuzzleFinished();
#endif
  }

  // ==================================================

  [SerializeField] private GameObject goBlocker;
  private void SetBlock(bool value) => goBlocker.SetActive(value);

  // ==================================================

  [ContextMenu("Start 00 Game")]
  private void TestStart00Game() => StartGame("0");
}