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 Destinations : ContentPage
    {
        public Destinations()
        {
            InitializeComponent();
            BindingContext = new DestinationViewModel();
            GetDestinations();
        }
        public async void GetDestinations()
        {
            string RefCountryId = App._RefCountryId;

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

            string returnArray = "";
            if(RefCountryId!= null && RefCountryId != "")
            {
                returnArray = (await client.GetAsync(Functions._DataAPI("AllDestinationsByCountryID"))).Content.ReadAsStringAsync().Result;
                App._RefCountryId = "";
            }
            else
            {
                returnArray = (await client.GetAsync(Functions._DataAPI("AllDestinations"))).Content.ReadAsStringAsync().Result;
            }

            //string returnArray = response.;
            DestinationModel resultParse = new DestinationModel();
            if (returnArray != "")
            {
                resultParse = JsonConvert.DeserializeObject<DestinationModel>(returnArray);
            }

            ListViewDestinations.ItemsSource = resultParse.Records;
        }
        async void DestinationButtonClicked(object sender, EventArgs args)
        {
            /*Button button = (Button)sender;
            await Navigation.PushAsync(new Restaurants());*/

            Button button = (Button)sender;
            App._RefDestinationId = button.CommandParameter.ToString();
            await Navigation.PushAsync(new Types());
        }
    }
}