using Xamarin.Essentials;
using Xamarin.Forms;

namespace Pixiview.UI
{
    public static class StyleDefinition
    {
        public const double FontSizeTitle = 18.0;

        public static readonly Thickness ScreenBottomPadding;
        public static readonly Thickness TopOffset16 = new Thickness(0, 16, 0, 0);
        public static readonly Thickness TopOffset32 = new Thickness(0, 32, 0, 0);
        public static readonly Thickness TopOffset37 = new Thickness(0, 37, 0, 0);
        public static readonly Color ColorLightShadow = Color.FromRgba(0, 0, 0, 0x20);
        public static readonly Color ColorDeepShadow = Color.FromRgba(0, 0, 0, 0x50);
        public static readonly Color ColorRedBackground = Color.FromRgb(0xfd, 0x43, 0x63);
        public static readonly Color ColorDownloadBackground = Color.FromRgb(0xd7, 0xd9, 0xe0);
        public static readonly ImageSource DownloadBackground = ImageSource.FromFile("download.png");
        public static readonly ImageSource ProfileNone = ImageSource.FromFile("no_profile.png");
        public static readonly double FontSizeMicro = Device.GetNamedSize(NamedSize.Micro, typeof(Label));
        public static readonly double FontSizeSmall = Device.GetNamedSize(NamedSize.Small, typeof(Label));

#if __IOS__
        public const string IconLightFontFamily = "FontAwesome5Pro-Light";
        public const string IconRegularFontFamily = "FontAwesome5Pro-Regular";
        public const string IconSolidFontFamily = "FontAwesome5Pro-Solid";

        public const string IconLeft = "\uf104";
#elif __ANDROID__
        public const string IconLightFontFamily = "fa-light-300.ttf#FontAwesome5Pro-Light";
        public const string IconRegularFontFamily = "fa-regular-400.ttf#FontAwesome5Pro-Regular";
        public const string IconSolidFontFamily = "fa-solid-900.ttf#FontAwesome5Pro-Solid";

        public const string IconLeft = "\uf053";
#endif

        public const string IconUser = "\uf007";
        public const string IconSparkles = "\uf890";
        public const string IconOrder = "\uf88f";
        public const string IconLayer = "\uf302";
        public const string IconRefresh = "\uf2f9";
        public const string IconLove = "\uf004";
        public const string IconCircleLove = "\uf4c7";
        public const string IconOption = "\uf013";
        public const string IconFavorite = "\uf02e";
        public const string IconShare = "\uf1e0";
        public const string IconCaretDown = "\uf0d7";
        //public const string IconCaretUp = "\uf0d8";
        public const string IconCircleCheck = "\uf058";
        public const string IconPlay = "\uf04b";
        public const string IconPause = "\uf04c";
        public const string IconMore = "\uf142";
        public const string IconCaretCircleLeft = "\uf32e";
        public const string IconCaretCircleRight = "\uf330";
        public const string IconCalendarDay = "\uf783";
        public const string IconClose = "\uf057";
        public const string IconCloudDownload = "\uf381";

        static StyleDefinition()
        {
            _ = IsFullscreenDevice;
            if (_isBottomPadding)
            {
                if (DeviceInfo.Idiom == DeviceIdiom.Phone)
                {
                    ScreenBottomPadding = new Thickness(0, 0, 0, 26);
                }
                else
                {
                    ScreenBottomPadding = new Thickness(0, 0, 0, 16);
                }
            }
        }

        private static bool _isBottomPadding;
        private static bool? _isFullscreenDevice;
        public static bool IsFullscreenDevice
        {
            get
            {
                if (_isFullscreenDevice != null)
                {
                    return _isFullscreenDevice.Value;
                }
#if __IOS__
                try
                {
                    var model = DeviceInfo.Model;
                    if (model == "iPhone10,3")
                    {
                        // iPhone X
                        _isFullscreenDevice = true;
                        _isBottomPadding = true;
                    }
                    else if (model.StartsWith("iPhone"))
                    {
                        var vs = model.Substring(6).Split(',');
                        if (vs.Length == 2 && int.TryParse(vs[0], out int main) && int.TryParse(vs[1], out int sub))
                        {
                            // iPhone X/XS/XR or iPhone 11
                            var flag = (main == 10 && sub >= 6) || (main > 10);
                            _isFullscreenDevice = flag;
                            _isBottomPadding = flag;
                        }
                        else
                        {
                            _isFullscreenDevice = false;
                        }
                    }
                    else if (model.StartsWith("iPad8,"))
                    {
                        // iPad 11-inch or 12.9-inch (3rd+)
                        //_isFullscreenDevice = true;
                        _isBottomPadding = true;
                    }
#if DEBUG
                    else
                    {
                        // iPad or Simulator
                        var name = DeviceInfo.Name;
                        var flag = name.StartsWith("iPhone X")
                            || name.StartsWith("iPhone 11");
                        _isFullscreenDevice = flag;
                        _isBottomPadding = flag
                            || name.StartsWith("iPad Pro (11-inch)")
                            || name.StartsWith("iPad Pro (12.9-inch) (3rd generation)")
                            || name.StartsWith("iPad Pro (12.9-inch) (4th generation)");
                    }
#endif
                }
                catch (System.Exception ex)
                {
                    App.DebugError("device.get", $"failed to get the device model. {ex.Message}");
                }
#else
                // TODO:
                _isFullscreenDevice = false;
                _isBottomPadding = false;
#endif
                if (_isFullscreenDevice == null)
                {
                    _isFullscreenDevice = false;
                }
                return _isFullscreenDevice.Value;
            }
        }
    }
}
