using DamageAssesment.Api.Responses.Interfaces;
using DamageAssesment.Api.Responses.Models;
using Newtonsoft.Json;

namespace DamageAssesment.Api.Responses.Services
{
    public class SurveyServiceProvider :ServiceProviderBase, ISurveyServiceProvider
    {
        public SurveyServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Survey"), configuration.GetValue<string>("EndPointSettings:SurveyUrlBase"))
        {
        }

        public async Task<List<Survey>> getSurveysAsync(string language, string token)
        {
            try
            {
                if (!string.IsNullOrEmpty(language))
                    url = url + "/" + language;
                var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
                var surveys = JsonConvert.DeserializeObject<List<Survey>>(responseJsonString);

                if (surveys == null || !surveys.Any())
                    return new List<Survey>();
                else return surveys;
            }
            catch (Exception ex)
            {
                logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveysAsync()");
                return new List<Survey>();
            }
        }

        public async Task<Survey> getSurveyAsync(int surveyId, string token)
        {
            try
            {
                url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:SurveyById"), surveyId);
                var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
                var survey = JsonConvert.DeserializeObject<Survey>(responseJsonString);

                if (survey == null )
                    return null;
                else return survey;
            }
            catch (Exception ex)
            {
                logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveyAsync()");
                return null;
            }
        }
    }
}