using System;
using System.Net;
using RestClientAgorum.Model;
using RestClientAgorum.Model.Response;
using RestSharp;

namespace RestClientAgorum.RestClient
{
    partial class AgorumRestHelper
    {
        public static IRestClient CreateRestClient(string serverUrl)
        {
            return new RestSharp.RestClient(serverUrl)
            {
                CookieContainer = new CookieContainer()
            };
        }

        #region Private Implementation

        private static string GetAgorumAbsolutePath(string folderFullPath)
        {
            return string.Format("{0}{1}", AgorumSpecialFolders.RootFolder, folderFullPath);
        }

        private static string GetValidAgorumFileName(string item)
        {
            return AgorumObject.RemoveIllegalChars(item);
        }

        private static void ThrowIfNotSuccessful<T>(IRestResponse<RestResponseBaseExt<T>> response) where T : class
        {
            if (response == null || response.Data == null)
            {
                throw new Exception("No response recieved from Agorum");
            }
            if (!response.Data.Success || response.Data.Data == null)
            {
                throw new RequestFailedException(response);
            }
        }

        private static void ThrowIfNotSuccessful(IRestResponse<RestResponseBaseExt> response, string allowIfContains = null)
        {
            if (response == null || response.Data == null)
            {
                throw new Exception("No response recieved from Agorum");
            }

            if (response.Data.Success) 
                return;

            if (allowIfContains == null || !response.Data.Message.ToLower().Contains(allowIfContains.ToLower()))
            {
                throw new RequestFailedException(response);
            }
        }

        private static void ThrowIfNotSuccessful<T>(IRestResponse<RestResponseCollectionGeneric<T>> response) where T : class
        {
            if (response == null || response.Data == null)
            {
                throw new Exception("No response recieved from Agorum");
            }
            if (!response.Data.Success || response.Data.Data == null)
            {
                throw new RequestFailedException(response);
            }
        }


        private static void ThrowIfNotSuccessful(IRestResponse<LoginResponse> response)
        {
            if (response == null || response.Data == null)
            {
                throw new Exception("No response recieved from Agorum");
            }
            if (!response.Data.Success)
            {
                throw new RequestFailedException(response);
            }
        }
        private static void ThrowIfNotSuccessful(IRestResponse<TransactionResponse> response)
        {
            if (response == null || response.Data == null)
            {
                throw new Exception("No response recieved from Agorum");
            }
            if (!response.Data.Success)
            {
                throw new RequestFailedException(response);
            }
        }

        #endregion
    }
}