using System;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Discounts;
using Nop.Services.Catalog;
using Nop.Services.Customers;
using Nop.Services.Discounts;
using Nop.Services.ExportImport;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Messages;
using Nop.Services.Media;
using Nop.Services.Security;
using Nop.Services.Seo;
using Nop.Services.Stores;
using Nop.Web.Areas.Admin.Factories;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc;
using Nop.Web.Framework.Mvc.Filters;
using Nop.Web.Areas.Admin.Models.TopicSideMenu;
using Nop.Core.Domain.TopicSideMenu;
using Nop.Services.TopicSideMenuService;

namespace Nop.Web.Areas.Admin.Controllers
{
    public partial class TopicSideMenuB2CController : BaseAdminController
    {
        #region Fields

        private readonly IAclService _aclService;
        private readonly ITopicSideMenuB2CModelFactory _TopicSideMenuFactory;
        private readonly ICategoryService _categoryService;
        private readonly ICustomerActivityService _customerActivityService;
        private readonly ICustomerService _customerService;
        private readonly IDiscountService _discountService;
        private readonly IExportManager _exportManager;
        private readonly IImportManager _importManager;
        private readonly ILocalizationService _localizationService;
        private readonly ILocalizedEntityService _localizedEntityService;
        private readonly INotificationService _notificationService;
        private readonly IPermissionService _permissionService;
        private readonly IPictureService _pictureService;
        private readonly IProductService _productService;
        private readonly IStoreMappingService _storeMappingService;
        private readonly IStoreService _storeService;
        private readonly IUrlRecordService _urlRecordService;
        private readonly IWorkContext _workContext;

        private readonly ITopicSideMenuB2CService _topicSideMenuB2CService;


        #endregion

        #region Ctor

        public TopicSideMenuB2CController(IAclService aclService,
        ITopicSideMenuB2CModelFactory TopicSideMenuFactory,
        ICategoryService categoryService,
            ICustomerActivityService customerActivityService,
            ICustomerService customerService,
            IDiscountService discountService,
            IExportManager exportManager,
            IImportManager importManager,
            ILocalizationService localizationService,
            ILocalizedEntityService localizedEntityService,
            INotificationService notificationService,
            IPermissionService permissionService,
            IPictureService pictureService,
            IProductService productService,
            IStoreMappingService storeMappingService,
            IStoreService storeService,
            IUrlRecordService urlRecordService,
            IWorkContext workContext,
             ITopicSideMenuB2CService topicSideMenuB2CService)
        {
            _aclService = aclService;
            _TopicSideMenuFactory = TopicSideMenuFactory;
            _categoryService = categoryService;
            _customerActivityService = customerActivityService;
            _customerService = customerService;
            _discountService = discountService;
            _exportManager = exportManager;
            _importManager = importManager;
            _localizationService = localizationService;
            _localizedEntityService = localizedEntityService;
            _notificationService = notificationService;
            _permissionService = permissionService;
            _pictureService = pictureService;
            _productService = productService;
            _storeMappingService = storeMappingService;
            _storeService = storeService;
            _urlRecordService = urlRecordService;
            _workContext = workContext;
            _topicSideMenuB2CService = topicSideMenuB2CService;
        }

        #endregion

        #region List

        public virtual IActionResult Index()
        {
            return RedirectToAction("List");
        }

        public virtual IActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedView();

            //prepare model
            var model = _TopicSideMenuFactory.PrepareTopicSideMenuSearchModel(new TopicSideMenuSearchModel());

            return View(model);
        }

        [HttpPost]
        public virtual IActionResult List(TopicSideMenuSearchModel searchModel)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedDataTablesJson();

            //prepare model
            var model = _TopicSideMenuFactory.PrepareTopicSideMenuListModel(searchModel);

            return Json(model);
        }

        #endregion

        #region Create / Edit / Delete

        public virtual IActionResult Create()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedView();

            //prepare model
            var model = _TopicSideMenuFactory.PrepareTopicSideMenuModel(new TopicSideMenuModel(), null);

            return View(model);
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public virtual IActionResult Create(TopicSideMenuModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedView();

            if (ModelState.IsValid)
            {
                var TopicSideMenu = model.ToEntity<TopicSideMenu>();
                TopicSideMenu.CreatedOnUtc = DateTime.UtcNow;
                _topicSideMenuB2CService.InsertTopicSideMenu(TopicSideMenu);


                //activity log
                _customerActivityService.InsertActivity("AddNewTopicSideMenu",
                    string.Format(_localizationService.GetResource("ActivityLog.AddNewTopicSideMenu"), TopicSideMenu.Name), TopicSideMenu);

                _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Catalog.TopicSideMenu.Added"));

                if (!continueEditing)
                    return RedirectToAction("List");

                return RedirectToAction("Edit", new { id = TopicSideMenu.Id });
            }

            //prepare model
            model = _TopicSideMenuFactory.PrepareTopicSideMenuModel(model, null, true);

            //if we got this far, something failed, redisplay form
            return View(model);
        }

        public virtual IActionResult Edit(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedView();

            //try to get a category with the specified id
            var TopicSideMenu = _topicSideMenuB2CService.GetTopicSideMenuById(id);
            if (TopicSideMenu == null || TopicSideMenu.Deleted)
                return RedirectToAction("List");

            //prepare model
            var model = _TopicSideMenuFactory.PrepareTopicSideMenuModel(null, TopicSideMenu);

            return View(model);
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public virtual IActionResult Edit(TopicSideMenuModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedView();

            //try to get a category with the specified id
            var TopicSideMenu = _topicSideMenuB2CService.GetTopicSideMenuById(model.Id);
            if (TopicSideMenu == null || TopicSideMenu.Deleted)
                return RedirectToAction("List");

            if (ModelState.IsValid)
            {

                var TopicSideMenus = model.ToEntity(TopicSideMenu);
                _topicSideMenuB2CService.UpdateTopicSideMenu(TopicSideMenus);


                //activity log
                _customerActivityService.InsertActivity("EdittopicSideMenu",
                    string.Format(_localizationService.GetResource("ActivityLog.EdittopicSideMenu"), TopicSideMenus.Name), TopicSideMenus);

                _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Catalog.topicSideMenu.Updated"));

                if (!continueEditing)
                    return RedirectToAction("List");

                return RedirectToAction("Edit", new { id = TopicSideMenus.Id });
            }

            //prepare model
            model = _TopicSideMenuFactory.PrepareTopicSideMenuModel(model, TopicSideMenu, true);

            //if we got this far, something failed, redisplay form
            return View(model);
        }

        [HttpPost]
        public virtual IActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedView();

            //try to get a category with the specified id
            var topicSideMenu = _topicSideMenuB2CService.GetTopicSideMenuById(id);
            if (topicSideMenu == null)
                return RedirectToAction("List");

            _topicSideMenuB2CService.DeleteTopicSideMenu(topicSideMenu);

            //activity log
            _customerActivityService.InsertActivity("DeletetopicSideMenu",
                string.Format(_localizationService.GetResource("ActivityLog.DeletetopicSideMenu"), topicSideMenu.Name), topicSideMenu);

            _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Catalog.topicSideMenu.Deleted"));

            return RedirectToAction("List");
        }

        #endregion
    }
}