/*
 *Copyright(C) 2023 by Chief All rights reserved.
 *Unity版本：2023.2.5f1c1 
 *作者:Chief  
 *创建日期: 2024-02-22 
 *模块说明：Ecs通用模块
 *版本: 1.0
*/

using System;
using Unity.Jobs;

namespace Aster.ECS
{
    /// <summary>
    /// 辅助Dots的NativeJob
    /// </summary>
    public abstract class NativeJobHandler : IDisposable
    {

        public bool IsRunning { get; private set; }

        public bool IsCompleted { get; private set; }

        /// <summary>
        /// 是否在等待Job完成
        /// </summary>
        public bool IsWaitingJob => IsRunning && !IsCompleted;

        public JobHandle CurrentJobHandle { get; private set; }

        public bool RunJob()
        {
            UpdateCheckJob();
            if (IsRunning)
                return false;
            IsRunning = true;
            IsCompleted = false;
            CurrentJobHandle = Execude();
            return true;
        }

        protected abstract JobHandle Execude();

        public void UpdateCheckJob()
        {
            if (!IsRunning)
                return;

            IsCompleted = CurrentJobHandle.IsCompleted;
            if (IsCompleted)
            {
                IsRunning = false;
                OnCompleted();
            }
        }

        protected virtual void OnCompleted() { }

        public virtual void Dispose() { }

    }
}