using Windows.UI.Xaml.Controls;
using Google.Apis.YouTube.v3.Data;
using Google.Apis.YouTube.v3;
using Windows.UI.Xaml;
using System;
using Windows.UI.Popups;
using Windows.ApplicationModel.Resources;

namespace FoxTube.Controls
{
    public sealed partial class ReportVideo : ContentDialog
    {
        string videoId;
        public ReportVideo(string id)
        {
            InitializeComponent();
            Initialize();
            videoId = id;
        }

        async void Initialize()
        {
            VideoAbuseReportReasonsResource.ListRequest req = SecretsVault.Service.VideoAbuseReportReasons.List("id,snippet");
            req.Hl = SettingsStorage.RelevanceLanguage;
            VideoAbuseReportReasonListResponse reasons = await req.ExecuteAsync();

            foreach (VideoAbuseReportReason i in reasons.Items)
                primaryReason.Items.Add(new ComboBoxItem
                {
                    Tag = i,
                    Content = i.Snippet.Label
                });
        }

        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            VideoAbuseReport report = new VideoAbuseReport
            {
                Comments = Methods.GuardFromNull(comment.Text),
                VideoId = videoId,
                ReasonId = ((primaryReason.SelectedItem as ComboBoxItem).Tag as VideoAbuseReportReason).Id,
                SecondaryReasonId = secondaryReason.SelectedItem == null ? null : (secondaryReason.SelectedItem as ComboBoxItem).Tag as string
            };

            ResourceLoader resources = ResourceLoader.GetForCurrentView("Report");
            try { await SecretsVault.Service.Videos.ReportAbuse(report).ExecuteAsync(); }
            catch
            {
                await new MessageDialog(resources.GetString("/Report/err")).ShowAsync();
                return;
            }
            await new MessageDialog(resources.GetString("/Report/submittedHeader"), resources.GetString("/Report/submittedBody")).ShowAsync();
        }

        private void PrimaryReason_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IsPrimaryButtonEnabled = true;
            secondaryReason.Items.Clear();
            foreach (VideoAbuseReportSecondaryReason i in ((primaryReason.SelectedItem as ComboBoxItem).Tag as VideoAbuseReportReason).Snippet.SecondaryReasons)
                secondaryReason.Items.Add(new ComboBoxItem
                {
                    Tag = i.Id,
                    Content = i.Label
                });

            secondaryReason.Visibility = secondaryReason.Items.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}
