using System;
using Xamarin.Forms;

namespace Pixiview.UI
{
    public class AdaptedPage : ContentPage
    {
        static readonly Thickness LandscapeLeftPadding = new Thickness(34, 0, 0, 0);
        static readonly Thickness LandscapeRightPadding = new Thickness(0, 0, 34, 0);

        public static readonly BindableProperty StatusBarPaddingProperty = BindableProperty.Create(
            nameof(StatusBarPadding), typeof(Thickness), typeof(AdaptedPage), default(Thickness));

        public Thickness StatusBarPadding
        {
            get => (Thickness)GetValue(StatusBarPaddingProperty);
            set => SetValue(StatusBarPaddingProperty, value);
        }

        public event EventHandler Load;
        public event EventHandler<OrientationEventArgs> OrientationChanged;

        public virtual void OnLoad()
        {
            Load?.Invoke(this, EventArgs.Empty);
        }

        public virtual void OnOrientationChanged(Orientation orientation)
        {
            if (orientation == Orientation.LandscapeLeft || orientation == Orientation.PortraitUpsideDown)
            {
                StatusBarPadding = StyleDefinition.IsFullscreenDevice ? LandscapeLeftPadding : default;
            }
            else if (orientation == Orientation.LandscapeRight)
            {
                StatusBarPadding = StyleDefinition.IsFullscreenDevice ? LandscapeRightPadding : default;
            }
            else
            {
                StatusBarPadding = default;
            }

            OrientationChanged?.Invoke(this, new OrientationEventArgs { CurrentOrientation = orientation });
        }
    }

    public class OrientationEventArgs : EventArgs
    {
        public Orientation CurrentOrientation { get; set; }
    }

    public enum Orientation
    {
        Unknown,
        Portrait,
        PortraitUpsideDown,
        LandscapeLeft,
        LandscapeRight
    }
}
