using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace FoxTube.Controls
{
    public sealed partial class Advert : UserControl
    {
        public string AdUnitId { get; set; } = "test";
        public string AppId => "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
        public bool OverrideSize { get; set; } = false;

        public new double Height
        {
            get { return ad.Height; }
            set
            {
                ad.Height = value;
                OverrideSize = true;
            }
        }

        public new double Width
        {
            get { return ad.Width; }
            set
            {
                ad.Width = value;
                OverrideSize = true;
            }
        }

        public Advert()
        {
            InitializeComponent();
            if (!SecretsVault.AdsDisabled)
                Visibility = Visibility.Visible;
            else
                Visibility = Visibility.Collapsed;
            SecretsVault.Purchased += (s, e) => Visibility = (bool)e[0] ? Visibility.Collapsed : Visibility.Visible;
        }

        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (OverrideSize)
                return;

            if(grid.ActualWidth >= 728)
            {
                ad.Width = 728;
                ad.Height = 90;
            }
            else if (grid.ActualWidth >= 640)
            {
                ad.Width = 640;
                ad.Height = 100;
            }
            else if (grid.ActualWidth >= 320)
            {
                ad.Width = 320;
                ad.Height = 50;
            }
            else
            {
                ad.Width = 300;
                ad.Height = 50;
            }
        }
    }
}
