﻿using System.Net;
using System.Net.Http;
using AnnuaireCertifies.Infrastructure.Services.Depots;
using AnnuaireCertifies.Infrastructure.Services.Encryption;
using AnnuaireCertifies.Web.ApiControllers.Modularity;
using AnnuaireCertifies.WebServices.Web.Models.ModelBuilders;
using AnnuaireCertifies.WebServices.Web.Models.Models.Autocomplete;
using Castle.Core.Logging;

namespace AnnuaireCertifies.WebServices.Web.Controllers
{
    /// <summary>
    /// qsfzef
    /// </summary>
    public class AutocompleteController : ApiModularController
    {
        private readonly IAutocompleteModelBuilder _autocompleteModelBuilder;

        /// <summary>
        /// pppppp
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="encryption"></param>
        /// <param name="depotFactory"></param>
        /// <param name="catalogueModelBuilder"></param>
        public AutocompleteController(
            ILogger logger,
            ICryptography encryption,
            IDepotFactory depotFactory,
            IAutocompleteModelBuilder autocompleteModelBuilder
            )
            : base(logger, encryption, depotFactory)
        {
            _autocompleteModelBuilder = autocompleteModelBuilder;
        }

        // GET autocomplete/{code}/{searchTerm}
        /// <summary>
        /// Renvoie les résultats pour l'autocomplétion
        /// </summary>
        /// <param name="code">Code de l'objet sur lequel porte l'autocomplétion (ADRESSE...)</param>
        /// <param name="searchTerm">Mot-clé sur lequel on réalise l'autocomplétion</param>
        /// <returns>Une liste de mot-clé</returns>
        public HttpResponseMessage Get(string code, string searchTerm)
        {
            try
            {
                var res = _autocompleteModelBuilder.BuildAutocompleteResultatModel(code, searchTerm);
                return Request.CreateResponse<AutocompleteResultModel>(HttpStatusCode.OK, res);
            }
            catch (System.Exception e)
            {
                 _logger.Error(string.Format("{0}/AutocompleteController = > Get", Context.User.ID), e);
                return Request.CreateResponse(HttpStatusCode.InternalServerError);
            }
        }
    }
}