namespace inutralia.Views.Question
{
    using inutralia.ViewModels;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewConsultationView : ContentPage
    {
        //protected NewQuestionViewModel ViewModel => BindingContext as NewQuestionViewModel;

        NewQuestionViewModel vm;

        public NewConsultationView()
        {
            InitializeComponent();

            BindingContext = vm = new NewQuestionViewModel();

            vm.ListMessages.CollectionChanged += (sender, e) =>
            {
                var target = vm.ListMessages[vm.ListMessages.Count - 1];
                MessagesListView.ScrollTo(target, ScrollToPosition.End, true);
            };

            //newQuest.IsEnabled = true;
            //buttonNewQuestion.IsEnabled = false;

            //buttonNewQuestion.Clicked += ButtonNewQuestion_Clicked;
        }

        protected void ItemTapped(object sender, ItemTappedEventArgs e)
        {
            ((ListView)sender).SelectedItem = null;
        }

        //protected override void OnBindingContextChanged()
        //{
        //    base.OnBindingContextChanged();
        //    ViewModel.PropertyChanged += ViewModel_PropertyChanged;
        //}

        //private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{
        //    OnDataRefreshed();
        //}

        //private async void ButtonNewQuestion_Clicked(object sender, EventArgs e)
        //{
        //    if (String.IsNullOrWhiteSpace(newQuest.Text))
        //    {
        //        newQuest.Text = "";
        //        return;
        //    }

        //    await ViewModel.SaveQuestion(newQuest.Text);
        //    listNewConsultation.SelectedItem = null;
        //    //OnDataRefreshed();
        //}

        ///// <summary>
        ///// Método llamado cada vez que una página pasa a ser visible
        ///// </summary>
        //protected override async void OnAppearing()
        //{
        //    base.OnAppearing();
        //    await ViewModel.RefreshData();
        //    OnDataRefreshed();
        //}

        ///// <summary>
        ///// Método llamado al hacer tap en un elemento de la lista. Navega a la página de detalle
        ///// de la notificación seleccionada
        ///// </summary>
        ///// <param name="sender">La ListiView</param>
        ///// <param name="e">Argumentos del evento</param>
        //void ItemTapped(object sender, ItemTappedEventArgs e)
        //{
        //    if (e == null) return; // has been set to null, do not 'process' tapped event

        //    QuestionMessage item = (QuestionMessage)e.Item;
        //    if (ViewModel.NewQuestion.Last() == item && item.FromUser)
        //    {
        //        buttonNewQuestion.IsEnabled = true;
        //        newQuest.IsEnabled = true;
        //        newQuest.Text = item.Text;
        //    }
        //    else
        //    {
        //        ((ListView)sender).SelectedItem = null; // de-select the row
        //    }
        //}

        //private void OnDataRefreshed()
        //{
        //    newQuest.Text = "";
        //    newQuest.IsEnabled = !(ViewModel.IsCloseOrPending);
        //    newQuest.Placeholder = (ViewModel.IsCloseOrPending) ? ((ViewModel.QuestionSpecialist.State == QuestionSpecialist.Estatus.Closed) ? "La pregunta está cerrada" : "Esperando respuesta") : "Realiza una pregunta";
        //    buttonNewQuestion.IsEnabled = !(ViewModel.IsCloseOrPending);
        //}
    }
}