using System;
using Xamarin.Essentials;
using Xamarin.Forms;

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

        public static readonly Thickness HorizonLeft10 = new Thickness(10, 0, 0, 0);
        public static readonly Thickness HorizonRight10 = new Thickness(0, 0, 10, 0);
        public static readonly Thickness LeftBottom10 = new Thickness(10, 0, 0, 10);

        public const string IconLayer = "\uf302";

        private static bool? _isFullscreenDevice;
        public static bool IsFullscreenDevice
        {
            get
            {
                if (_isFullscreenDevice != null)
                {
                    return _isFullscreenDevice.Value;
                }
                if (Device.RuntimePlatform == Device.iOS)
                {
                    try
                    {
                        var model = DeviceInfo.Model;
                        if (model == "iPhone10,3")
                        {
                            // iPhone X
                            _isFullscreenDevice = 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
                                _isFullscreenDevice = (main == 10 && sub >= 6) || (main > 10);
                            }
                            else
                            {
                                _isFullscreenDevice = false;
                            }
                        }
                        else if (model.StartsWith("iPad8,"))
                        {
                            // iPad 11-inch or 12.9-inch (3rd+)
                            _isFullscreenDevice = true;
                        }
#if DEBUG
                        else
                        {
                            // iPad or Simulator
                            var name = DeviceInfo.Name;
                            _isFullscreenDevice = name.StartsWith("iPhone X")
                                || name.StartsWith("iPhone 11")
                                || 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 (Exception ex)
                    {
                        App.DebugError("device.get", $"failed to get the device model. {ex.Message}");
                    }
                }
                else
                {
                    // TODO:
                    _isFullscreenDevice = false;
                }
                if (_isFullscreenDevice == null)
                {
                    _isFullscreenDevice = false;
                }
                return _isFullscreenDevice.Value;
            }
        }
    }
}
