using Serilog;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TINK.Model.Device;
using TINK.Repository;
using TINK.Repository.Request;
using TINK.Repository.Response;

namespace TINK.Model.Services.CopriApi
{
    /// <summary> Object which manages calls to copri in a thread safe way inclding cache functionality. </summary>
    public class CopriProviderHttps : ICachedCopriServer
    {
        /// <summary> Object which manages stored copri answers. </summary>
        private ICopriCache CacheServer { get; }

        /// <summary> Communicates whith copri server. </summary>
        private ICopriServer HttpsServer { get; }

        /// <summary> True if connector has access to copri server, false if cached values are used. </summary>
        public bool IsConnected => HttpsServer.IsConnected;

        /// <summary> Gets the session cookie if user is logged in, an empty string otherwise. </summary>
        public string SessionCookie => HttpsServer.SessionCookie;

        /// <summary> Gets the merchant id.</summary>
        public string MerchantId => HttpsServer.MerchantId;

        /// <summary> Constructs copri provider object to connet to https using a cache objet. </summary>
        /// <param name="copriHost"></param>
        /// <param name="merchantId"></param>
        /// <param name="userAgent">Holds the name and version of the TINKApp.</param>
        /// <param name="sessionCookie">Cookie of user if a user is logged in, false otherwise.</param>
        /// <param name="expiresAfter">Timespan which holds value after which cache expires.</param>
        /// <param name="isExpired">Delegate which returns if cache conted is out of date or not.</param>
        public CopriProviderHttps(
            Uri copriHost,
            string merchantId,
            string userAgent,
            string sessionCookie = null,
            TimeSpan? expiresAfter = null,
            ICopriCache cacheServer = null,
            ICopriServer httpsServer = null)
        {
            CacheServer = cacheServer ?? new CopriCallsMonkeyStore(merchantId, sessionCookie, expiresAfter);
            HttpsServer = httpsServer ?? new CopriCallsHttps(copriHost, merchantId, userAgent, sessionCookie);
        }

        /// <summary>Gets bikes available.</summary>
        /// <param name="p_strMerchantId">Id of the merchant.</param>
        /// <param name="sessionCookie">Auto cookie of user if user is logged in.</param>
        /// <returns>Response holding list of bikes.</returns>
        public async Task<Result<BikesAvailableResponse>> GetBikesAvailable(bool fromCache = false)
        {
            Log.ForContext<CopriProviderHttps>().Debug($"Request to get bikes available{(fromCache ? " from cache" : "")}...");
            if (!CacheServer.IsBikesAvailableExpired
                || fromCache)
            {
                // No need to query because previous answer is not yet outdated.
                Log.ForContext<CopriProviderHttps>().Debug($"Returning bikes available from cache.");
                return new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesAvailableAsync());
            }

