using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using iElection.Models;
using iElection.Models.Config;
using iElection.Services.Contracts;
using iElection.Services.Rest;
using iElection.Utilities;
using iElection.Views.PopUps;
using Newtonsoft.Json;
using Prism.Navigation;
using Refit;

namespace iElection.Services
{
    public class RestWrapper : IRestWrapper
    {
        private static RefitSettings setting = new RefitSettings(new NewtonsoftJsonContentSerializer(new JsonSerializerSettings
        {
            Formatting = Newtonsoft.Json.Formatting.Indented,
            ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
        }));

        public ICrudApiRest<T, TKey> CrudApiRest<T, TKey>(string address) where T : class
        {
            return RestService.For<ICrudApiRest<T, TKey>>(address, setting);
        }

        public IPageRestApi PageRestApi { get; } = RestService.For<IPageRestApi>(Address.PageController, setting);

        public IAccountApiRest AccountApiRest { get; } = RestService.For<IAccountApiRest>(Address.AccountController, setting);





        public async void UnhandledExceptionRaiser(ApiException apiException , INavigationService NavigationService = null)
        {

            if (apiException.StatusCode == HttpStatusCode.NotFound)
                UtilitiesWrapper.Instance.PopUpUtilities.PushError(AppResource.BadGetway);
            else if (apiException.StatusCode == HttpStatusCode.Unauthorized)
            {
                if (NavigationService == null)
                {
                    UtilitiesWrapper.Instance.PopUpUtilities.PushError("لطفا مجددا وارد حساب کاربری خود شوید");
                }
                else
                {

                    var popup = new QuestionPopUp(AppResource.UnAuthorizeError);
                    UtilitiesWrapper.Instance.PopUpUtilities.PopAsync();
                    await UtilitiesWrapper.Instance.PopUpUtilities.PushAsync(popup);
                    popup.Accepted += ((sender, e) =>
                    {
                        //UtilitiesWrapper.Instance.UserUtilities.Logout();
                        //NavigationService.NavigateAsync("myapp:///NavigationPage/LoginPage");
                    });
                }
            }
            else if (apiException.StatusCode == HttpStatusCode.BadGateway)
                UtilitiesWrapper.Instance.PopUpUtilities.PushError(AppResource.BadGetway);
            else if (apiException.StatusCode == HttpStatusCode.ServiceUnavailable)
                UtilitiesWrapper.Instance.PopUpUtilities.PushError(AppResource.BadGetway);
            else if (apiException.StatusCode == 0)
                UtilitiesWrapper.Instance.PopUpUtilities.PushError(AppResource.NotFoundError);
            else if (apiException.StatusCode == HttpStatusCode.Forbidden)
            {
                UtilitiesWrapper.Instance.PopUpUtilities.PushError("شما به این بخش اجازه دسترسی ندارید");
            }
            else
            {
                var ex = await apiException.GetContentAsAsync<ApiResult>();
                if (ex != null)
                    UtilitiesWrapper.Instance.PopUpUtilities.PushError(ex.Message);
                else
                    UtilitiesWrapper.Instance.PopUpUtilities.PushError(AppResource.NotFoundError);
            }
        }
    }
}
