using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace esoft_Mobile
{
    public partial class MainPage : ContentPage
    {
        private HttpClient httpClient;

        public MainPage()
        {
            InitializeComponent();
            httpClient = new HttpClient();
            InitializeAsync();
        }

        private async Task InitializeAsync()
        {
            await LoadTasksAsync();
        }

        private async Task LoadTasksAsync()
        {
            try
            {
                var response = await httpClient.GetAsync("http://localhost:55429/api/Tasks/");
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    var tasks = JsonConvert.DeserializeObject<List<Tasks>>(content);
                    LviewTask.ItemsSource = new ObservableCollection<Tasks>(tasks);
                }
                else
                {
                    await DisplayAlert("Ошибка", "Не удалось получить задания", "OK");
                }
            } 
            catch (Exception ex)
            {
                
            }
        }
    }
}