using UnityEngine;

namespace BingoBrain.Core
{
    public class SunDgas : MonoBehaviour
    {
        private bool isOrthographic;
        private float orthographicSize;

        private Camera cameraCom;

        public void DoAdaptive(bool isOrthographic, float orthographicSize)
        {
            this.isOrthographic = isOrthographic;
            this.orthographicSize = orthographicSize;
            this.cameraCom = GetComponent<Camera>();

            Adaptive();
        }

#if UNITY_EDITOR
        private float currScreenHeight;
        private float currScreenWidth;

        private void Update()
        {
            if (currScreenHeight != Screen.height || currScreenWidth != Screen.width)
            {
                currScreenHeight = Screen.height;
                currScreenWidth = Screen.width;
                CenConst.CurrAspectRatio = (float)Screen.height / Screen.width;
                Adaptive();
            }
        }
#endif

        private void Adaptive()
        {
            if (cameraCom == null) return;

            cameraCom.orthographic = isOrthographic;
            if (isOrthographic)
            {
                if (CenConst.CurrAspectRatio > CenConst.StandardAspectRatio)
                {
                    float computeHeight = Screen.width * CenConst.StandardAspectRatio;
                    float heightRatio = Screen.height / computeHeight;
                    cameraCom.orthographicSize = orthographicSize * heightRatio;
                }
                else
                {
                    cameraCom.orthographicSize = orthographicSize;
                }
            }
            else
            {
                float fov = Get3DFOV();
                cameraCom.fieldOfView = fov;
            }
        }

        private float Get3DFOV()
        {
            float defaultFOV = 60;
            float standardWidth = CenConst.StandardWidth;
            float standardHeight = CenConst.StandardHeight;
            float currWidth = Screen.width;
            float currHeight = Screen.height;
            float nowHeight;

            if (CenConst.CurrAspectRatio > CenConst.StandardAspectRatio)
            {
                nowHeight = Mathf.RoundToInt(standardWidth / currWidth * currHeight);
            }
            else
            {
                nowHeight = standardHeight;
            }

            float heightScale = nowHeight / standardHeight;
            float fov = defaultFOV * heightScale;
            return fov;
        }
    }
}