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

namespace CafeTerrace.ViewModels
{
    public class FoodPageViewModel : ViewModelBase
    {
        public ICommand BackButtonCommand { get; set; }
        public ICommand BuyButtonCommand { get; set; }
        public ICommand BuyBagButtonCommand { get; set; }
        public ICommand AddCommentButtonCommand { get; set; }
        private Food _food;
        public Food Food
        {
            get { return _food; }
            set { SetProperty(ref _food, value); }
        }
        private bool _isTyped;
        public bool IsTyped
        {
            get { return _isTyped; }
            set { SetProperty(ref _isTyped, value); }
        }
        public FoodType SelectedFoodType { get; set; }
        public bool ServiceNow { get; set; } = false;
        public FoodPageViewModel(INavigationService navigationService,IPageDialogService dialogService):base(navigationService,dialogService)
        {

        }

        public override void InitializeCommand()
        {
            base.InitializeCommand();
            BackButtonCommand = new DelegateCommand(() => { NavigationService.GoBackAsync(); });
            BuyButtonCommand = new DelegateCommand(() =>
            {
                var foodCounterDialogBox = new FoodCounterDialogBox();
                foodCounterDialogBox.Submited += ((count, ee) =>
                {
                    PopupNavigation.Instance.PopAsync();
                    if (Food.FoodType == null || Food.FoodType.Count == 0)
                    {
                        App.BuyBagService.AddFood(new OrderFood
                        {
                            Food = Food,
                            FoodId = Food.FoodId,
                            ServiceNow = ServiceNow,
                            CountFood = (int)count
                        });
                    }
                    else if (SelectedFoodType != null)
                    {
                        App.BuyBagService.AddFood(new OrderFood
                        {
                            Food = Food,
                            FoodId = Food.FoodId,
                            ServiceNow = ServiceNow,
                            CountFood = (int)count,
                            FoodType = SelectedFoodType.Type
                            
                        });

                    }
                    else
                    {
                        PopupNavigation.Instance.PushAsync(PopUpUtilites.ErrorPage("لطفا نوع سرو غذا را انتخاب کنید"));
                    }
                });
                PopupNavigation.Instance.PushAsync(new PopupPage
                {
                    CloseWhenBackgroundIsClicked = true,
                    Content = foodCounterDialogBox
                });
            });
            BuyBagButtonCommand = new DelegateCommand(() => { NavigationService.NavigateAsync("PaymentPage"); });
            AddCommentButtonCommand = new DelegateCommand(() =>
                {
                    try
                    {
                        PopupNavigation.Instance.PushAsync(new PopupPage {Content = new AddCommentDialogBox(Food)});
                    }
                    catch (Exception e)
                    {
                        PageDialogService.DisplayAlertAsync("خطا", e.Message, "بستن");
                    }
                });
        }

        public override async void Initialize(INavigationParameters parameters)
        {
            base.Initialize(parameters);
            try
            {
                var bFood = parameters.GetValue<Food>("Food");
                if (bFood != null)
                {
                    Title = bFood.Name;
                    Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(PopUpUtilites.IndicatorPage);
                    RestService<Food> restService = new RestService<Food>(Address.BaseAddress);
                    var rest = await restService.RequestBody($"{Address.GetFoodById}", Method.POST, new { FoodId = bFood.FoodId, UserId = Utilities.Instance.LoginedUser.UserId.ToString() }, true);

                    Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAllAsync();
                    if (rest.IsSuccess)
                    {
                        Food = rest.Data;
                        if (Food.FoodType != null && Food.FoodType.Count > 0)
                            IsTyped = true;
                        else
                            IsTyped = false;
                    }
                    return;
                }
            }
            catch (Exception e)
            {
                try
                {

                    var food = parameters.GetValue<FoodForCategoryRoot>("Food");
                    if (food != null)
                    {
                        Title = food.Name;
                        Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(PopUpUtilites.IndicatorPage);
                        RestService<Food> restService = new RestService<Food>(Address.BaseAddress);
                        var rest = await restService.RequestBody($"{Address.GetFoodById}", Method.POST, new { FoodId = food.FoodId, UserId = Utilities.Instance.LoginedUser.UserId.ToString() }, true);
                        Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAllAsync();
                        if (rest.IsSuccess)
                        {
                            Food = rest.Data;
                            if (Food.FoodType != null && Food.FoodType.Count > 0)
                                IsTyped = true;
                            else
                                IsTyped = false;
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (PopupNavigation.Instance.PopupStack.Count > 0)
                        await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAllAsync();
                    if (e is RestServiceExeption)
                    {
                        if ((e as RestServiceExeption).Status == RestStatus.UnAuthorized)
                            NavigationService.NavigateAsync("LoginPage");
                    }
                    PageDialogService.DisplayAlertAsync("خطا", e.Message, "بستن");
                }
            }
        }
    }
}
