/*-------------------------------------------------------------------------
 * 功能描述：ResponseRelation
 * 作者：xulisong
 * 创建时间： 2018/12/19 17:25:01
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Com.FirmLib
{
    public class ResponseRelationManager
    {
        public readonly static ResponseRelationManager Current = new ResponseRelationManager();
        /// <summary>
        /// 枷锁对象
        /// </summary>
        private List<ResponseRelation> Data { get; set; }
        public ResponseRelationManager()
        {
            Data = new List<ResponseRelation>();
        }
        private bool IsClearing { get; set; }
        public void ClearNoAlived()
        {
            if (IsClearing)
                return;
            lock (this)
            {
                try
                {
                    IsClearing = true;
                    for (int i = Data.Count - 1; i > -1; i--)
                    {
                        var relation = Data[i];
                        if (relation == null)
                            continue;
                        if (!relation.Key.TryGetTarget(out Task<HttpResponseMessage> reference) || reference == null)
                        {
                            Data.RemoveAt(i);
                        }
                    }
                }
                finally
                {
                     IsClearing = false;
                }
            }     
        }

        public void Add(Task<HttpResponseMessage> tr, RequestInfo requestInfo)
        {
            try
            {
                lock (this)
                {
                    Data.Add(new ResponseRelation(tr, requestInfo));
                }

                if (Data.Count > 5)
                {
                    Task.Run(() => { ClearNoAlived(); });

                }
            }
            catch (Exception)
            {

            }
        }

        public RequestInfo GetValue(Task<HttpResponseMessage> tr)
        {
            try
            {
                RequestInfo result = null;
                for (int i = Data.Count - 1; i > -1; i--)
                {
                    var relation = Data[i];
                    if (!relation.Key.TryGetTarget(out Task<HttpResponseMessage> reference) || reference == null)
                    {
                        Data.RemoveAt(i);
                    }
                    else if (reference == tr)
                    {
                        result = relation.Value;
                    }
                }
                return result;
            }
            catch (Exception)
            {

            }
            return null;
        }
    }

    public class ResponseRelation
    {
       
        public ResponseRelation(Task<HttpResponseMessage> tr, RequestInfo requestInfo)
        {
            Key = new WeakReference<Task<HttpResponseMessage>>(tr);
            Value = requestInfo;
        }
        public WeakReference<Task<HttpResponseMessage>> Key { get;private set; }
        public RequestInfo Value { get;private set; }
    }
}
