﻿using Castle.Core.Logging;
using AnnuaireCertifies.Infrastructure.Constant;
using AnnuaireCertifies.Infrastructure.Services.Depots;
using AnnuaireCertifies.Infrastructure.Services.Encryption;
using AnnuaireCertifies.BackOffice.Web.Controllers.ActionResults;
using AnnuaireCertifies.BackOffice.Web.Controllers.Filters;
using AnnuaireCertifies.BackOffice.Web.Controllers.Helpers;
using AnnuaireCertifies.BackOffice.Web.Controllers.Modularity;
using AnnuaireCertifies.BackOffice.Web.ViewModels.ModelBuilders.Administration.Edition;
using AnnuaireCertifies.BackOffice.Web.ViewModels.ModelBuilders.Common;
using AnnuaireCertifies.BackOffice.Web.ViewModels.Pages.Administration.Edition.Details;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using AnnuaireCertifies.Infrastructure.Extensions;
using System.Transactions;
using AnnuaireCertifies.Infrastructure.Attributes.Modularity;
using AnnuaireCertifies.Domaine.Criterias;
using AnnuaireCertifies.Infrastructure.Services.Caching;
using AnnuaireCertifies.Infrastructure.Services.Exports;
using AnnuaireCertifies.BackOffice.Web.ViewModels.Pages.Common;
using AnnuaireCertifies.Domaine;

namespace AnnuaireCertifies.BackOffice.Web.Controllers.Administration
{
    public class EditionController : ModularController
    {
        private readonly IEditionModelBuilder _editionModelBuilder;

        public EditionController(
            ILogger logger,
            ICryptography encryption,
            IDepotFactory depotFactory,
            IEditionModelBuilder editionModelBuilder,
            ICommonModelBuilder commonModelBuilder,
            ICaching cachingService,
            IExcelExport exportService)
            : base(logger, encryption, depotFactory, commonModelBuilder, cachingService, exportService)
        {
            _editionModelBuilder = editionModelBuilder;
            Page = PageEnum.Edition;
        }

        public ActionResult Index()
        {
            return ProcessPage();
        }

        [HttpGet]
        public ActionResult Recherche()
        {
            BuildActionContext(VueEnum.EditionSearch);

            var vm = _commonModelBuilder.BuildPartialViewModel(Context, null);

            return View(vm);
        }

        [HttpPost]
        public ActionResult Recherche(SearchEditionCriterias criterias)
        {
            var res = _depotFactory.EditionDepot.Search(criterias.Description);

            var model = new RechercheResultViewModel
            {
                Results = res
            };

            if (criterias.ExportExcel)
            {
                model.ExportUrl = CreateExportFile(res, "ExportRechercheDemande.xls", FileDownloadTypes.ExportRecherche);
            }

            return new JsonHttpStatusResult((int)HttpStatusCode.OK, model);
        }

        public ActionResult Details(long id, long? vueID)
        {
            var vm = _commonModelBuilder.BuildDetailsViewModel(Context, (long)ActionModeAffichageEnum.Consultation, (long)GroupeVueEnum.EditionDetails, null, vueID);

            foreach (var item in vm.GroupeVue.Vues)
            {
                item.Url = RoutesHelper.GetUrl(item.CodeRoute, new { id = id, utilisateurID = Context.User.ID, vueID = item.ID, type = Constants.GetStringValue(GroupeVueTraductibleEnum.Edition) });
            }

            return View("~/Views/Common/Details.cshtml", vm);
        }


        public ActionResult InformationsGenerales(long id)
        {
            BuildActionContext(VueEnum.EditionInformationsGenerales);

            var vm = _editionModelBuilder.BuildInfosGeneralesViewModel(Context, id);

            return View(vm);
        }

        public ActionResult Create()
        {
            var vm = _commonModelBuilder.BuildDetailsViewModel(Context, (long)ActionModeAffichageEnum.Creation, (long)GroupeVueEnum.EditionDetails, 0, null);

            foreach (var item in vm.GroupeVue.Vues)
            {
                item.Url = RoutesHelper.GetUrl(item.CodeRoute, new { id = 0 });
            }

            return View("~/Views/Common/Details.cshtml", vm);
        }

        [HttpPost]
        [ValidateModelState]
        public ActionResult SaveInformationsGenerales(InfosGeneralesViewModel model)
        {
            BuildActionContext(VueEnum.EditionInformationsGenerales, model.Edition.ID, model.Edition.ID > 0 ? (long)TypeActionEnum.Save : (long)TypeActionEnum.Add, model.TimeStamp);

            var edition = Save(() => _depotFactory.EditionDepot.SaveEditionInfosGenerales(AutoMapper.Mapper.Map<EditionViewModel, EditionDto>(model.Edition)));

            return new JsonHttpStatusResult((int)HttpStatusCode.OK, new { id = edition.ID });
        }
    }
}
