using Castle.Core.Logging;
using EspaceClient.FrontOffice.Annuaire.Web.ViewModels.Pages.Home;
using EspaceClient.FrontOffice.Infrastructure.Constant;
using EspaceClient.FrontOffice.Infrastructure.Services.Depots;
using EspaceClient.FrontOffice.Infrastructure.Services.Encryption;
using EspaceClient.FrontOffice.Infrastructure.Services.Translation;
using EspaceClient.FrontOffice.Web.ViewModels;
using EspaceClient.FrontOffice.Web.ViewModels.Pages.Common;
using EspaceClient.FrontOffice.Web.ViewModels.Pages.Module;
using EspaceClient.FrontOffice.Web.ViewModels.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace EspaceClient.FrontOffice.Annuaire.Web.ViewModels.ModelBuilders.Home
{
    public class HomeModelBuilder : IHomeModelBuilder
    {
        private readonly ITranslator _translationService;
        private readonly IDepotFactory _depotFactory;
        private readonly ILogger _logger;
        private readonly ICryptography _cryptography;

        public HomeModelBuilder(ILogger logger, IDepotFactory depotFactory, ITranslator translationService, ICryptography cryptography)
        {
            _logger = logger;
            _depotFactory = depotFactory;
            _translationService = translationService;
            _cryptography = cryptography;
        }

        public IEnumerable<OrganisationBlocBase> BuildRechercheCriteres(Context context)
        {
            List<OrganisationBlocBase> Result = new List<OrganisationBlocBase>()
            {
                new OrganisationBlocManual()
                {
                    Bloc = new BlocViewModel()
                    {
                        Code = ViewNames.RechercheCriteres.ToString(),
                    },
                    ViewPattern = "~/Views/Home/Blocs/{0}.cshtml",

                    ManualBlocName = ViewNames.RechercheCriteres.ToString(),
                    Ordre = 0,
                    ZonePage=Zones.Center
                }
            };

            return Result;
        }


        public RechercheCriteresViewModel BuildRechercheCriteresBloc(Context context)
        {
            var referentiels = _depotFactory.ReferentielDepot.GetAllItems(context.User.LangueID);
            var pays = _depotFactory.PaysDepot.GetCountries(context.User.LangueID);
            var codeEA = _depotFactory.EAFrancaisDepot.GetCodeEA(context.User.LangueID);
            var codeNAF = _depotFactory.CorrespondanceNAF_EADepot.GetAll();

            return new RechercheCriteresViewModel(context, _translationService)
            {
                Referentiels = referentiels,
                Pays = pays,
                CodeEA = codeEA,
                CodeNAFComplete = codeNAF
            }; 
        }

        public IEnumerable<OrganisationBlocBase> BuildRechercheResultats(Context context)
        {
            List<OrganisationBlocBase> Result = new List<OrganisationBlocBase>()
            {
                new OrganisationBlocManual()
                {
                    Bloc = new BlocViewModel()
                    {
                        Code = ViewNames.RechercheResultats.ToString(),
                    },
                    ViewPattern = "~/Views/Home/Blocs/{0}.cshtml",

                    ManualBlocName = ViewNames.RechercheResultats.ToString(),
                    Ordre = 0,
                    ZonePage=Zones.Center
                }
            };

            return Result;
        }

        public EspaceClient.FrontOffice.Annuaire.Web.ViewModels.Pages.Home.RechercheViewModel BuildRechercheResultatsBloc(Context context, RechercheCriteres criteres, bool isConnected)
        {
            var results = _depotFactory.RechercheDepot.GetCertifies(criteres.NumberCertificat,
                                                                    criteres.RaisonSociale, 
                                                                    criteres.Referentiel, 
                                                                    criteres.Pays, 
                                                                    criteres.Region, 
                                                                    criteres.Departement, 
                                                                    criteres.CodeEA, 
                                                                    criteres.CodeNAF, 
                                                                    criteres.Page,
                                                                    criteres.MaxResultsPerPage,
                                                                    isConnected);

            return new EspaceClient.FrontOffice.Annuaire.Web.ViewModels.Pages.Home.RechercheViewModel(context, _translationService)
            {
                Pagination = new PaginationViewModel(
                    criteres.MaxResultsPerPage,
                    results.Any() ? results.First().NombreElements : 0,
                    criteres.Page
                ),
                Results = results,
                CriteresJs = new JavaScriptSerializer().Serialize(criteres),
                Criteres = criteres
            };
        }

        public IEnumerable<OrganisationBlocBase> BuildCertifieDetail(Context context)
        {
            List<OrganisationBlocBase> Result = new List<OrganisationBlocBase>()
            {
                new OrganisationBlocManual()
                {
                    Bloc = new BlocViewModel()
                    {
                        Code = ViewNames.Detail.ToString(),
                    },
                    ViewPattern = "~/Views/Home/Blocs/{0}.cshtml",

                    ManualBlocName = ViewNames.Detail.ToString(),
                    Ordre = 0,
                    ZonePage=Zones.Center
                }
            };

            return Result;
        }


        public CertificatDetailViewModel BuildCertifieDetailBloc(Context context, string lienWeb)
        {
            var detail = _depotFactory.CertificatDepot.GetDetail(lienWeb);

            return new CertificatDetailViewModel(context, _translationService)
            {
                Detail = detail
            };
        }

    }
}
