using System;
using System.Collections.Generic;
using System.Linq;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Discounts;
using Nop.Core.Domain.TopicSideMenu;
using Nop.Services.Catalog;
using Nop.Services.Discounts;
using Nop.Services.Localization;
using Nop.Services.Seo;
using Nop.Services.TopicSideMenuService;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Areas.Admin.Models.TopicSideMenu;
using Nop.Web.Framework.Extensions;
using Nop.Web.Framework.Factories;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories
{
    /// <summary>
    /// Represents the category model factory implementation
    /// </summary>
    public partial class TopicSideMenuB2CModelFactory : ITopicSideMenuB2CModelFactory
    {
        #region Fields

        private readonly CatalogSettings _catalogSettings;
        private readonly IAclSupportedModelFactory _aclSupportedModelFactory;
        private readonly IBaseAdminModelFactory _baseAdminModelFactory;
        private readonly ICategoryService _categoryService;
        private readonly IDiscountService _discountService;
        private readonly IDiscountSupportedModelFactory _discountSupportedModelFactory;
        private readonly ILocalizationService _localizationService;
        private readonly ILocalizedModelFactory _localizedModelFactory;
        private readonly IProductService _productService;
        private readonly IStoreMappingSupportedModelFactory _storeMappingSupportedModelFactory;
        private readonly IUrlRecordService _urlRecordService;

        private readonly ITopicSideMenuB2CService _TopicSideMenuB2CService;

        #endregion

        #region Ctor

        public TopicSideMenuB2CModelFactory(CatalogSettings catalogSettings,
            IAclSupportedModelFactory aclSupportedModelFactory,
            IBaseAdminModelFactory baseAdminModelFactory,
            ICategoryService categoryService,
            IDiscountService discountService,
            IDiscountSupportedModelFactory discountSupportedModelFactory,
            ILocalizationService localizationService,
            ILocalizedModelFactory localizedModelFactory,
            IProductService productService,
            IStoreMappingSupportedModelFactory storeMappingSupportedModelFactory,
            IUrlRecordService urlRecordService,
            ITopicSideMenuB2CService TopicSideMenuB2CService)
        {
            _catalogSettings = catalogSettings;
            _aclSupportedModelFactory = aclSupportedModelFactory;
            _baseAdminModelFactory = baseAdminModelFactory;
            _categoryService = categoryService;
            _discountService = discountService;
            _discountSupportedModelFactory = discountSupportedModelFactory;
            _localizationService = localizationService;
            _localizedModelFactory = localizedModelFactory;
            _productService = productService;
            _storeMappingSupportedModelFactory = storeMappingSupportedModelFactory;
            _urlRecordService = urlRecordService;
            _TopicSideMenuB2CService = TopicSideMenuB2CService;
        }

        #endregion


        #region Methods

        /// <summary>
        /// Prepare category search model
        /// </summary>
        /// <param name="searchModel">Category search model</param>
        /// <returns>Category search model</returns>
        public virtual TopicSideMenuSearchModel PrepareTopicSideMenuSearchModel(TopicSideMenuSearchModel searchModel)
        {
            if (searchModel == null)
                throw new ArgumentNullException(nameof(searchModel));

            //prepare page parameters
            searchModel.SetGridPageSize();

            return searchModel;
        }

        /// <summary>
        /// Prepare paged category list model
        /// </summary>
        /// <param name="searchModel">Category search model</param>
        /// <returns>Category list model</returns>
        public virtual TopicSideMenuListModel PrepareTopicSideMenuListModel(TopicSideMenuSearchModel searchModel)
        {
            if (searchModel == null)
                throw new ArgumentNullException(nameof(searchModel));

            //get categories
            var categories = _TopicSideMenuB2CService.GetAllTopicSideMenu(categoryName: searchModel.SearchName,
                showHidden: true,
                pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);

            //prepare grid model
            var model = new TopicSideMenuListModel().PrepareToGrid(searchModel, categories, () =>
            {
                return categories.Select(category =>
                {
                    //fill in model values from the entity
                    var categoryModel = category.ToModel<TopicSideMenuModel>();

                    //fill in additional values (not existing in the entity)

                    return categoryModel;
                });
            });

            return model;
        }

        /// <summary>
        /// Prepare category model
        /// </summary>
        /// <param name="model">Category model</param>
        /// <param name="category">Category</param>
        /// <param name="excludeProperties">Whether to exclude populating of some properties of model</param>
        /// <returns>Category model</returns>
        public virtual TopicSideMenuModel PrepareTopicSideMenuModel(TopicSideMenuModel model, TopicSideMenu TopicSideMenu, bool excludeProperties = false)
        {
            if (TopicSideMenu != null)
            {
                //fill in model values from the entity
                if (model == null)
                {
                    model = TopicSideMenu.ToModel<TopicSideMenuModel>();
                }
                //prepare nested search model
            }

            //set default values for the new model
            if (TopicSideMenu == null)
            {
                model.PageSize = _catalogSettings.DefaultCategoryPageSize;
                model.PageSizeOptions = _catalogSettings.DefaultCategoryPageSizeOptions;
                model.Published = true;
            }
            return model;
        }

        ///// <summary>
        ///// Prepare paged category product list model
        ///// </summary>
        ///// <param name="searchModel">Category product search model</param>
        ///// <param name="category">Category</param>
        ///// <returns>Category product list model</returns>
        //public virtual CategoryProductListModel PrepareCategoryProductListModel(CategoryProductSearchModel searchModel, Category category)
        //{
        //    if (searchModel == null)
        //        throw new ArgumentNullException(nameof(searchModel));

        //    if (category == null)
        //        throw new ArgumentNullException(nameof(category));

        //    //get product categories
        //    var productCategories = _categoryService.GetProductCategoriesByCategoryId(category.Id,
        //        showHidden: true,
        //        pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);

        //    //prepare grid model
        //    var model = new CategoryProductListModel().PrepareToGrid(searchModel, productCategories, () =>
        //    {
        //        return productCategories.Select(productCategory =>
        //        {
        //            //fill in model values from the entity
        //            var categoryProductModel = productCategory.ToModel<CategoryProductModel>();

        //            //fill in additional values (not existing in the entity)
        //            categoryProductModel.ProductName = _productService.GetProductById(productCategory.ProductId)?.Name;

        //            return categoryProductModel;
        //        });
        //    });

        //    return model;
        //}

        ///// <summary>
        ///// Prepare product search model to add to the category
        ///// </summary>
        ///// <param name="searchModel">Product search model to add to the category</param>
        ///// <returns>Product search model to add to the category</returns>
        //public virtual AddProductToCategorySearchModel PrepareAddProductToCategorySearchModel(AddProductToCategorySearchModel searchModel)
        //{
        //    if (searchModel == null)
        //        throw new ArgumentNullException(nameof(searchModel));

        //    //prepare available categories
        //    _baseAdminModelFactory.PrepareCategories(searchModel.AvailableCategories);

        //    //prepare available manufacturers
        //    _baseAdminModelFactory.PrepareManufacturers(searchModel.AvailableManufacturers);

        //    //prepare available stores
        //    _baseAdminModelFactory.PrepareStores(searchModel.AvailableStores);

        //    //prepare available vendors
        //    _baseAdminModelFactory.PrepareVendors(searchModel.AvailableVendors);

        //    //prepare available product types
        //    _baseAdminModelFactory.PrepareProductTypes(searchModel.AvailableProductTypes);

        //    //prepare page parameters
        //    searchModel.SetPopupGridPageSize();

        //    return searchModel;
        //}

        ///// <summary>
        ///// Prepare paged product list model to add to the category
        ///// </summary>
        ///// <param name="searchModel">Product search model to add to the category</param>
        ///// <returns>Product list model to add to the category</returns>
        //public virtual AddProductToCategoryListModel PrepareAddProductToCategoryListModel(AddProductToCategorySearchModel searchModel)
        //{
        //    if (searchModel == null)
        //        throw new ArgumentNullException(nameof(searchModel));

        //    //get products
        //    var products = _productService.SearchProducts(showHidden: true,
        //        categoryIds: new List<int> { searchModel.SearchCategoryId },
        //        manufacturerId: searchModel.SearchManufacturerId,
        //        storeId: searchModel.SearchStoreId,
        //        vendorId: searchModel.SearchVendorId,
        //        productType: searchModel.SearchProductTypeId > 0 ? (ProductType?)searchModel.SearchProductTypeId : null,
        //        keywords: searchModel.SearchProductName,
        //        pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);

        //    //prepare grid model
        //    var model = new AddProductToCategoryListModel().PrepareToGrid(searchModel, products, () =>
        //    {
        //        return products.Select(product =>
        //        {
        //            var productModel = product.ToModel<ProductModel>();
        //            productModel.SeName = _urlRecordService.GetSeName(product, 0, true, false);

        //            return productModel;
        //        });
        //    });

        //    return model;
        //}

        #endregion
    }
}