using RothenburgAR.Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RothenburgAR.UI
{
    public class FontsizeManager : Singleton<FontsizeManager>
    {
        public FontsizeSetting CurrentFontsizeSetting = FontsizeSetting.Default;

        private const float InchPerPt = 1f / 72f;
        private const float PtPerPixel = 0.75f;

        // font size in pt relative to dpi specific logical inch
        private readonly Dictionary<FontsizeSetting, Dictionary<FontsizeClass, float>> sizes =
        new Dictionary<FontsizeSetting, Dictionary<FontsizeClass, float>>
        {
            {
                FontsizeSetting.Default, new Dictionary<FontsizeClass, float>
                {
                    {FontsizeClass.Header, 48f},
                    {FontsizeClass.Body, 30f},
                    {FontsizeClass.ExhibitTitle, 1f}
                }
            },
            {
                FontsizeSetting.Large, new Dictionary<FontsizeClass, float>
                {
                    {FontsizeClass.Header, 56},
                    {FontsizeClass.Body, 35f},
                    {FontsizeClass.ExhibitTitle, 1.2f}
                }
            }
        };

        public float GetFontsize(FontsizeClass sizeClass, FontsizeSetting sizeSetting)
        {
            return sizes[sizeSetting][sizeClass];
        }

        /// <summary>
        /// Calculates font size that is roughly equal in size on "all" displays
        /// </summary>
        public float GetFontsize(FontsizeClass sizeClass)
        {
            return GetFontsize(sizeClass, CurrentFontsizeSetting);

            var dpi = Screen.dpi;
            var res = Screen.currentResolution;

            //TODO remove, just a hack to get readable fonts in the unity preview window
            dpi = dpi == 96 ? 320 : dpi;

            // Screen diagonal in px
            var diagonal = Mathf.Sqrt(Mathf.Pow(res.height, 2) + Mathf.Pow(res.width, 2));

            // Screen diagonal in logical inches
            var diagonalIn = diagonal / dpi;

            // ratio of font size to screen size
            var ratio = sizes[CurrentFontsizeSetting][sizeClass] * InchPerPt / diagonalIn;

            // final font size in pt
            var fontsizePt = diagonal * ratio * PtPerPixel;

            return fontsizePt;
        }
    }

    public enum FontsizeSetting
    {
        Default,
        Large
    }

    public enum FontsizeClass
    {
        Header,
        Body, 
        ExhibitTitle
    }
}