using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using CafeTerrace.Models;
using CafeTerrace.Models.SignalR;
using CafeTerrace.Utilities;
using CafeTerrace.ViewModels;
using CafeTerrace.Views.PopUps;
using Microsoft.AspNetCore.SignalR.Client;
using Newtonsoft.Json;
using Plugin.Settings;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;

namespace CafeTerrace.Services
{

    
    public class RealTimeSignal
    {
        public event EventHandler Signaled;
        private readonly HubConnection _connection;
        public RealTimeSignal()
        {
            try
            {
                _connection = new HubConnectionBuilder()
                    .WithUrl(new Uri($"{Address.BaseHost}/CafeHub"), options =>
                    {
                        try
                        {
                            options.AccessTokenProvider = () => Task.FromResult(Utilities.Instance.LoginedUser.Token.Access_Token);
                        }
                        catch (Exception e)
                        {
                            _connection.StopAsync();
                        }
                    })
                    .Build();
                _connection.On<FoodHub>("ClientSignal", (f =>
                {
                    Signaled?.Invoke(f, new EventArgs());
                }));
                _connection.On<NotifyHub>("PushNotify", (p =>
                {
                    if (p.Status == NotifyHubType.AcceptFood)
                    {
                        var json = CrossSettings.Current.GetValueOrDefault("Order", string.Empty);
                        if (!string.IsNullOrEmpty(json))
                        {
                            var viewModel = JsonConvert.DeserializeObject<SuccessBuyDialogBoxViewModel>(json);
                            viewModel.IsShow = true;
                            Plugin.Settings.CrossSettings.Current.AddOrUpdateValue("Order",
                                Newtonsoft.Json.JsonConvert.SerializeObject(viewModel));
                        }
                    }
                    else
                    {
                        var json = CrossSettings.Current.GetValueOrDefault("Order", string.Empty);
                        if (!string.IsNullOrEmpty(json))
                        {
                            CrossSettings.Current.AddOrUpdateValue("Order", string.Empty);
                        }
                    }
                    PopupNavigation.Instance.PushAsync(PopUpUtilites.PushNotify(p.Message));
                }));
            }
            catch (Exception e)
            {

            }
        }

        public async void Start()
        {
            try
            {
                if(_connection.State!=HubConnectionState.Connected)
                    await _connection.StartAsync();
            }
            catch (Exception e)
            {
                await _connection.StartAsync();
            }
        }
    }
}
