using System.Diagnostics.CodeAnalysis;
using Foundation;
using Pixiview.iOS.Renderers;
using Pixiview.UI;
using Pixiview.Utils;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(AdaptedPage), typeof(AdaptedPageRenderer))]
namespace Pixiview.iOS.Renderers
{
    public class AdaptedPageRenderer : PageRenderer
    {
        UIDeviceOrientation lastOrientation;
        NSObject observer;

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            if (Element is AdaptedPage page)
            {
                page.OnLoad();
            }
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            var style = ConvertStyle(StatusBar.GetStatusBarStyle(Element));
            if (UIApplication.SharedApplication.StatusBarStyle != style)
            {
                SetStatusBarStyle(style);
            }

            observer = UIDevice.Notifications.ObserveOrientationDidChange(ChangeOrientation);
            ChangeOrientation(null, null);
        }

        public override void ViewWillDisappear(bool animated)
        {
            if (observer != null)
            {
                observer.Dispose();
                observer = null;
            }

            base.ViewWillDisappear(animated);
        }

        private void SetStatusBarStyle(UIStatusBarStyle style)
        {
            if (style == UIStatusBarStyle.BlackOpaque)
            {
                UIApplication.SharedApplication.SetStatusBarHidden(true, UIStatusBarAnimation.Fade);
            }
            else
            {
                UIApplication.SharedApplication.SetStatusBarStyle(style, true);
                UIApplication.SharedApplication.SetStatusBarHidden(false, UIStatusBarAnimation.Fade);
            }

            SetNeedsStatusBarAppearanceUpdate();
        }

        [SuppressMessage("Code Notifications", "XI0002:Notifies you from using newer Apple APIs when targeting an older OS version", Justification = "<Pending>")]
        private UIStatusBarStyle ConvertStyle(StatusBarStyles style)
        {
            switch (style)
            {
                case StatusBarStyles.DarkText:
                    return UIStatusBarStyle.DarkContent;
                case StatusBarStyles.WhiteText:
                    return UIStatusBarStyle.LightContent;
                case StatusBarStyles.Hidden:
                    return UIStatusBarStyle.BlackOpaque;
                case StatusBarStyles.Default:
                default:
                    return UIStatusBarStyle.Default;
            }
        }

        void ChangeOrientation(object sender, NSNotificationEventArgs e)
        {
            var current = UIDevice.CurrentDevice.Orientation;
            if (current == UIDeviceOrientation.FaceUp || current == UIDeviceOrientation.FaceDown)
            {
                //current = UIDeviceOrientation.Portrait;
                return;
            }
            if (lastOrientation != current)
            {
                lastOrientation = current;
                if (Element is AdaptedPage page)
                {
                    page.OnOrientationChanged((Orientation)lastOrientation);
                }
            }
        }
    }
}
