using System;
using System.Timers;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Utilities;
using CafeTerrace.ViewModels;
using CafeTerrace.Views.Controller;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;
using Xamarin.Forms.Internals;

namespace CafeTerrace.Views
{
    public partial class FoodPage : ContentPage
    {
        public FoodPage()
        {
            InitializeComponent();
            Timer timer = new Timer(2500);
            timer.Start();
            timer.Elapsed += Timer_Elapsed;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            sliderFrame.Content = new BuyBagSlideMenu();
            (sliderFrame.Content as BuyBagSlideMenu).SubmitBuyBagClicked += BuyBagSlideMenu_OnSubmitBuyBagClicked;
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            (sliderFrame.Content as BuyBagSlideMenu).Dispose();
        }

        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
            if (!SimpleBottomSheet.IsInitiated)
                SimpleBottomSheet.Init(height, width);
        }

        private void CloseBuyBagButton_OnClicked(object sender, EventArgs e)
        {

            SimpleBottomSheet.Close();
            SimpleBottomSheet.IsOpen = false;
        }

        private void BuyBagTapGestureRecognizer_OnTapped(object sender, EventArgs e)
        {
            SimpleBottomSheet.Open();
            SimpleBottomSheet.IsOpen = true;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (sliderCarousel.CurrentItem != null)
            {
                if (sliderCarousel.Position == (BindingContext as FoodPageViewModel).Food.FoodPhoto.Count)
                    sliderCarousel.Position = 0;
                else
                    sliderCarousel.Position = sliderCarousel.Position + 1;
            }
        }

        private void BuyBagSlideMenu_OnSubmitBuyBagClicked(object sender, EventArgs e)
        {

            (BindingContext as FoodPageViewModel)?.BuyBagButtonCommand.Execute(null);
        }

        private void FoodTypeButton_OnTapped(object sender, EventArgs e)
        {
            foodTypePicker.Focus();
        }

        private void FoodTypePicker_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            var type = (sender as Picker).SelectedItem as FoodType;
            (BindingContext as FoodPageViewModel).SelectedFoodType = type;
            serveFrame.BackgroundColor = Color.Accent;
            (serveFrame.Content as Grid).Children.ForEach(item =>
            {
                if (item is Label)
                    (item as Label).Text = type.Type;
            });
        }

        private void FastServe_OnTapped(object sender, EventArgs e)
        {
            if((BindingContext as FoodPageViewModel).ServiceNow == false)
            {
                //serveTimeFrame.BackgroundColor = Color.Accent;
                (BindingContext as FoodPageViewModel).ServiceNow = true;
            }
            else
            {
                //serveTimeFrame.BackgroundColor = (Color) Application.Current.Resources["Gray-200"];
                (BindingContext as FoodPageViewModel).ServiceNow = false;
            }
        }

        private void SliderTapGestureRecognizer_OnTapped(object sender, EventArgs e)
        {
            if ((sender as Grid).BindingContext is FoodImage food)
            {
                PopupNavigation.Instance.PushAsync(Utilities.PopUpUtilites.ImagePopUp(food.Image));

            }
        }

        protected override bool OnBackButtonPressed()
        {
            if (SimpleBottomSheet.IsOpen == true)
            {
                SimpleBottomSheet.Close();
                SimpleBottomSheet.IsOpen = false;
                return true;
            }
            else
                return base.OnBackButtonPressed();
        }
    }
}
