using System;
using UnityEngine;
using UnityEngine.Rendering.Universal;

namespace NBF.Setting
{
    public enum AnisotropicLevelEnum
    {
        Off = -1,
        x2 = 2,
        x4 = 4,
        x8 = 8,
        x16 = 16
    }


    /// <summary>
    /// 全局各向异性过滤限制
    /// </summary>
    [Sort(8)]
    public class GlobalAnisotropicFilteringLimitsSetting : MultiOption<AnisotropicLevelEnum>
    {
        public override string Name => "GlobalAnisotropicFilteringLimits";
        public override string Group => SettingsDef.Group.Graphic;
        public override string Tab => SettingsDef.Tab.Graphic;
        protected override int DefaultValue => (int)AnisotropicLevelEnum.x4;

        protected override void OnInitialize()
        {
            var enumValues = Enum.GetValues(typeof(AnisotropicLevelEnum));
            foreach (var value in enumValues)
            {
                AddOption(value.ToString(), (AnisotropicLevelEnum)value);
            }
        }

        protected override void OnApply()
        {
            // 获取当前URP Asset
            UniversalRenderPipelineAsset URPAsset =
                QualitySettings.GetRenderPipelineAssetAt(QualitySettings.GetQualityLevel()) as
                    UniversalRenderPipelineAsset;
            if (URPAsset)
            {
                var current = QualitySettings.anisotropicFiltering;
                var level = GetSelectedOption();
                if (current == AnisotropicFiltering.Disable ||
                    current == AnisotropicFiltering.Enable)
                {
                    Texture.SetGlobalAnisotropicFilteringLimits(-1, -1);
                }
                else if (current == AnisotropicFiltering.ForceEnable)
                {
                    Texture.SetGlobalAnisotropicFilteringLimits((int)level, (int)level);
                }
            }
        }
    }
}