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

namespace MyGame.Scripts
{
    public class TutorialManager : SingletonMonoBehaviour<TutorialManager>
    {
        /*
         * UIMaskをPrefab化
         * 必要な時にInstantiateしてTransform設定
         * Unmask自体にクリック判定？
         * 指定ボタンが押されたらマスクDestroy
         * 次のTutorialへ
         * BrotherConversationはCompleteにコールバック設定できるのでそのタイミングで次のステップ実行
         * ステップを記憶
         */
        [SerializeField] private TutorialObjectMask maskPrefab;
        public int Index => index;
        private int index;
        private List<BrotherScriptData> scriptList;
        private void Start()
        {
            index = 1;
            scriptList = SpreadsheetDataManager.Instance.GetBaseDataList<BrotherScriptData>(Const.TutorialScriptDataSheet);
        }

        public void ShowTutorialConversation(int id, Action onComplete = null)
        {
            Debug.Log($"count:{scriptList.Count}");
            var list = scriptList.Where(data => data.id == id).ToList();
            if (list.Count == 0)
            {
                return;
            }
            index = id;
            LocalCacheManager.Save(BrotherConversation.CallbackTag, onComplete);
            LocalCacheManager.Save(BrotherConversation.ScriptDataTag, list);
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.Conversation);
        }

        public GameObject ShowTutorialMask(Transform parent, RectTransform targetRectTransform)
        {
            var tutorialObjectMask = Instantiate(maskPrefab, parent);
            tutorialObjectMask.SetTarget(targetRectTransform);
            return tutorialObjectMask.gameObject;
        }
    }
}