using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CafeTerrace.Models;
using CafeTerrace.Models.Api;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Services;
using CafeTerrace.Utilities;
using Prism.Services;
using RestSharp;
using Rg.Plugins.Popup.Services;

namespace CafeTerrace.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        public ICommand ProfileButtonCommand { get; set; }
        public ICommand FoodItemCommand { get; set; }
        public ICommand CategorieSelect { get; set; }
        public ICommand CategorieNewSelect { get; set; }

        public ICommand BuyBagButtonCommand { get; set; }
        private HomeMobileRoot _homeMobileRoot;
        public HomeMobileRoot HomeMobileRoot
        {
            get { return _homeMobileRoot; }
            set { SetProperty(ref _homeMobileRoot, value); }
        }

        public ObservableCollection<string> Slider { get; set; } = new ObservableCollection<string>();
        public ObservableCollection<Category> Categories { get; set; } = new ObservableCollection<Category>();
        public ICommand ChefSuggestSliderCommand { get; set; }

        public MainPageViewModel(INavigationService navigationService,IPageDialogService dialogService)
            : base(navigationService,dialogService)
        {
            Title = "Main Page";
            Slider.Add("https://image.freepik.com/free-psd/catering-food-background-with-copy-space_23-2148315227.jpg");
            Slider.Add("https://image.freepik.com/free-vector/fast-food-black-background-poster_1284-14589.jpg");
            Slider.Add("https://img.freepik.com/free-vector/pizza-menu-chalkboard-background_1284-11715.jpg");
            
        }

        public override void InitializeCommand()
        {
            base.InitializeCommand();
            ProfileButtonCommand = new DelegateCommand(() =>
            {
                NavigationService.NavigateAsync("ProfilePage");
            });
            FoodItemCommand = new DelegateCommand(() =>
            {
                NavigationService.NavigateAsync("FoodPage");
            });
            CategorieSelect = new DelegateCommand<Category>((category) =>
            {
                if (category != null)
                {
                    NavigationService.NavigateAsync("FoodListedPage", new NavigationParameters() {{"Category", category}});
                }
            });
            ChefSuggestSliderCommand = new DelegateCommand<Food>((food) =>
            {
                var parammetrs = new NavigationParameters();
                parammetrs.Add("Food", food);
                NavigationService.NavigateAsync("FoodPage", parammetrs);
            });
            CategorieNewSelect = new DelegateCommand<Category>((cat) =>
            {
                NavigationParameters parameters = new NavigationParameters();
                if (cat.IsEnable == true)
                {
                    if (cat.Categories != null && cat.Categories.Count > 0)
                    {
                        parameters.Add("Category", cat);
                        NavigationService.NavigateAsync("CategoryPage", parameters);
                    }
                    else
                    {
                        if (cat != null)
                        {
                            NavigationService.NavigateAsync("FoodListedPage", new NavigationParameters() { { "Category", cat } });
                        }
                    }
                }
            });
            BuyBagButtonCommand = new DelegateCommand(() => { NavigationService.NavigateAsync("PaymentPage"); });
        }

        public override async void Initialize(INavigationParameters parameters)
        {
            base.Initialize(parameters);
            try
            {
                Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(PopUpUtilites.IndicatorPage);
                RestService<HomeMobileRoot> restService = new RestService<HomeMobileRoot>(Address.BaseAddress);
                var rest = await restService.RequestParameters(Address.HomeMobile, Method.GET, new Dictionary<string, string>(),true);
                Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                if (rest.IsSuccess)
                {
                    List<Category> paranet = rest.Data.BaseCategories.Where(c => c.Parent == null).ToList();
                    foreach (var category in paranet)
                    {
                        category.Categories = rest.Data.BaseCategories.Where(c => c.Parent!=null && c.Parent.CategoryId == category.CategoryId).ToList();
                    }

                    rest.Data.BaseCategories = paranet;
                    HomeMobileRoot = rest.Data;
                }
                else
                {
                    PageDialogService.DisplayAlertAsync("خطا", rest.Message, "بستن");
                }
            }
            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, "بستن");
            }
        }
    }
}
