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 ProductUserPriceService : ServiceBase<ProductUserPriceEntity>, IFindService
    {
        CourseContext m_DbContext;
        ProductService m_ProductService;
        ProductPackageService m_ProductPackageService;
        public ProductUserPriceService(ProductPackageService m_ProductPackageService
            , ProductService m_ProductService
            ,CourseContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
        {
            m_DbContext = dbContext;
            this.m_ProductPackageService = m_ProductPackageService;
            this.m_ProductService = m_ProductService;
        }

        public async Task<List<ProductWithPackageUserPriceResponse>> GetPackageUserPrice(int userId)
        {
            var products = await m_ProductService.Query( m=>m.OnLine == 1).OrderBy(m=>m.Sort).ToListAsync();

            var packages = m_ProductPackageService.Query(true);
            var userPrice = this.Query(m => m.UserId == userId);
            var ret = from pakcage in packages
                      join uPrice in userPrice on pakcage.Id equals uPrice.PackageId into temp
                      from uPrice in temp.DefaultIfEmpty()
                      select new PackageUserPriceResponse()
                      {
                          Package = pakcage,
                          UserPrice = uPrice ?? new ProductUserPriceEntity() { RefundDayPrice= products.FirstOrDefault(m=>m.Id==pakcage.ProductId).RefundDayPrice, UserPrice=pakcage.Price }
                      };
            var packgesPrice = await ret.ToListAsync();

            List<ProductWithPackageUserPriceResponse> respList = new List<ProductWithPackageUserPriceResponse>();
            products.ForEach(p =>
            {
                var resp = new ProductWithPackageUserPriceResponse();
                resp.Product = p.MapTo<ProductResponse>();
                resp.PackageUserPrices = packgesPrice.Where(m => m.Package.ProductId == p.Id).ToList();
                respList.Add(resp);
            });
            return respList;
        }

        public async Task<List<ProductUserPriceEntity>> GetProductUserPrice(int product,int userId)
        {
           return  this.Query(m => m.ProductId == product && m.UserId == userId && m.Status == 1).ToList();
        }

        public async Task<List<ProductUserPriceEntity>> GetProductUserPrice(int userId)
        {
            return this.Query(m => m.UserId == userId && m.Status == 1).ToList();
        }
        public async Task<ProductUserPriceEntity> GetPackageUserPrice(int packageId, int userId)
        {
            return this.Query(m => m.PackageId == packageId && m.UserId == userId&& m.Status==1).FirstOrDefault();
        }
    }
}
