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

namespace MyGame.Scripts
{
    public class TutorialManager : SingletonMonoBehaviour<TutorialManager>
    {
        [SerializeField] private TutorialObjectMask maskPrefab;
        public int Index => index;
        private int index;
        private List<BrotherScriptData> scriptList;
        private void Start()
        {
            ResetTutorial();
            scriptList = SpreadsheetDataManager.Instance.GetBaseDataList<BrotherScriptData>(Const.TutorialScriptDataSheet);
        }

        public void ResetTutorial()
        {
            index = 1;
        }

        public void ShowTutorialConversation(int id, Action onComplete = null)
        {
            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, bool isFlip = false)
        {
            var tutorialObjectMask = Instantiate(maskPrefab, parent);
            tutorialObjectMask.SetTarget(targetRectTransform);
            tutorialObjectMask.SetTargetArrow(targetRectTransform, isFlip);
            return tutorialObjectMask.gameObject;
        }
    }
}