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 CafeTerrace.Models.DataBase;
using CafeTerrace.Models.SignalR;
using CafeTerrace.Services;
using CafeTerrace.Utilities;
using Prism.Navigation;
using Prism.Services;
using RestSharp;
using Rg.Plugins.Popup.Services;

namespace CafeTerrace.ViewModels
{
    public class CategoryPageViewModel : ViewModelBase
    {
        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<Category> Categories { get; set; } = new ObservableCollection<Category>();
        public ICommand CategorieSelect { get; set; }
        private Category BaseCategory { get; set; }
        public CategoryPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService) :base(navigationService,pageDialogService)
        {
            App.RealTimeSignal.Start();
            App.RealTimeSignal.Signaled += Signaled;
        }

        private async void Signaled(object sender, EventArgs e)
        {
            if ((sender as FoodHub).Status == FoodHubStatus.ChangeCategory)
            {
                Categories.Clear();
                 InitData();
            }
        }

        public override void Destroy()
        {
            App.RealTimeSignal.Signaled -= Signaled;
            base.Destroy();
        }
        public override void InitializeCommand()
        {
            BackButtonCommand = new DelegateCommand(() => { NavigationService.GoBackAsync(); });
            CategorieSelect = new DelegateCommand<Category>((category) =>
            {
                if (category != null)
                {
                    NavigationService.NavigateAsync("FoodListedPage", new NavigationParameters() { { "Category", category } });
                }
            });
            base.InitializeCommand();
        }

        public override void Initialize(INavigationParameters parameters)
        {
            try
            {
                BaseCategory = parameters.GetValue<Category>("Category");
                BaseCategory.Categories.ForEach(c => Categories.Add(c));
                if (BaseCategory != null)
                {
                    switch (BaseCategory.CategoryName)
                    {
                        case "غذای ایرانی":
                            PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage(
                                "در صورت تمایل برای سرو کباب در ظرف مخصوص ( گهواره کباب ) از قسمت نوع سرو گزینه سرو با گهواره کباب را انتخاب نمایید"));
                            break;

                    }
                }

                HeaderImage = BaseCategory.ImageBase64;
                Title = BaseCategory.CategoryName;
            }
            catch (Exception e)
            {
                
            }
        }

        private async void InitData()
        {
            try
            {
                Categories.Clear();
                Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(PopUpUtilites.IndicatorPage);
                RestService<List<Category>> restService = new RestService<List<Category>>(Address.BaseAddress);
                var rest = await restService.RequestParameters($"{Address.GetAllSybCategories}/{BaseCategory.CategoryId}", Method.GET, new Dictionary<string, string>(), true);
                Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                if (rest.IsSuccess)
                {
                    rest.Data.ForEach(f => Categories.Add(f));
                }
            }
            catch (Exception e)
            {
                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, "بستن");
            }

        }

    }
}
