using System;
using CafeTerrace.Dependencies;
using CafeTerrace.Models.DataBase;
using CafeTerrace.ViewModels;
using CafeTerrace.Views.DialogBox;
using MD.PersianDateTime.Standard;
using Newtonsoft.Json;
using Plugin.Settings;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;

namespace CafeTerrace.Views
{
    public partial class ProfilePage : ContentPage
    {
        public ProfilePage()
        {
            InitializeComponent();
            var json = CrossSettings.Current.GetValueOrDefault("Order", string.Empty);
            if (!string.IsNullOrEmpty(json))
            {
                var SuccessBuyDialogBoxViewModel = JsonConvert.DeserializeObject<SuccessBuyDialogBoxViewModel>(json);
                if (SuccessBuyDialogBoxViewModel.IsEnded == false)
                {
                    if (SuccessBuyDialogBoxViewModel.IsShow)
                    {
                        SuccessBuyDialogBoxViewModel.Start();
                        counterGrid.BindingContext = SuccessBuyDialogBoxViewModel;
                        SuccessBuyDialogBoxViewModel.Ended += SuccessBuyDialogBoxViewModel_Ended;
                    }
                    else
                        counterGrid.IsVisible = false;
                }
                else
                {
                    counterGrid.IsVisible = false;
                }

                
            }
            else
            {
                counterGrid.IsVisible = false;
            }
        }

        private void SuccessBuyDialogBoxViewModel_Ended(object sender, EventArgs e)
        {
            counterGrid.IsVisible = false;
        }

        private void BirthDayEntry_OnFocused(object sender, FocusEventArgs e)
        {
            try
            {
                var datePickerDialog = new PersianDatePicker();
                PopupNavigation.Instance.PushAsync(new PopupPage
                {
                    Content = datePickerDialog,
                    CloseWhenBackgroundIsClicked = false
                });
                datePickerDialog.DateSelected += ((date, ee) =>
                {
                    (BindingContext as ProfilePageViewModel).SignUpUser.BirthDate = (DateTime)date;
                    birthDayEntry.Text = (new PersianDateTime((DateTime)date)).ToLongDateString();
                });
            }
            catch (Exception exception)
            {
                DisplayAlert("خطا", exception.Message, "بستن");
            }
        }

        private void BirthDateTapGestureRecognizer_OnTapped(object sender, EventArgs e)
        {
            if (sender is Frame frame && frame.BindingContext is UserBirthDate BirthDate)
                (BindingContext as ProfilePageViewModel)?.RemoveBirthDate.Execute(BirthDate);
        }

        private void BirthDayEntry_OnFocusChangeRequested(object sender, FocusRequestArgs e)
        {

        }

        private void Job_OnFocused(object sender, FocusEventArgs e)
        {
            (BindingContext as ProfilePageViewModel)?.OpenJobDialogCommand.Execute(null);
        }

        private void GenderPicker_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (sender is Picker picker)
            {
                (BindingContext as ProfilePageViewModel).SignUpUser.Gender = (GenderType)picker.SelectedIndex+1;
            }
        }
    }
}
