using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Services;
using CafeTerrace.Views.PopUps;
using Prism.Commands;
using Prism.Navigation;
using Prism.Services;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;

namespace CafeTerrace.ViewModels
{
    public class BuyBagSlideMenuViewModel : ViewModelBase, IDisposable
    {
        public ObservableCollection<OrderFood> Foods { get; set; } = new ObservableCollection<OrderFood>();
        public ICommand RemoveFoodButtonCommand { get; set; }
        public ICommand FoodAmountChangedCommand { get; set; }
        private Int64 _totalAmount;
        public Int64 TotalAmount
        {
            get { return _totalAmount; }
            set { SetProperty(ref _totalAmount, value); }
        }

        public BuyBagSlideMenuViewModel(INavigationService dialogService) : base(dialogService)
        {
            App.BuyBagService.FoodChanged += BuyBagService_FoodChanged;
            App.BuyBagService.Foods.ForEach(f => Foods.Add(f));
            TotalAmount = Foods.Sum(f => f.CountAmount);
        }

        public override void InitializeCommand()
        {
            base.InitializeCommand();
            RemoveFoodButtonCommand = new DelegateCommand<OrderFood>((food) =>
            {
                QuestionPopUp questionPopUp = new QuestionPopUp($"ایا از حذف غذا مطمئنید ؟");
                questionPopUp.Accepted += ((ss, ee) =>
                {
                    PopupNavigation.Instance.PopAsync();
                    Foods.Remove(food);
                    App.BuyBagService.RemoveFood(food);

                });
                questionPopUp.Canceled += ((ss, ee) =>
                {
                    PopupNavigation.Instance.PopAsync();
                });
                PopupNavigation.Instance.PushAsync(new PopupPage
                {
                    Content = questionPopUp,
                    CloseWhenBackgroundIsClicked = false
                });
            });
            FoodAmountChangedCommand = new DelegateCommand<OrderFood>((food) =>
            {
                TotalAmount = Foods.Sum(f => f.Food.Price.FinalAmount * f.CountFood);
            });
        }

        private void BuyBagService_FoodChanged(object sender, Services.BagEventArgs e)
        {
            var food = sender as OrderFood;
            if (food != null)
            {
                if (e.BagEventStatus == BagEventStatus.Added)
                {
                    if (Foods.Any(f => f.FoodId == food.FoodId))
                    {
                        //Foods.FirstOrDefault(f => f.FoodId == food.FoodId).CountFood++;
                    }
                    else
                    {
                        Foods.Add(food);
                        TotalAmount = Foods.Sum(f => f.CountAmount);
                    }
                }
                else if (e.BagEventStatus == BagEventStatus.Removed)
                {
                    Foods.Remove(food);
                    TotalAmount = Foods.Sum(f => f.CountAmount);
                }
            }
        }

        public void Dispose()
        {
            App.BuyBagService.FoodChanged -= BuyBagService_FoodChanged;
        }
    }
}
