﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Threading.Tasks;
using AdvertiserObjectModel.Interfaces;
using WCFTask.Models;

namespace WCFTask.DataRepository
{
    public static class DataStore
    {
        public static List<IListing> LoadListing()
        {

            var listingTask = Task<List<IListing>>.Factory.StartNew(() => DispalyNames());
            return listingTask.Result;

        }

        private static List<IListing> DispalyNames()
        {
            var listings = new List<IListing>() { new Listing() { Name = "SVR1", Id = 1, Coupons = new List<ICoupon> { new Coupon() { Id = 1, Name = "Coupon1", Value = 10.00M } } } };
            listings.Add(new Listing() { Name = "SVR2", Id = 2, Coupons = new List<ICoupon> { new Coupon() { Id = 1, Name = "Coupon2", Value = 10.00M } } });
            listings.Add(new Listing() { Name = "SVR3-1", Id = 3, Coupons = new List<ICoupon> { new Coupon() { Id = 1, Name = "Coupon3", Value = 10.00M } } });
            listings.Add(new Listing() { Name = "SVR3-2", Id = 3, Coupons = new List<ICoupon> { new Coupon() { Id = 1, Name = "Coupon3", Value = 10.00M } } });
            listings.Add(new Listing() { Name = "SVR3-3", Id = 3, Coupons = new List<ICoupon> { new Coupon() { Id = 1, Name = "Coupon3", Value = 10.00M } } });

            return listings;
        }


    }
    // To prevent inheriting class
    public sealed class SingletonListingStore
    {
        private static readonly Lazy<List<IListing>> listingStore = new Lazy<List<IListing>>(() => DataStore.LoadListing());
        static SingletonListingStore()
        {


        }


        public static List<IListing> GetListingStore
        {
            get
            {
                return listingStore.Value;
            }
        }

    }

    public static class ListingFactory
    {
        public static IListing FindListing(int userid)
        {
            var task = Task.Factory.StartNew<IListing>(() =>
           {
               IListing listing = null;
               var listingStore = SingletonListingStore.GetListingStore;
               try
               {
                   listing = listingStore.SingleOrDefault(l => l.Id == userid);
               }
               catch (Exception ex)
               {
                   throw ex;
               }
               return listing;

           });

            return task.Result;
        }

        public static IListing FindListing(int userId, CancellationToken cancelToken)
        {
            var task = Task.Factory.StartNew<IListing>((cToken) =>
            {
                IListing listing = null;
                var listingStore = SingletonListingStore.GetListingStore;
                try
                {
                    if(cancelToken.IsCancellationRequested)
                    {
                        cancelToken.ThrowIfCancellationRequested();
                    }
                    listing = listingStore.Single(l => l.Id == userId);
                }
                catch (Exception)
                {

                    throw;
                }
                return listing;

            }, cancelToken);

            return task.Result;
        }
    }
}