namespace inutralia.Views
{
    using inutralia.ViewModels;
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    using Xamarin.Essentials;

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

        public ListDelSuper()
        {
            InitializeComponent();
        }

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

        async void BuyModalButton(object sender, EventArgs e)
        {
            var browser = API.Constants.ApiUrl;

            foreach (var element in ViewModel.ShoppingList)
            {
                browser += element.Id.ToString() + ',';
            }

            browser = browser.TrimEnd(',');

            await Xamarin.Essentials.Launcher.OpenAsync(new Uri(browser)); 
            //Device.OpenUri(new Uri(browser));
        }

        async void CancelModalButton(object sender, EventArgs e)
        {
            await Navigation.PopAsync();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            await ViewModel.ExecuteLoadShoppingListCommand();
        }
    }
}
