﻿using FormsWorkshopSample.Core;
using FormsWorkshopSample.Services;
using FormsWorkshopSample.ViewModels;
using Xamarin.Forms;

namespace FormsWorkshopSample.Views
{
    public partial class MainPage
    {
        private readonly MainViewModel _viewModel;
        public MainPage()
        {
            InitializeComponent();
            _viewModel = new MainViewModel(new HttpConferenceService(new FormsHttpService()));
            BindingContext = _viewModel;
        }
        protected override async void OnAppearing()
        {
            base.OnAppearing();
            await _viewModel.RefreshAsync();
        }

        private void Session_ItemSelected(object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
        {
            var selectedSession = e.SelectedItem as Session;
            if (selectedSession != null)
            {
                // Navigate to details page and provide selected session
                Navigation.PushAsync(new SessionDetailsPage(selectedSession));
            }

            // Unselect item
            (sender as ListView).SelectedItem = null;
        }

        private void Speaker_ItemSelected(object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
        {
            var selectedSpeaker = e.SelectedItem as Speaker;
            if (selectedSpeaker != null)
            {
                // Navigate to details page and provide selected session
                Navigation.PushAsync(new SpeakerDetailsPage(selectedSpeaker));
            }

            // Unselect item
            (sender as ListView).SelectedItem = null;
        }
    }
}
