using Dapper;
using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Enums;
using Lada.Framework.Data.Repositories;
using Lada.Framework.Infrastructures.Helpers;
using ladaexpress.vn.Infrastructures.DTO;
using ladaexpress.vn.Infrastructures.DTO.Models.Categories;
using ladaexpress.vn.Infrastructures.DTO.Models.Post.Index.Childs;
using ladaexpress.vn.Infrastructures.DTO.Models.Product.Index.Childs;
using Microsoft.EntityFrameworkCore;
using System.Data;
using System.Data.Common;

namespace ladaexpress.vn.Infrastructures.Services
{
    public interface ICategoryService
    {
        Task<CategoryDetailModel> PrepareCategoryDetail(int id);
    }
    public class CategoryService : ICategoryService
    {
        private readonly IRepository<Category> _categoryRepository;
        private readonly IDbConnection _dbConnection;

        public CategoryService(IRepository<Category> categoryRepository, IDbConnection dbConnection)
        {
            _categoryRepository = categoryRepository;
            _dbConnection = dbConnection;
        }

        public async Task<CategoryDetailModel> PrepareCategoryDetail(int id)
        {
            var model = new CategoryDetailModel();
            var currentTime = DateTimeHelper.CurrentTime();

            var category = await _categoryRepository.Query().FirstOrDefaultAsync(c => c.Id == id);

            if (category != null && category.Id > 0)
            {
                model.ContentType = category.ContentType;
                switch (category.ContentType)
                {
                    case ContentType.Post:
                        model = category.ToDetailModel();
                        model.FeaturedPosts = await PreparePosts(id, "CategoryDetail_FeaturedPosts");
                        model.Posts = await PreparePosts(id, "CategoryDetail_Posts");
                        break;
                    case ContentType.Product:
                        model = category.ToDetailModel();
                        model.FeaturedProducts = await PrepareProducts(id, "CategoryDetail_FeaturedProducts");
                        model.Products = await PrepareProducts(id, "CategoryDetail_Products");
                        break;
                    case ContentType.JobPost:
                        break;
                    default:
                        break;
                }


            }

            return model;
        }

        public async Task<List<PostItem>> PreparePosts(int id, string storeName)
        {
            var posts = new List<PostItem>();
            try
            {
                posts = (await _dbConnection.QueryAsync<PostItem>(storeName, new { CatId = id }, commandType: CommandType.StoredProcedure)).ToList();
            }
            catch (Exception)
            {
            }
            return posts;
        }

        public async Task<List<ProductItem>> PrepareProducts(int id, string storeName)
        {
            var posts = new List<ProductItem>();
            try
            {
                posts = (await _dbConnection.QueryAsync<ProductItem>(storeName, new { CatId = id }, commandType: CommandType.StoredProcedure)).ToList();
            }
            catch (Exception)
            {
            }
            return posts;
        }
    }
}
