using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using CafeTerrace.Dependencies;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Utilities;
using CafeTerrace.ViewModels;
using CafeTerrace.Views.Controller;
using CafeTerrace.Views.PopUps;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;

namespace CafeTerrace.Views
{
    public partial class MainPage : ContentPage
    {



        public MainPage()
        {
            InitializeComponent();
            Timer timer = new Timer(2500);
            timer.Start();
            timer.Elapsed += Timer_Elapsed;
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (sliderCarousel.CurrentItem != null)
            {
                if (sliderCarousel.Position == (BindingContext as MainPageViewModel).HomeMobileRoot.BaseCategories.Count)
                    sliderCarousel.Position = 0;
                else
                    sliderCarousel.Position = sliderCarousel.Position + 1;
            }
        }

        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
            if (!SimpleBottomSheet.IsInitiated)
                SimpleBottomSheet.Init(height, width);
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            sliderMenuFrame.Content = new BuyBagSlideMenu();
            (sliderMenuFrame.Content as BuyBagSlideMenu).SubmitBuyBagClicked += BuyBagSlideMenu_OnSubmitBuyBagClicked;
        }

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

        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 CategoryCollection_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var collectionView = sender as CollectionView;
            if (collectionView.SelectedItem != null && collectionView.SelectedItem is Category)
            {

                (BindingContext as MainPageViewModel)?.CategorieSelect.Execute(collectionView.SelectedItem);
                collectionView.SelectedItem = null;
            }
        }

        private void BuyBagSlideMenu_OnSubmitBuyBagClicked(object sender, EventArgs e)
        {
            (BindingContext as MainPageViewModel)?.BuyBagButtonCommand.Execute(null);
        }

        private void ChefSuggestSlider_OnTapped(object sender, EventArgs e)
        {

            (BindingContext as MainPageViewModel)?.ChefSuggestSliderCommand.Execute((sender as Grid).BindingContext);
        }

        private void Categor_OnTapped(object sender, EventArgs e)
        {
            (BindingContext as MainPageViewModel)?.CategorieNewSelect.Execute((sender as Grid).BindingContext);
        }

        private bool quit = false;
        protected override bool OnBackButtonPressed()
        {
            if (quit == true)
            {
                return base.OnBackButtonPressed();
            }
            if (SimpleBottomSheet.IsOpen == true)
            {
                SimpleBottomSheet.Close();
                SimpleBottomSheet.IsOpen = false;
                return true;
            }
            else
            {
                QuestionPopUp questionPopUp = new QuestionPopUp($"می خواهید خارج شوید ؟");
                questionPopUp.Accepted += (async (ss, ee) =>
                {
                    await PopupNavigation.Instance.PopAsync();
                    quit = true; 
                    var closer = DependencyService.Get<ICloseApplication>();
                    closer?.closeApplication();
                });
                questionPopUp.Canceled += ((ss, ee) =>
                {
                    PopupNavigation.Instance.PopAsync();
                });
                PopupNavigation.Instance.PushAsync(new PopupPage
                {
                    Content = questionPopUp,
                    CloseWhenBackgroundIsClicked = false
                });
                return true;
            }
        }
    }
}