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

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

            list.Children.Clear();
            DownloadAgent.Items.ForEach(i => list.Children.Add(i));

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

            Methods.MainPage.PageContent.LoadingPage.Close();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            list.Children.Clear();
            DownloadAgent.Page = null;
        }

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

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