﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Castle.Core.Logging;
using AnnuaireCertifies.Infrastructure.Services.Depots;
using AnnuaireCertifies.Infrastructure.Services.Encryption;
using AnnuaireCertifies.BackOffice.Web.Controllers.Modularity;
using AnnuaireCertifies.BackOffice.Web.Controllers.Helpers;
using AnnuaireCertifies.Infrastructure.Constant;
using AnnuaireCertifies.BackOffice.Web.Controllers.Filters;
using System.Net;
using AnnuaireCertifies.BackOffice.Web.Controllers.ActionResults;
using AnnuaireCertifies.Infrastructure.Extensions;
using AnnuaireCertifies.Domaine.Composite;
using AnnuaireCertifies.Domaine.Results;
using AnnuaireCertifies.BackOffice.Web.ViewModels.ModelBuilders.Common;
using System.Text;
using System;
using AnnuaireCertifies.BackOffice.Web.ViewModels.ModelBuilders.Intervenant.Certification;
using AnnuaireCertifies.Infrastructure.Attributes.Modularity;
using AnnuaireCertifies.Infrastructure.Services.Caching;
using AnnuaireCertifies.BackOffice.Web.ViewModels.Pages.Intervenant.Certification.Details;
using AnnuaireCertifies.Infrastructure.Services.Exports;

namespace AnnuaireCertifies.BackOffice.Web.Controllers.Intervenant
{
    public class CertificationController : IntervenantControllerBase
    {
        private readonly ICertificationModelBuilder _certificationModelBuilder;

        public CertificationController(
            ILogger logger,
            ICryptography encryption,
            IDepotFactory depotFactory,
            ICertificationModelBuilder certificationModelBuilder,
            ICommonModelBuilder commonModelBuilder,
            ICaching cachingService,
            IExcelExport exportService)
            : base(logger, encryption, depotFactory, commonModelBuilder, cachingService, exportService)
        {
            _certificationModelBuilder = certificationModelBuilder;
            Page = PageEnum.IntervenantCertification;
        }

        public override ActionResult Index(long id)
        {
            var intervenantRole = _depotFactory.IntervenantRoleDepot.GetById(id);

            if (intervenantRole != null)
            {
                _intervenantID = intervenantRole.IntervenantID;
            }

            return base.Index(id);
        }

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

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

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

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

            var vm = _certificationModelBuilder.BuildInformationsGeneralesViewModel(Context, id);

            return View(vm);
        }


        [HttpPost]
        [ValidateModelState]
        public ActionResult SaveInformationsGenerales(InformationsGeneralesViewModel model)
        {
            BuildActionContext(VueEnum.IntervenantCertificationInformationsGenerales, model.IntervenantRoleID, model.IntervenantRoleID > 0 ? (long)TypeActionEnum.Save : (long)TypeActionEnum.Add, model.TimeStamp);

            var role = AutoMapper.Mapper.Map<InformationsGeneralesViewModel, IntervenantCertificationInfosGeneralesComposite>(model);

            Save(() => { _depotFactory.IntervenantCertificationDepot.SaveInfosGenerales(role); return null; });

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

    }
}
