using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Utilities;
using CafeTerrace.Views.Controller;
using CafeTerrace.Views.PopUps;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;

namespace CafeTerrace.Services
{
    public enum BagEventStatus
    {
        Added,
        Removed,
        Changed
    }
    public class BagEventArgs : EventArgs
    {
        public BagEventStatus BagEventStatus { get; }

        public BagEventArgs(BagEventStatus bagEventStatus)
        {
            BagEventStatus = bagEventStatus;
        }
    }
    public class BuyBagService
    {
        public List<OrderFood> Foods { get; set; } = new List<OrderFood>();
        public event EventHandler<BagEventArgs> FoodChanged;

        public void AddFood(OrderFood food)
        {
            if (Foods.Any(f => f.FoodId == food.FoodId))
            {
                var foodAdded = Foods.FirstOrDefault(f => f.FoodId == food.FoodId && f.FoodType == food.FoodType);
                if (foodAdded == null)
                {

                    Foods.Add(food);
                    FoodChanged?.Invoke(food, new BagEventArgs(BagEventStatus.Added));
                    PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage("غذا به سبد خرید شما اضافه شد"));
                }
                else
                {
                    foodAdded.CountFood += food.CountFood;
                    PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage("غذا در سبد خرید شما وجود دارد تعداد فعلی به تعداد قبلی اضافه شد"));
                }
            }

            else
            {
                Foods.Add(food);
                FoodChanged?.Invoke(food, new BagEventArgs(BagEventStatus.Added));
                PopupNavigation.Instance.PushAsync(PopUpUtilites.SeccussPage("غذا به سبد خرید شما اضافه شد"));
            }
        }

        public void RemoveFood(OrderFood food)
        {
            if (Foods.Any(f => f.FoodId == food.FoodId))
            {
                Foods.Remove(food);
                FoodChanged?.Invoke(food, new BagEventArgs(BagEventStatus.Removed));
                PopupNavigation.Instance.PushAsync(PopUpUtilites.InfoPage("غذا از سبد خرید شما حذف شد"));
            }

        }
    }
}
