using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Versioning;
using System.ServiceModel;
using System.ServiceModel.Web;
using BookingModule.BusinessLogic;
using BookingModule.Entity;
using BookingModule.Exceptions;
using BookingModule.FeatureAccess;
using BookingService.Constants;
using CoreLibrary.Util;
using ThirdPartyBookingFeature.FeatureAccess;

namespace BookingService
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class BookingService : IBookingService
    {
        #region Servive Methods

        /// <summary>
        /// Get Availability based on the request object
        /// </summary>
        /// <param name="request">Request from the user</param>
        /// <returns>Availability of the booking</returns>
        public AvailabilityResponse GetAvailability(AvailabilityRequest request)
        {
            try
            {
                return BookingBusinessLogicManager.DoAvailabilityRequest(request);
            }
            catch (Exception ex)
            {
                throw handleException(ex, Resources.Common.Error_GettingAvailabilityRequest);
            }
        }

        /// <summary>
        /// Get last requests logged on the system
        /// </summary>
        /// <returns>List of performed requests</returns>
        public List<AvailabilityRequest> GetLastRequests()
        {
            try
            {
                return BookingBusinessLogicManager.GetLastAvailabilityRequestList();
            }
            catch (Exception ex)
            {
                throw handleException(ex, Resources.Common.Error_GettingAvailabilityRequestList);
            }
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Static constructor
        /// </summary>
        static BookingService()
        {
            registerTypes();
        }

        /// <summary>
        /// Register the service locator types depending on the service configuration
        /// </summary>
        private static void registerTypes()
        {
            if (Configuration.GetBooleanConfigKey(ConfigurationKeys.USE_MOCK_SERVICE))
            {
                ThirdPartyBookingMockFeature.FeatureAccess.ThirdPartyBookingFeatureAccessManager mockfeatureAccessManager = new ThirdPartyBookingMockFeature.FeatureAccess.ThirdPartyBookingFeatureAccessManager();
                ServiceLocator.Add<IBookingFeatureAccessManager, ThirdPartyBookingMockFeature.FeatureAccess.ThirdPartyBookingFeatureAccessManager>(mockfeatureAccessManager);
            }
            else
            {
                ThirdPartyBookingFeatureAccessManager featureAccessManager = new ThirdPartyBookingFeatureAccessManager();
                ServiceLocator.Add<IBookingFeatureAccessManager, ThirdPartyBookingFeatureAccessManager>(featureAccessManager);

            }
        }

        #endregion

        #region Private methods

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

            return new WebFaultException<string>(message, HttpStatusCode.BadRequest);
        }

        #endregion
    }
}
