using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace MainMenu
{
    public class LevelManager : MonoBehaviour
    {
        [SerializeField] private LevelData _data;
        [SerializeField] private LevelView LevelImage;
        [SerializeField] private GameObject loadUI;
        [SerializeField] private Transform parantLevelView;
        [SerializeField] private ScrollerPage _scrollerPage;
        private int index = 0;
        private TMP_Text _percentText;
        private bool _isLoadingLevel = false;
        private LevelData.Level _curLevel;
        private AsyncOperation _loadOpertion;

        private void Start()
        {
            loadUI.GetComponent<Image>().DOFade(0f, 0f);
            _curLevel = _data.Levels[0];
            _data.Levels.ForEach(level =>
            {
                var lev = Object.Instantiate(LevelImage, parantLevelView);
                lev.LevelImage.sprite = level.levelSprite;
            });
            
            _scrollerPage.Init();
            _scrollerPage.OnLevelChanged += SelectLevel;
            _percentText = loadUI.GetComponentInChildren<TMP_Text>();
        }


        private void SelectLevel(int curentMenu)
        {
            _curLevel = _data.Levels[curentMenu];
        }
        
        public void LoadLevel()
        {
            loadUI.SetActive(true);
            loadUI.GetComponent<Image>().DOFade(1f, 0.3f).OnComplete(() =>
            {
                _loadOpertion = SceneManager.LoadSceneAsync(_curLevel.sceneName);
                _isLoadingLevel = true;
            }).SetEase(Ease.InQuad);
            Debug.Log(_curLevel.sceneName);
        }

        private void Update()
        {
            if (_isLoadingLevel && !_loadOpertion.isDone)
            {
                float progressValue = Mathf.Clamp01(_loadOpertion.progress / 0.9f);

                _percentText.text = Mathf.Round(progressValue * 100) + " %";
            }
        }
    }
}