using UnityEngine;
using UnityEngine.Video;

namespace NBF
{
    public class Init : MonoBehaviour
    {
        [SerializeField] private VideoPlayer videoPlayer;
        [SerializeField] private GameObject startCanvas;
        [SerializeField] private bool playVideo;

        private void CheckOver(VideoPlayer vp)
        {
            Destroy(startCanvas);
            StartGame();
        }

        private void Awake()
        {
            Debug.Log($"assetsPath = {Application.streamingAssetsPath}  persistentDataPath={Application.persistentDataPath}");
            DontDestroyOnLoad(gameObject);
            Game.SetInitCallback(gameObject, InitCallback);
        }

        private void OnDestroy()
        {
            PermanentCommon.Dispose();
        }

        void InitCallback()
        {
            ES3.Init();
            ES3.Save("NBF", 1);
            if (!playVideo)
            {
                videoPlayer.Stop();
            }
            else
            {
                videoPlayer.loopPointReached += CheckOver;
            }

            QualitySettings.vSyncCount = 0;
            if (!playVideo)
            {
                CheckOver(videoPlayer);
            }
        }


        public void StartGame()
        {
            PermanentCommon.Init();
            InputDef.LoadIcon();
            OpenUI();
        }


        private void OpenUI()
        {
            // await Task.Delay(100);
            CommonTopPanel.Show();
            // SettingPanel.Show();
            LoginPanel.Show();
            
            // SettingPanel.Show();
            
            // MessageBox.Show();
            // FishingShopPanel.Show();
            // PreviewPanel.Show();
        }
    }
}