﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notakey.SDK
{
    public class UserNotFoundException : Exception
    {
        public UserNotFoundException(string message, Exception inner)
            : base(message, inner)
        {

        }
    }

    public class ApiNotBoundException : Exception
    {
        public ApiNotBoundException(string message)
            : base(message)
        {

        }
    }

    public class ServerErrorException : Exception
    {
        public ServerErrorException(string message, Exception inner)
            : base(message, inner)
        {

        }
    }

    public class InvalidApiEndpoint : Exception
    {
        public InvalidApiEndpoint(string message)
            : base(message)
        {

        }
    }

    public class InvalidApiBoundState : Exception
    {
        public InvalidApiBoundState(string message)
            : base(message)
        {

        }
    }

    public class KeyExpiredException: Exception
    {
        public KeyExpiredException()
            : base("")
        {

        }
    }

    public static class ApiObservableExtensions
    {
        public static IObservable<T> WrapApiException<T, E>(this IObservable<T> o, int statusCode, string message) where E : Exception
        {
            return o
                .Catch<T, Refit.ApiException>(apiException =>
                {
                    if ((int)apiException.StatusCode == statusCode)
                    {
                        return Observable.Throw<T>((E)Activator.CreateInstance(typeof(E), message, apiException));
                    }
                    else
                    {
                        // Do default
                        return Observable.Throw<T>(apiException);
                    }
                });
        }
    }
}
