using NBC;
using UnityEngine;

namespace NBF
{
    public class InitPlayersTask : NTask
    {
        private ParallelTaskCollection _parallelTaskCollection = new ParallelTaskCollection();
        private FishingPlay _fishingPlay;

        public InitPlayersTask(FishingPlay fishingPlay)
        {
            _fishingPlay = fishingPlay;
        }

        protected override void OnStart()
        {
            _parallelTaskCollection.ParallelNum = 2;

            var players = _fishingPlay.Data.Players.Values;

            foreach (var player in players)
            {
                _parallelTaskCollection.AddTask(new InitPlayerTask(_fishingPlay, player));
            }

            _parallelTaskCollection.OnCompleted(LoadDone);
            _parallelTaskCollection.Run(DefRunner.Scheduler);

            // var player = Object.FindFirstObjectByType<FPlayer>();
            // _fishingPlay.MainPlayer = player;
            // Finish();
        }

        private void LoadDone(ITask task)
        {
            Finish();
        }
    }

    public class InitPlayerTask : NTask
    {
        private FishingPlay _fishingPlay;

        private FPlayerData _playerData;

        public InitPlayerTask(FishingPlay fishingPlay, FPlayerData playerData)
        {
            _fishingPlay = fishingPlay;
            _playerData = playerData;
        }

        protected override void OnStart()
        {
            base.OnStart();
            var model = Resources.Load<GameObject>("Prefabs/Player/male");
            var playerObject = Object.Instantiate(model, SceneSettings.Instance.Node);
            var player = playerObject.GetComponent<FPlayer>();
            player.InitData(_playerData);
            Finish();
        }
    }
}