using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using CafeTerrace.Models;
using CafeTerrace.Models.Api;
using Prism.Navigation;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Models.SignalR;
using CafeTerrace.Services;
using CafeTerrace.Utilities;
using FFImageLoading.Work;
using Prism.Services;
using RestSharp;
using Rg.Plugins.Popup.Contracts;
using Rg.Plugins.Popup.Services;

namespace CafeTerrace.ViewModels
{
    public class FoodListedPageViewModel : ViewModelBase
    {
        public Category Category { get; set; }
        public ICommand BackButtonCommand { get; set; }
        private Xamarin.Forms.ImageSource _headerSource;
        public Xamarin.Forms.ImageSource HeaderImage
        {
            get { return _headerSource; }
            set { SetProperty(ref _headerSource, value); }
        }

        public ObservableCollection<FoodForCategoryRoot> Foods { get; set; } = new ObservableCollection<FoodForCategoryRoot>();
        public ICommand SelectFood { get; set; }
        public FoodListedPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService) : base(navigationService, pageDialogService)
        {
            App.RealTimeSignal.Start();
            App.RealTimeSignal.Signaled += Signaled;
        }


        public override void Destroy()
        {
            App.RealTimeSignal.Signaled -= Signaled;
            base.Destroy();
        }

        private async void Signaled(object sender, EventArgs e)
        {
            if ((sender as FoodHub).Status == FoodHubStatus.ChangeFood)
            {
                Foods.Clear();
                await InitData();
            }
        }
        public override void InitializeCommand()
        {
            base.InitializeCommand();
            BackButtonCommand = new DelegateCommand(() => { NavigationService.GoBackAsync(); });
            SelectFood = new DelegateCommand<FoodForCategoryRoot>((food) =>
            {
                var parammetrs = new NavigationParameters();
                parammetrs.Add("Food",food);
                NavigationService.NavigateAsync("FoodPage", parammetrs);
            });
        }

        public override async void Initialize(INavigationParameters parameters)
        {
            base.Initialize(parameters);
            try
            {
                Category = parameters.GetValue<Category>("Category");
                
                if (Category != null)
                {
                    
                    HeaderImage = Category.ImageBase64;
                    Title = Category.CategoryName;
                    await InitData();
                    switch (Category.CategoryName)
                    {
                        case "غذای ایرانی همراه کته":
                            PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage("با توجه به پخت کته در فر حداقل زمان انتظار 35 دقیقه می باشد"));
                            break;

                        case "دسر":
                            PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage(
                                "با توجه به اماده سازی پنکیک ، کرپ ، وافل در لحظه سفارش شما ، لذا خداقل زنان انتظار 20 دقیقه می باشد"));
                            break;

                        case "دسر و عصرانه":
                            PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage(
                                "با توجه به اماده سازی پنکیک ، کرپ ، وافل در لحظه سفارش شما ، لذا حداقل زمان انتظار 20 دقیقه می باشد"));
                            break;

                        case "نوشیدنی های قوطی":
                            PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage(
                                "در صورتی که تمایل دارید نوشیدنی شما همراه غذای اصلی و یا پیش غذا سرو شود از منو سرو گزینه مورد نظر خود را انتخاب نمایید"));
                            break;
                    }
                }
                else
                {
                    Category = new Category();
                }
            }
            catch (Exception e)
            {
                if(Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopupStack.Count>0)
                    await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                if (e is RestServiceExeption)
                {
                    if ((e as RestServiceExeption).Status == RestStatus.UnAuthorized)
                        NavigationService.NavigateAsync("LoginPage");
                }
                PageDialogService.DisplayAlertAsync("خطا", e.Message, "بستن");
            }
        }

        private async Task InitData()
        {
            Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(PopUpUtilites.IndicatorPage);
            RestService<List<FoodForCategoryRoot>> restService = new RestService<List<FoodForCategoryRoot>>(Address.BaseAddress);
            var rest = await restService.RequestParameters($"{Address.GetFoodCategory}/{Category.CategoryId}", Method.GET, new Dictionary<string, string>(), true);
            await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
            if (rest.IsSuccess)
            {
                rest.Data.ForEach(f => Foods.Add(f));
            }
        }
    }
}
