using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using Microsoft.AppCenter.Analytics;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.DataTransfer;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

namespace FoxTube.Controls
{
    /// <summary>
    /// Playlist card control
    /// </summary>
    public sealed partial class PlaylistCard : Page
    {
        Playlist item;
        public string playlistId;

        public bool NeedInitialize { get; set; } = true;

        public PlaylistCard(string id)
        {
            InitializeComponent();
            Initialize(id);
        }

        public async void Initialize(string id)
        {
            try
            {
                playlistId = id;
                PlaylistsResource.ListRequest request = SecretsVault.Service.Playlists.List("snippet,contentDetails");
                request.Id = playlistId;
                item = (await request.ExecuteAsync()).Items[0];

                title.Text = item.Snippet.Title;
                channelName.Text = item.Snippet.ChannelTitle;
                counter.Text = item.ContentDetails.ItemCount.ToString();
                date.Text = Methods.GetAgo(item.Snippet.PublishedAt.Value);

                ChannelsResource.ListRequest r = SecretsVault.Service.Channels.List("snippet");
                r.Id = item.Snippet.ChannelId;

                try { thumbnail.Source = new BitmapImage(item.Snippet.Thumbnails.Medium.Url.ToUri()); }
                catch { }
                try { avatar.ProfilePicture = new BitmapImage(new Uri((await r.ExecuteAsync()).Items[0].Snippet.Thumbnails.Medium.Url)); }
                catch { }

                Opacity = 1;
            }
            catch (Exception e)
            {
                Visibility = Visibility.Collapsed;
                Analytics.TrackEvent("PlaylistCard loading failed", new Dictionary<string, string>()
                {
                    { "Exception", e.GetType().ToString() },
                    { "Message", e.Message },
                    { "Playlist ID", playlistId }
                });
            }
        }

        public void Button_Click(object sender, RoutedEventArgs e)
        {
            Methods.MainPage.GoToPlaylist(item.Id);
        }

        private void OpenChannel_Click(object sender, RoutedEventArgs e)
        {
            Methods.MainPage.GoToChannel(item.Snippet.ChannelId);
        }

        private void GetLink_Click(object sender, RoutedEventArgs e)
        {
            DataPackage data = new DataPackage();
            data.SetText($"https://www.youtube.com/playlist?list={playlistId}");
            Clipboard.SetContent(data);
        }

        private async void InBrowser_Click(object sender, RoutedEventArgs e)
        {
            await Launcher.LaunchUriAsync($"https://www.youtube.com/playlist?list={playlistId}".ToUri());
        }

        private void Thumbnail_ImageOpened(object sender, RoutedEventArgs e)
        {
            thumbnail.Opacity = 1;
        }
    }
}
