using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XamarinStudy.Models;

namespace XamarinStudy.Views
{
	[XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class ToyMartPage : ContentPage
	{
		public ToyMartPage()
		{
			InitializeComponent();
			InitInstance();
		}

		private void InitInstance()
		{
			ToyRepository dd = new ToyRepository();
			//menuListView.ItemsSource = ToyRepository.ToyList;
			menuListView.ItemsSource = ToyRepository.ToyListKindOf;
		}

		private void menuListView_ItemTapped(object sender, ItemTappedEventArgs e)
		{
			Navigation.PushAsync(new ToyDetailPage((Toy)e.Item));
		}

		private void orderToolbarItem_Clicked(object sender, EventArgs e)
		{
			List<Toy> selected = ToyRepository.ToyListKindOf.SelectMany(t => t).Where(t => t.IsChecked).ToList();
			if (selected.Count < 1)
			{
				DisplayAlert("Alert", "Please select the items.", "OK");
				return;
			}

			Navigation.PushAsync(new ToyOrderPage(selected));
		}

		private void menuListView_Refreshing(object sender, EventArgs e)
		{
			List<Toy> selected = ToyRepository.ToyListKindOf.SelectMany(t => t).Where(t => t.IsChecked).ToList();
			foreach (Toy toy in selected)
			{
				toy.IsChecked = false;
			}

			menuListView.EndRefresh();
		}

		private void unselectAllToolbarItem_Clicked(object sender, EventArgs e)
		{
			List<Toy> selected = ToyRepository.ToyListKindOf.SelectMany(t => t).Where(t => t.IsChecked).ToList();
			foreach (Toy toy in selected)
			{
				toy.IsChecked = false;
			}
		}
	}
}