using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Service;
using Hncore.Pass.Vpn.Domain;
using Hncore.Pass.Vpn.Response.Product;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hncore.Pass.Vpn.Service
{
    public partial class ProductService : ServiceBase<ProductEntity>, IFindService
    {
        private static readonly string m_ProductCacheKey = "Product:{0}";
        CourseContext m_DbContext;
        ProductPackageService m_ProductPackageService;
        public ProductService(CourseContext dbContext, ProductPackageService _ProductPackageService, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
        {
            m_DbContext = dbContext;
            m_ProductPackageService = _ProductPackageService;
        }


        public async override Task<ProductEntity> GetById(object id)
        {
            return await base.GetById(id);
            string key = string.Format(m_ProductCacheKey, id);

            var cacheData = await RedisHelper.GetAsync<ProductEntity>(key);
            if (cacheData == null)
            {
                cacheData = await base.GetById(id);
                if (cacheData != null)
                    await RedisHelper.SetAsync(key, cacheData, 60 * 30);
            }
           
            return cacheData;
        }


        public async override Task<bool> Update(ProductEntity entity, bool autoSave = true)
        {
            string key = string.Format(m_ProductCacheKey, entity.Id);
            await RedisHelper.SetAsync(key, entity, 60 * 30);           
            return await base.Update(entity, autoSave);
        }

        public async Task<List<ProductWithPackageResponse>> ProductWithPackage(int online=1)
        {
            var products = await this.Query(m => m.Sort != 1000).OrderBy(m=>m.Sort).Where(m=>m.OnLine==online).ToListAsync();
            var packages = await m_ProductPackageService.Query(true).ToListAsync();

            List<ProductWithPackageResponse> respList = new List<ProductWithPackageResponse>();
            products.ForEach(p =>
            {
                var resp = new ProductWithPackageResponse();
                resp.Product = p.MapTo<ProductResponse>();
                resp.Packages = packages.Where(m => m.ProductId == p.Id).ToList();
                respList.Add(resp);
            });
            return respList;
        }

        public async Task<ProductWithPackageResponse> GetOneProductWithPackage(int productId)
        {
            var product = await this.GetById(productId);
            var packages = await m_ProductPackageService.Query(m => m.ProductId == productId).ToListAsync();

            List<ProductWithPackageResponse> respList = new List<ProductWithPackageResponse>();
            var resp = new ProductWithPackageResponse();
            resp.Product = product.MapTo<ProductResponse>();
            resp.Packages = packages;
            return resp;
        }

    }
}
