namespace Monkey.Game
{

    public static class QualityManager
    {

        private const string FOG_VOLUME_GAME_OBJECT_PATH = "/indoor Sky and Fog Volume";
        private const string CAMERA_VOLUME_GAME_OBJECT_PATH = "/Fps Camera Rig/Indoor Main Camera/Volume";

        public static bool IsFogVolumeEnabled
        {
            get
            {
                UnityEngine.Rendering.Volume fogVolume = GetFogVolume();

                if (fogVolume == null)
                    return false;

                return fogVolume.enabled;
            }
            set
            {
                UnityEngine.Rendering.Volume fogVolume = GetFogVolume();

                if (fogVolume == null)
                    return;

                fogVolume.enabled = value;
            }
        }

        public static bool IsAmbientOcclusionEnabled
        {
            get => IsCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.AmbientOcclusion>();
            set => SetCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.AmbientOcclusion>(value);
        }

        public static bool IsBloomEnabled
        {
            get => IsCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.Bloom>();
            set => SetCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.Bloom>(value);
        }

        public static bool IsDepthOfFieldEnabled
        {
            get => IsCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.DepthOfField>();
            set => SetCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.DepthOfField>(value);
        }

        public static bool IsMotionBlurEnabled
        {
            get => IsCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.MotionBlur>();
            set => SetCameraVolumeComponentEnabled<UnityEngine.Rendering.HighDefinition.MotionBlur>(value);
        }

        static QualityManager()
        {

        }

        private static UnityEngine.GameObject GetFogVolumeGameObject()
        {
            return UnityEngine.GameObject.Find(FOG_VOLUME_GAME_OBJECT_PATH);
        }

        private static UnityEngine.GameObject GetCameraVolumeGameObject()
        {
            return UnityEngine.GameObject.Find(CAMERA_VOLUME_GAME_OBJECT_PATH);
        }

        private static UnityEngine.Rendering.Volume GetFogVolume()
        {
            UnityEngine.GameObject fogVolumeGameObject = GetFogVolumeGameObject();

            if (fogVolumeGameObject == null)
                return null;

            return fogVolumeGameObject.GetComponent<UnityEngine.Rendering.Volume>();
        }

        private static T GetCameraVolumeComponent<T>() where T : UnityEngine.Rendering.HighDefinition.VolumeComponentWithQuality
        {
            UnityEngine.GameObject cameraVolumeGameObject = GetCameraVolumeGameObject();

            if (cameraVolumeGameObject == null)
                return null;

            UnityEngine.Rendering.Volume volume = cameraVolumeGameObject.GetComponent<UnityEngine.Rendering.Volume>();

            if ((volume == null) || (volume.profile == null))
                return null;

            if (!volume.profile.TryGet<T>(out T cameraVolumeComponent))
                return null;

            return cameraVolumeComponent;
        }

        private static bool IsCameraVolumeComponentEnabled<T>() where T : UnityEngine.Rendering.HighDefinition.VolumeComponentWithQuality
        {
            UnityEngine.Rendering.HighDefinition.VolumeComponentWithQuality cameraVolumeComponent = GetCameraVolumeComponent<T>();

            if (cameraVolumeComponent == null)
                return false;

            return cameraVolumeComponent.active;
        }

        private static void SetCameraVolumeComponentEnabled<T>(bool isEnabled) where T : UnityEngine.Rendering.HighDefinition.VolumeComponentWithQuality
        {
            UnityEngine.Rendering.HighDefinition.VolumeComponentWithQuality cameraVolumeComponent = GetCameraVolumeComponent<T>();

            if (cameraVolumeComponent == null)
                return;

            cameraVolumeComponent.active = isEnabled;
        }

    }

}
