using System.Collections;
using UnityEngine;

namespace Cute;

public class QualityManager : MonoBehaviour, IManager
{
	public enum GPUQualityLevel
	{
		Level_1,
		Level_2,
		Level_3,
		Level_4
	}

	public enum AssetQualityLevel
	{
		None,
		Level_1,
		Level_2,
		Max
	}

	public enum SoundQualityLevel
	{
		None,
		Level_1,
		Level_2,
		Max
	}

	public enum MovieQualityLevel
	{
		None,
		Level_1,
		Level_2,
		Max
	}

	public enum MemoryQualityLevel
	{
		Level_1,
		Level_2
	}

	public enum GameQualityLevel
	{
		Level_1,
		Level_2,
		Level_3,
		Level_4
	}

	public enum OutLineLevel
	{
		OutLine_On,
		OutLine_Off
	}

	private GPUQualityLevel _gpuQualityLevel;

	private AssetQualityLevel _assetQualityLevel;

	private SoundQualityLevel _soundQualityLevel;

	private MovieQualityLevel _movieQualityLevel;

	private MemoryQualityLevel _memoryQualityLevel;

	private Vector2 _deviceResolution = Vector2.zero;

	private Vector2 _gameResolution = Vector2.zero;

	private int _defaultFrameRate = 60;

	private bool _isDeviceResolutionSetting;

	public bool isFullScreen;

	private GameQualityLevel _gameQualityLevel;

	private OutLineLevel _outlineLevel;

	public Vector2 deviceResolution => _deviceResolution;

	public Vector2 gameResolution => _gameResolution;

	private void Awake()
	{
		UpdateFromUnity();
	}

	private IEnumerator Start()
	{
		while (Toolbox.SavedataManager == null)
		{
			yield return 0;
		}
		Initialize();
		Toolbox.QualityManager = this;
	}

	private void OnDestroy()
	{
	}

	public void Initialize()
	{
		CheckGPUQuality();
		CheckMemoryQuality();
		CheckAssetQuality();
		CheckSoundQuality();
		CheckMovieQuality();
		InitializeGameQuality();
	}

	private void CheckGPUQuality()
	{
		_gpuQualityLevel = GPUQualityLevel.Level_4;
	}

	private void CheckMemoryQuality()
	{
		_memoryQualityLevel = MemoryQualityLevel.Level_2;
	}

	private void CheckAssetQuality()
	{
		_assetQualityLevel = ((_memoryQualityLevel == MemoryQualityLevel.Level_1) ? AssetQualityLevel.Level_1 : AssetQualityLevel.Level_2);
		SetAssetQualityLevel(_assetQualityLevel);
	}

	private void CheckSoundQuality()
	{
		int num = Toolbox.SavedataManager.GetInt("SOUNDQUALIY");
		if (num != 0)
		{
			_soundQualityLevel = (SoundQualityLevel)num;
		}
		else
		{
			_soundQualityLevel = ((_memoryQualityLevel == MemoryQualityLevel.Level_1) ? SoundQualityLevel.Level_1 : SoundQualityLevel.Level_2);
		}
		SetSoundQualityLevel(_soundQualityLevel);
	}

	private void CheckMovieQuality()
	{
		_movieQualityLevel = ((_memoryQualityLevel == MemoryQualityLevel.Level_1) ? MovieQualityLevel.Level_1 : MovieQualityLevel.Level_2);
	}

	public static GPUQualityLevel GetGPUQualityLevel()
	{
		if (Toolbox.QualityManager == null)
		{
			return GPUQualityLevel.Level_1;
		}
		return Toolbox.QualityManager._gpuQualityLevel;
	}

	public static MemoryQualityLevel GetMemoryQualityLevel()
	{
		if (Toolbox.QualityManager == null)
		{
			return MemoryQualityLevel.Level_1;
		}
		return Toolbox.QualityManager._memoryQualityLevel;
	}

	public static void SetAssetQualityLevel(AssetQualityLevel _AssetQualityLevel)
	{
		if (Toolbox.QualityManager != null)
		{
			Toolbox.QualityManager._assetQualityLevel = _AssetQualityLevel;
		}
	}

	public static AssetQualityLevel GetAssetQualityLevel()
	{
		if (Toolbox.QualityManager == null)
		{
			return AssetQualityLevel.Level_1;
		}
		return Toolbox.QualityManager._assetQualityLevel;
	}

	public static void SetSoundQualityLevel(SoundQualityLevel _SoundQualityLevel)
	{
		if (Toolbox.QualityManager != null)
		{
			Toolbox.QualityManager._soundQualityLevel = _SoundQualityLevel;
			Toolbox.SavedataManager.SetInt("SOUNDQUALIY", (int)Toolbox.QualityManager._soundQualityLevel);
		}
	}

