using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CafeTerrace.Models.DataBase;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Xaml;

namespace CafeTerrace.Views.Controller
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class BuyBagFoodItem : ContentView
    {
        private OrderFood _food;
        public OrderFood Food
        {
            get { return _food; }
            set { _food = value; OnPropertyChanged(nameof(Food)); }
        }
        

        public event EventHandler FoodRemoved;
        public event EventHandler FoodAmountChanged;
        public BuyBagFoodItem()
        {
            InitializeComponent();
        }

        protected override void OnPropertyChanged(string propertyName = null)
        {
            base.OnPropertyChanged(propertyName);
            if (propertyName == nameof(BindingContext))
            {
                Food = BindingContext is OrderFood ? BindingContext as OrderFood : null;
                totalAmountLabel.Text = (Food.CountFood * Food.Food.Price.FinalAmount).ToString("N0");
                countLabel.Text = Food.CountFood.ToString();
            }
        }

        private void FoodRemoveButton_OnClicked(object sender, EventArgs e)
        {
            FoodRemoved?.Invoke(_food,new EventArgs());
        }

        private void PlusButton_OnClicked(object sender, EventArgs e)
        {
            Food.CountFood++;
            countLabel.Text = string.Empty;
            countLabel.Text = Food.CountFood.ToString();
            totalAmountLabel.Text = (Food.CountFood * Food.Food.Price.FinalAmount).ToString("N0");
            FoodAmountChanged?.Invoke(Food,new EventArgs());
        }

        private void MinusButton_OnClicked(object sender, EventArgs e)
        {
            if (Food.CountFood > 1)
                Food.CountFood--;
            countLabel.Text = string.Empty;
            countLabel.Text = Food.CountFood.ToString();
            totalAmountLabel.Text = (Food.CountFood * Food.Food.Price.FinalAmount).ToString("N0");
            FoodAmountChanged?.Invoke(Food, new EventArgs());
        }
    }
}