            try
            {
                Log.ForContext<CopriProviderHttps>().Debug($"Querrying bikes available from copri.");
                return new Result<BikesAvailableResponse>(
                    typeof(CopriCallsHttps),
                    (await HttpsServer.GetBikesAvailableAsync()).GetIsResponseOk("Abfrage der verfügbaren Räder fehlgeschlagen."));

            }
            catch (Exception exception)
            {
                // Return response from cache.
                Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying bikes available. {Exception}.", exception);
                return new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesAvailableAsync(), exception);
            }
        }

        /// <summary> Gets a list of bikes reserved/ booked by acctive user. </summary>
        /// <param name="sessionCookie">Cookie to authenticate user.</param>
        /// <returns>Response holding list of bikes.</returns>
        public async Task<Result<BikesReservedOccupiedResponse>> GetBikesOccupied(bool fromCache = false)
        {
            Log.ForContext<CopriProviderHttps>().Debug($"Request to get bikes occupied{(fromCache ? " from cache" : "")}...");
            if (!CacheServer.IsBikesOccupiedExpired
            || fromCache)
            {
                // No need to query because previous answer is not yet outdated.
                Log.ForContext<CopriProviderHttps>().Debug($"Returning bikes occupied from cache.");
                return new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesOccupiedAsync());
            }

            try
            {
                Log.ForContext<CopriProviderHttps>().Debug($"Querrying bikes occupied from copri.");
                return new Result<BikesReservedOccupiedResponse>(
                    typeof(CopriCallsHttps),
                    (await HttpsServer.GetBikesOccupiedAsync()).GetIsResponseOk("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen."));

            }
            catch (Exception exception)
            {
                // Return response from cache.
                Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying bikes occupied. {Exception}.", exception);
                return new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesOccupiedAsync(), exception);
            }
        }

        /// <summary> Get list of stations. </summary>
        /// <returns>List of files.</returns>
        public async Task<Result<StationsAvailableResponse>> GetStations(bool fromCache = false)
        {
            Log.ForContext<CopriProviderHttps>().Debug($"Request to get stations{(fromCache ? " from cache" : "")}...");
            if (!CacheServer.IsStationsExpired
                || fromCache)
            {
                // No need to query because previous answer is not yet outdated.
                Log.ForContext<CopriProviderHttps>().Debug($"Returning stations from cache.");
                return new Result<StationsAvailableResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetStationsAsync());
            }

            try
            {
                Log.ForContext<CopriProviderHttps>().Debug($"Querrying stations from copri.");

                var stations = await HttpsServer.GetStationsAsync();

                return new Result<StationsAvailableResponse>(
                    typeof(CopriCallsHttps),
                    stations.GetIsResponseOk("Abfrage der Stationen fehlsgeschlagen."));
            }
            catch (Exception exception)
            {
                // Return response from cache.
                Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying stations. {Exception}.", exception);
                return new Result<StationsAvailableResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetStationsAsync(), exception);
            }
        }

        /// <summary>Adds https--response to cache if response is ok. </summary>
        /// <param name="response">Response to add to cache.</param>
        /// <returns></returns>
        public void AddToCache(Result<StationsAvailableResponse> result)
        {
            Log.ForContext<CopriProviderHttps>().Debug($"Request to add stations all response to cache...");
            if (result.Source == typeof(CopriCallsMonkeyStore)
                || result.Exception != null)
            {
                // Do not add responses form cache or invalid responses to cache.
                return;
            }

            Log.ForContext<CopriProviderHttps>().Debug($"Add bikes available response to cache.");
            CacheServer.AddToCache(result.Response);
        }

        /// <summary>Adds https--response to cache if response is ok. </summary>
        /// <param name="response">Response to add to cache.</param>
        /// <returns></returns>
        public void AddToCache(Result<BikesAvailableResponse> result)
        {
            Log.ForContext<CopriProviderHttps>().Debug($"Request to add bikes available response to cache...");
            if (result.Source == typeof(CopriCallsMonkeyStore)
                || result.Exception != null)
            {
                // Do not add responses form cache or invalid responses to cache.
                return;
            }

            Log.ForContext<CopriProviderHttps>().Debug($"Add bikes available response to cache.");
            CacheServer.AddToCache(result.Response);

        }

        /// <summary>Adds https--response to cache if response is ok. </summary>
        /// <param name="response">Response to add to cache.</param>
        /// <returns></returns>
        public void AddToCache(Result<BikesReservedOccupiedResponse> result)
        {
            Log.ForContext<CopriProviderHttps>().Debug($"Request to add bikes occupied response to cache...");
            if (result.Source == typeof(CopriCallsMonkeyStore)
                || result.Exception != null)
            {
                // Do not add responses form cache or invalid responses to cache.
                return;
            }

            Log.ForContext<CopriProviderHttps>().Debug($"Add bikes occupied response to cache.");
            CacheServer.AddToCache(result.Response);
        }

        public async Task<AuthorizationResponse> DoAuthorizationAsync(string p_strMailAddress, string p_strPassword, string p_strDeviceId)
        {
            return await HttpsServer.DoAuthorizationAsync(p_strMailAddress, p_strPassword, p_strDeviceId);
        }

        public async Task<AuthorizationoutResponse> DoAuthoutAsync()
        {
            return await HttpsServer.DoAuthoutAsync();
        }

        public async Task<ReservationBookingResponse> DoReserveAsync(string bikeId, Uri operatorUri)
        {
            return await HttpsServer.DoReserveAsync(bikeId, operatorUri);
        }

        public async Task<ReservationCancelReturnResponse> DoCancelReservationAsync(string bikeId, Uri operatorUri)
        {
            return await HttpsServer.DoCancelReservationAsync(bikeId, operatorUri);
        }

        public async Task<ReservationBookingResponse> CalculateAuthKeysAsync(string bikeId, Uri operatorUri)
        {
            return await HttpsServer.CalculateAuthKeysAsync(bikeId, operatorUri);
        }

        public async Task<ReservationBookingResponse> UpdateLockingStateAsync(
            string bikeId,
            LocationDto location,
            lock_state state,
            double batteryLevel,
            Uri operatorUri)
            => await HttpsServer.UpdateLockingStateAsync(bikeId, location, state, batteryLevel, operatorUri);

        /// <summary> Books a bike. </summary>
        /// <param name="bikeId">Id of the bike to book.</param>
        /// <param name="guid">Used to publish GUID from app to copri. Used for initial setup of bike in copri.</param>
        /// <param name="batteryPercentage">Holds the filling level percentage of the battery.</param>
        /// <returns>Response on booking request.</returns>
        public async Task<ReservationBookingResponse> DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri)
        {
            return await HttpsServer.DoBookAsync(bikeId, guid, batteryPercentage, operatorUri);
        }

        public async Task<ReservationCancelReturnResponse> DoReturn(
            string bikeId, 
            LocationDto location,
            ISmartDevice smartDevice,
            Uri operatorUri)
        {
            return await HttpsServer.DoReturn(bikeId, location, smartDevice, operatorUri);
        }

        /// <summary>
        /// Submits feedback to copri server.
        /// </summary>
        /// <param name="bikeId">Id of the bike to submit feedback for.</param>
        /// <param name="message">General purpose message or error description.</param>
        /// <param name="isBikeBroken">True if bike is broken.</param>
        public async Task<SubmitFeedbackResponse> DoSubmitFeedback(string bikeId, string message, bool isBikeBroken, Uri opertorUri) =>
             await HttpsServer.DoSubmitFeedback(bikeId, message, isBikeBroken, opertorUri);

        /// <summary> Submits mini survey to copri server. </summary>
        /// <param name="answers">Collection of answers.</param>
        public async Task<ResponseBase> DoSubmitMiniSurvey(IDictionary<string, string> answers)
            => await HttpsServer.DoSubmitMiniSurvey(answers);

    }
}