using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Net.Http;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using MobileApp.ViewModels;
using MobileApp.Model;

namespace MobileApp.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Restaurants : ContentPage
    {
        public Restaurants()
        {
            InitializeComponent();
            BindingContext = new RestaurantViewModel();
            GetRestaurants();
        }
        public async void GetRestaurants()
        {
            string RefDestinationId = App._RefDestinationId;

            var client = new System.Net.Http.HttpClient();

            string returnArray = "";
            if (RefDestinationId != null && RefDestinationId !="" )
            {
                returnArray = (await client.GetAsync(Functions._DataAPI("AllRestaurantsByDestinationID"))).Content.ReadAsStringAsync().Result;
            }
            else
            {
                returnArray = (await client.GetAsync(Functions._DataAPI("AllRestaurants"))).Content.ReadAsStringAsync().Result;
            }
            RestaurantModel resultParse = new RestaurantModel();
            if (returnArray != "")
            {
                resultParse = JsonConvert.DeserializeObject<RestaurantModel>(returnArray);
            }

            ListViewRestaurants.ItemsSource = resultParse.Records;
        }
        async void RestaurantButtonClicked(object sender, EventArgs args)
        {
            Button button = (Button)sender;
            App._RefRestaurantId = button.CommandParameter.ToString();
            await Navigation.PushAsync(new SingleRestaurant());
        }
    }
}