using System;
using NBC;
using UnityEngine;

namespace NBF
{
    public class FishingPlay
    {
        public FishingDatasource Data { get; private set; }

        public FishingPlay(FishingDatasource datasource)
        {
            Data = datasource;
        }

        public FPlayer SelfPlayer;
        
        public void Init()
        {
            // FishingData = fishingData;
            LoadLevel();
        }

        #region Loading UnLoading

        private SequenceTaskCollection _battleLoadingTasks;

        private SequenceTaskCollection _battleUnLoadingTasks;

        protected void ClearLoadingTasks()
        {
            if (_battleLoadingTasks == null) return;
            _battleLoadingTasks.Stop();
            _battleLoadingTasks.Clear();
            _battleLoadingTasks = null;
        }

        protected void ClearUnLoadingTasks()
        {
            if (_battleUnLoadingTasks == null) return;
            _battleUnLoadingTasks.Stop();
            _battleUnLoadingTasks.Clear();
            _battleUnLoadingTasks = null;
        }

        #endregion

        #region Load and Unload

        protected void LoadLevel()
        {
            _battleLoadingTasks = new SequenceTaskCollection();
            _battleLoadingTasks.AddTask(LoadBattleTask());
            _battleLoadingTasks.AddTask(LoadObjects());
            _battleLoadingTasks.OnCompleted(_ => { OnLoadBattleDone(); });
            Loading.Show(_battleLoadingTasks);
            _battleLoadingTasks.Run(DefRunner.Scheduler);
        }

        public void UnLoadLevel(Action callback)
        {
        }

        /// <summary>
        /// 加载场景需要资源
        /// </summary>
        /// <returns></returns>
        private ParallelTaskCollection LoadBattleTask()
        {
            ParallelTaskCollection subTask1 = new();
            subTask1.AddTask(new LoadSceneTask("Map4"));

            return subTask1;
        }

        /// <summary>
        /// 加载实例化场景对象
        /// </summary>
        /// <returns></returns>
        private ParallelTaskCollection LoadObjects()
        {
            ParallelTaskCollection subTask1 = new();
            subTask1.AddTask(new InitPlayersTask(this));
            subTask1.AddTask(new RunFunTask(AddCameraStack));
            subTask1.AddTask(new RunFunTask(FindSceneObjects));
            return subTask1;
        }

        private void FindSceneObjects()
        {
            // WaterObject = GameObject.FindWithTag("Water").transform;
        }

        private void AddCameraStack()
        {
            // MainPlayer.m_Camera.AddStack(StageCamera.main);
        }

        private void RemoveCameraStack()
        {
            // MainPlayer.m_Camera.RemoveStack(StageCamera.main);
        }

        private void OnLoadBattleDone()
        {
            Debug.LogError("加载关卡结束===");
            FishingPanel.Show();


            // BaseCamera.AddStack(StageCamera.main);
            Loading.Hide();
        }

        #endregion

        public void Update()
        {
            
        }
    }
}