using System.Diagnostics.CodeAnalysis;
using CoreGraphics;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

namespace Gallery.iOS.Renderers.AppShellSection
{
    public class AppShellNavBarAppearanceTracker : IShellNavBarAppearanceTracker
    {
        UIColor _defaultBarTint;
        UIColor _defaultTint;
        UIStringAttributes _defaultTitleAttributes;
        float _shadowOpacity = float.MinValue;
        CGColor _shadowColor;

        public void ResetAppearance(UINavigationController controller)
        {
            if (_defaultTint != null)
            {
                var navBar = controller.NavigationBar;
                navBar.TintColor = _defaultBarTint;
                navBar.TintColor = _defaultTint;
                navBar.TitleTextAttributes = _defaultTitleAttributes;
            }
        }

        public void SetAppearance(UINavigationController controller, ShellAppearance appearance)
        {
            var background = appearance.BackgroundColor;
            var foreground = appearance.ForegroundColor;
            var titleColor = appearance.TitleColor;

            var navBar = controller.NavigationBar;

            if (_defaultTint == null)
            {
                _defaultBarTint = navBar.BarTintColor;
                _defaultTint = navBar.TintColor;
                _defaultTitleAttributes = navBar.TitleTextAttributes;
            }

            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                navBar.TintColor = UIColor.SecondaryLabelColor;
            }
            else
            {
                if (!background.IsDefault)
                    navBar.BarTintColor = background.ToUIColor();
                if (!foreground.IsDefault)
                    navBar.TintColor = foreground.ToUIColor();
                if (!titleColor.IsDefault)
                {
                    navBar.TitleTextAttributes = new UIStringAttributes
                    {
                        ForegroundColor = titleColor.ToUIColor()
                    };
                }
            }
        }

        public void SetHasShadow(UINavigationController controller, bool hasShadow)
        {
            var navigationBar = controller.NavigationBar;
            if (_shadowOpacity == float.MinValue)
            {
                // Don't do anything if user hasn't changed the shadow to true
                if (!hasShadow)
                    return;

                _shadowOpacity = navigationBar.Layer.ShadowOpacity;
                _shadowColor = navigationBar.Layer.ShadowColor;
            }

            if (hasShadow)
            {
                navigationBar.Layer.ShadowColor = UIColor.Black.CGColor;
                navigationBar.Layer.ShadowOpacity = 1.0f;
            }
            else
            {
                navigationBar.Layer.ShadowColor = _shadowColor;
                navigationBar.Layer.ShadowOpacity = _shadowOpacity;
            }
        }

        public void Dispose()
        {
        }

        public void UpdateLayout(UINavigationController controller)
        {
        }
    }

    public class AppShellTabBarAppearanceTracker : IShellTabBarAppearanceTracker
    {
        UIColor _defaultBarTint;
        UIColor _defaultTint;
        UIColor _defaultUnselectedTint;

        public void ResetAppearance(UITabBarController controller)
        {
            if (_defaultTint == null)
                return;

            var tabBar = controller.TabBar;
            tabBar.BarTintColor = _defaultBarTint;
            tabBar.TintColor = _defaultTint;
            tabBar.UnselectedItemTintColor = _defaultUnselectedTint;
        }

        public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
        {
            IShellAppearanceElement appearanceElement = appearance;
            var backgroundColor = appearanceElement.EffectiveTabBarBackgroundColor;
            var unselectedColor = appearanceElement.EffectiveTabBarUnselectedColor;
            var tintColor = appearanceElement.EffectiveTabBarForegroundColor; // appearanceElement.EffectiveTabBarTitleColor;

            var tabBar = controller.TabBar;

            if (_defaultTint == null)
            {
                _defaultBarTint = tabBar.BarTintColor;
                _defaultTint = tabBar.TintColor;
                _defaultUnselectedTint = tabBar.UnselectedItemTintColor;
            }

            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                tabBar.TintColor = UIColor.LabelColor;
                //tabBar.UnselectedItemTintColor = UIColor.TertiaryLabelColor;
            }
            else
            {
                if (!backgroundColor.IsDefault)
                    tabBar.BarTintColor = backgroundColor.ToUIColor();
                if (!tintColor.IsDefault)
                    tabBar.TintColor = tintColor.ToUIColor();
                if (!unselectedColor.IsDefault)
                    tabBar.UnselectedItemTintColor = unselectedColor.ToUIColor();
            }
        }

        public void Dispose()
        {
        }

        [SuppressMessage("Code Notifications", "XI0001:Notifies you with advices on how to use Apple APIs", Justification = "<Pending>")]
        public void UpdateLayout(UITabBarController controller)
        {
            var tabBar = controller.TabBar;
            if (tabBar != null && tabBar.Items != null && tabBar.Items.Length > 0)
            {
                var tabBarItem = tabBar.Items[0];
                tabBarItem.Image = UIImage.FromBundle("IconBookmarkRegular");
                tabBarItem.SelectedImage = UIImage.FromBundle("IconBookmark");
                if (tabBar.Items.Length > 1)
                {
                    tabBarItem = tabBar.Items[1];
                    tabBarItem.Image = UIImage.FromBundle("IconYandereRegular");
                    tabBarItem.SelectedImage = UIImage.FromBundle("IconYandere");
                }
                if (tabBar.Items.Length > 2)
                {
                    for (var i = 2; i < tabBar.Items.Length; i++)
                    {
                        tabBarItem = tabBar.Items[i];
                        tabBarItem.Image = UIImage.FromBundle("IconSourceRegular");
                        tabBarItem.SelectedImage = UIImage.FromBundle("IconSource");
                    }
                }
            }
        }
    }
}