using FoxTube.Controls;
using System;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace FoxTube.Pages
{
    /// <summary>
    /// Downloads page
    /// </summary>
    public sealed partial class Downloads : Page, NavigationPage
    {
        public object Parameter { get; set; } = null;
        public Downloads()
        {
            InitializeComponent();
            DownloadAgent.Page = this;
            path.Text = DownloadAgent.Downloads.Path;
            Refresh(this, null);
        }

        private async void Open_Click(object sender, RoutedEventArgs e)
        {
            await Launcher.LaunchFolderAsync(DownloadAgent.Downloads);
        }

        private void Refresh(object sender, RoutedEventArgs e)
        {
            list.Children.Clear();
            DownloadAgent.items.ForEach(i => list.Children.Add(i));

            empty.Visibility = list.Children.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
        }

        public void Remove(DownloadItem item)
        {
            list.Children.Remove(item);
            empty.Visibility = list.Children.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
