using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using MegaRobo.Contract;

namespace MegaRobo.RRQuartz
{
    public class JobItem : JobItemBase
    {
        public JobItem()
        {

        }

        public JobItem(JobItem jobItem) : base(jobItem)
        {
            this.ResultAction = jobItem.ResultAction;
            this.ExecuteAction = jobItem.ExecuteAction;
        }

        public JobItem(string name, string title, IConnection targetService, string descr = null, [CallerFilePath] string sourceFilePath = "", [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0)
            : base(name, title, targetService, descr, sourceFilePath, memberName, sourceLineNumber)
        {

        }

        public JobItem(string name, string title, IConnection targetService, TimeSpan timeout, string descr = null, [CallerFilePath] string sourceFilePath = "", [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0)
            : this(name, title, targetService, descr, sourceFilePath, memberName, sourceLineNumber)
        {
            this.Timeout = timeout;
        }

        public Action ExecuteAction { get; set; }
        public Action<object> ResultAction { get; set; }

        public override async Task OnExecuteActionAsync()
        {
            if(this.ExecuteAction == null) return;
            await Task.Delay(0);
            this.ExecCount++;
            this.Status = JobStatus.HasSend;
            this.UsedTime.Restart();
            this.ExecuteAction.Invoke();
        }

        public override async void Restart()
        {
            this.ExecCount = 0;
            await this.OnExecuteActionAsync();
        }
    }

    public class JobItem<TMode> : JobItem where TMode : Enum
    {
        public JobItem(TMode mode, string name, string title, IConnection targetService, TimeSpan timeout, string descr = null) : base(name, title, targetService, timeout, descr)
        {
            // this.Name = $"{mode}_{name}";
            this.Mode = mode;
        }

        public TMode Mode { get; }
    }
}