	public static SoundQualityLevel GetSoundQualityLevel()
	{
		if (Toolbox.QualityManager == null)
		{
			return SoundQualityLevel.Level_1;
		}
		return Toolbox.QualityManager._soundQualityLevel;
	}

	public static void SetMovieQualityLevel(MovieQualityLevel _MovieQualityLevel)
	{
		if (Toolbox.QualityManager != null)
		{
			Toolbox.QualityManager._movieQualityLevel = _MovieQualityLevel;
		}
	}

	public static MovieQualityLevel GetMovieQualityLevel()
	{
		if (Toolbox.QualityManager == null)
		{
			return MovieQualityLevel.Level_1;
		}
		return Toolbox.QualityManager._movieQualityLevel;
	}

	public void LimitResolution()
	{
		_gameResolution = _deviceResolution;
		if (_deviceResolution.x > 1280f || _deviceResolution.y > 1280f)
		{
			float num = 1280f / _deviceResolution.x;
			_gameResolution = new Vector2(_deviceResolution.x * num, _deviceResolution.y * num);
			Screen.SetResolution((int)_gameResolution.x, (int)_gameResolution.y, isFullScreen);
		}
	}

	public void ChangeResolution(float rate, bool saveratio = true)
	{
		LimitResolution();
		float num = _gameResolution.x * rate / _gameResolution.x;
		_gameResolution = new Vector2(_gameResolution.x * num, _gameResolution.y * num);
		Screen.SetResolution((int)_gameResolution.x, (int)_gameResolution.y, isFullScreen);
	}

	public void ChangeResolutionToDeviceSetting()
	{
		_gameResolution = _deviceResolution;
		Screen.SetResolution((int)_gameResolution.x, (int)_gameResolution.y, isFullScreen);
	}

	public void ChangeResolution(float width, float height, bool fullscreen)
	{
		if (width == 0f || height == 0f)
		{
			width = Screen.width;
			height = Screen.height;
		}
		isFullScreen = fullscreen;
		_deviceResolution = new Vector2(width, height);
		Screen.SetResolution((int)width, (int)height, fullscreen);
	}

	private void InitializeGameQuality()
	{
		if (_gpuQualityLevel == GPUQualityLevel.Level_4)
		{
			_gameQualityLevel = GameQualityLevel.Level_4;
		}
		else if (_gpuQualityLevel == GPUQualityLevel.Level_3)
		{
			_gameQualityLevel = GameQualityLevel.Level_3;
		}
		else if (_gpuQualityLevel == GPUQualityLevel.Level_2)
		{
			_gameQualityLevel = GameQualityLevel.Level_2;
		}
		else
		{
			_gameQualityLevel = GameQualityLevel.Level_1;
		}
		if (_memoryQualityLevel == MemoryQualityLevel.Level_1)
		{
			_gameQualityLevel = GameQualityLevel.Level_1;
		}
		if (!_isDeviceResolutionSetting)
		{
			_deviceResolution = new Vector2(Screen.width, Screen.height);
			_isDeviceResolutionSetting = true;
		}
		if (_gpuQualityLevel <= GPUQualityLevel.Level_2 && Toolbox.SavedataManager.GetInt("SOUNDQUALIY") == 0)
		{
			SetSoundQualityLevel(SoundQualityLevel.Level_1);
		}
		DecideDeviceSetting();
	}

	private void DecideDeviceSetting()
	{
		string graphicsDeviceName = SystemInfo.graphicsDeviceName;
		if (graphicsDeviceName.Contains("PowerVR") && graphicsDeviceName.Contains("SGX 540"))
		{
			_outlineLevel = OutLineLevel.OutLine_Off;
		}
	}

	public void SetGameQualityLevel(GPUQualityLevel level)
	{
		_gpuQualityLevel = level;
	}

	public GameQualityLevel GetGameQualityLevel()
	{
		return _gameQualityLevel;
	}

	public void SetFrameRate(int frameRate)
	{
		_defaultFrameRate = frameRate;
	}

	public int GetFrameRate()
	{
		return _defaultFrameRate;
	}

	public void ChangeResolutionFixedHalf()
	{
	}

	public void ChangeResolutionFixedBack()
	{
	}

	public OutLineLevel GetOutLineLevel()
	{
		return _outlineLevel;
	}

	public void UpdateFromUnity()
	{
		isFullScreen = Screen.fullScreen;
		_deviceResolution = new Vector2(Screen.width, Screen.height);
	}
}
