/****************************************************
    文件：FixScreenRatio.cs
    作者：陶长春
    邮箱：376248129@qq.com
    日期：2025年3月13日 9:10:37
    UnityVersion: 2021.3.13f1
    功能：UI适配
*****************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using TFramework;
using UnityEngine;
using UnityEngine.UI;

namespace TFramework
{
    public class FixScreenRatio : MonoBehaviour
    {
        public enum Orientation
        {
            Portrait,
            Landscape
        }

        public float defaultModifier = 0.5f;
        public Vector2 currentRes = new Vector2();
        public Vector2 newRes = new Vector2();
        public float refWidth = 480;
        public float refHeight = 320;
        public Orientation refOrientation = Orientation.Portrait;

        void OnEnable()
        {
            // Initially update scale
            newRes.x = Screen.width;
            newRes.y = Screen.height;
            UpdateScaling();
        }

        void Update()
        {
            // Update if resolution has changed
            newRes.x = Screen.width;
            newRes.y = Screen.height;
            if (newRes != currentRes)
            {
                UpdateScaling();
            }
        }

        private void OnValidate()
        {
            UpdateScaling();
        }

        /// <summary>
        /// Handles updating of the canvas scale based on device resolution
        /// </summary>
        public virtual void UpdateScaling()
        {
            currentRes = newRes;

            // Determine platform specific resolution multiplier
            float multiplier = 1f;
#if UNITY_ANDROID || UNITY_IOS
            switch (refOrientation)
            {
                case Orientation.Portrait:
                    multiplier = 1f + ((((float)currentRes.y - refHeight) / refHeight) * defaultModifier);
                    break;
                case Orientation.Landscape:
                    multiplier = 1f + ((((float)currentRes.x - refWidth) / refWidth) * defaultModifier);
                    break;
            }
#else
        // For desktop platforms we scale based on the largest value out of width & height
        if (currentRes.x > currentRes.y)
        {
            multiplier = 1f + ((((float)currentRes.y - refHeight) / refHeight) * defaultModifier);
        }
        else
        {
            multiplier = 1f + ((((float)currentRes.x - refWidth) / refWidth) * defaultModifier);
        }
#endif

            // Modify multiplier based on initial value.
            if (multiplier < 0.5f) // Ceil to 0.5 if less than 0.5
            {
                multiplier = 0.5f;
            }
            else if (multiplier < 2f) // Calc to the nearest 0.5 if less than 2
            {
                multiplier = Mathf.Max(1f, (float)(Math.Round(((double)multiplier * 2), MidpointRounding.AwayFromZero) / 2));
            }
            else // Otherwise if larger than 2 then round
            {
                multiplier = (float)Math.Round(multiplier, MidpointRounding.AwayFromZero);
            }

            // Apply new scale factor to canvas 
            GetComponent<CanvasScaler>().scaleFactor = multiplier;
        }
    }
}
