
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Newtonsoft.Json.Linq;
using System.Collections.Concurrent;

namespace ManagementApp.AServices.ThreadManage
{

    public class ConcurrentDictionaryThread<T>: IConcurrentTasks<T>, IConcurrentTasks
    {

        private readonly ConcurrentDictionary<T, Func<CancellationToken, ValueTask>> listTask;
        public ConcurrentDictionaryThread() { 
            this.listTask = new ConcurrentDictionary<T, Func<CancellationToken, ValueTask>> (); 
        }

        public async ValueTask Add(Func<CancellationToken, ValueTask> value)
        {
            throw new NotImplementedException();
        }
        public async ValueTask<bool> Add(T key, Func<CancellationToken, ValueTask> value)
        {
            return await Task<bool>.Run(() => { 
                return listTask.TryAdd(key, value);
            }) ;
        }

        public async ValueTask<Func<CancellationToken, ValueTask>> Last(CancellationToken cancellationToken)
        {
            return await Task<Func<CancellationToken, ValueTask>>.Run(() => {
                var d = listTask.Last();
                listTask.TryRemove(d);
                return d.Value;
            });
        }

        public async ValueTask<Func<CancellationToken, ValueTask>> First(CancellationToken cancellationToken)
        {
            return await Task<Func<CancellationToken, ValueTask>>.Run(() => {
                var d = listTask.First();
                listTask.TryRemove(d);
                return d.Value;
            });

        }

        public async ValueTask<Func<CancellationToken, ValueTask>> Search(T key, CancellationToken cancellationToken)
        {
            throw new NotImplementedException(); 
        }

        public async ValueTask<Func<CancellationToken, ValueTask>> Update(T key, CancellationToken cancellationToken, Func<CancellationToken, ValueTask> value)
        {
            throw new NotImplementedException();
        }

    }
}
