using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookingModule.DataAccess;
using BookingModule.Entity;
using BookingModule.Exceptions;
using BookingModule.FeatureAccess;
using BookingModule.Validation;
using CoreLibrary.Exceptions;
using CoreLibrary.Util;

namespace BookingModule.BusinessLogic
{
    /// <summary>
    /// Business logic manager to handle Booking realted operations
    /// </summary>
    public static class BookingBusinessLogicManager
    {
        #region Constants

        private const string MAX_REQUEST_LOAD = "MaxRequestsToLoad";        //  App settings key
        private const string LOG_AVAILABILITY_REQUESTS = "LogAvailabilityRequests";        //  App settings key

        #endregion

        #region Public Methods

        /// <summary>
        /// Request availablity from the configured third party system
        /// </summary>
        /// <param name="request">Request to make on the third party system</param>
        /// <returns>Response from the third party system</returns>
        public static AvailabilityResponse DoAvailabilityRequest(AvailabilityRequest request)
        {
            try
            {
                BookingValidationManager.ValidateAvailabilityRequest(request);
                if (Configuration.GetBooleanConfigKey(BookingBusinessLogicManager.LOG_AVAILABILITY_REQUESTS))
                {
                    BookingModuleDataAccessManager.InsertRequest(request);
                }

                return ServiceLocator.Resolve<IBookingFeatureAccessManager>().GetAvailability(request);
            }
            catch (Exception ex)
            {
                throw handleException(ex, Resources.Error.MakingAvailabilityRequest);
            }
        }

        /// <summary>
        /// Get a list of n(App.config configured) availability requests
        /// </summary>
        /// <returns>list with the las n availability requests</returns>
        public static List<AvailabilityRequest> GetLastAvailabilityRequestList()
        {
            try
            {
                int requestsToLoad = Configuration.GetIntConfigKey(BookingBusinessLogicManager.MAX_REQUEST_LOAD);
                return BookingModuleDataAccessManager.LoadLastAvailabilityRequestList(requestsToLoad);
            }
            catch (Exception ex)
            {
                throw handleException(ex, Resources.Error.MakingAvailabilityRequest);
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Handle the exception
        /// </summary>
        private static Exception handleException(Exception ex, string message)
        {
            ExceptionLogger.Log(ex);
            if (ex is BookingException)
            {
                BookingException parsed = ex as BookingException;
                if (parsed.Fault == BookingFault.Validation)
                {
                    return ex;
                }
            }

            return new BookingException(message, BookingFault.BusinessLogic);
        }

        #endregion

    }
}
