using System;
using System.Runtime.CompilerServices;
using Dalamud.Plugin.Services;

namespace OmicronMountMusicFixer;

internal static class BGMManager
{
	public delegate void SongChanged(int oldSong, int currentSong, int oldSecondSong, int oldCurrentSong, bool oldPlayedByOrch, bool playedByOrchestrion);

	[CompilerGenerated]
	private static class _003C_003EO
	{
		public static OnUpdateDelegate _003C0_003E__Update;
	}

	private static readonly BGMController _bgmController;

	private static bool _isPlayingReplacement;

	private static string _ddPlaylist;

	public static int CurrentSongId => _bgmController.CurrentSongId;

	public static int PlayingSongId => _bgmController.PlayingSongId;

	public static int CurrentAudibleSong => _bgmController.CurrentAudibleSong;

	public static int PlayingScene => _bgmController.PlayingScene;

	public static event SongChanged OnSongChanged;

	static BGMManager()
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		_bgmController = new BGMController();
		BGMController bgmController = _bgmController;
		bgmController.OnSongChanged = (BGMController.SongChangedHandler)Delegate.Combine(bgmController.OnSongChanged, new BGMController.SongChangedHandler(HandleSongChanged));
		DalamudApi.Framework.Update += new OnUpdateDelegate(Update);
	}

	public static void Dispose()
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Expected O, but got Unknown
		IFramework framework = DalamudApi.Framework;
		object obj = _003C_003EO._003C0_003E__Update;
		if (obj == null)
		{
			OnUpdateDelegate val = Update;
			_003C_003EO._003C0_003E__Update = val;
			obj = (object)val;
		}
		framework.Update -= (OnUpdateDelegate)obj;
		Stop();
		_bgmController.Dispose();
	}

	public static void Update(IFramework ignored)
	{
		_bgmController.Update();
	}

	private static void HandleSongChanged(int oldSong, int newSong, int oldSecondSong, int newSecondSong)
	{
		InvokeSongChanged(oldSong, newSong, oldSecondSong, newSecondSong, oldPlayedByOrch: false, playedByOrch: false);
	}

	public static void Play(int songId, bool isReplacement = false)
	{
		bool oldPlayedByOrch = PlayingSongId != 0;
		int currentAudibleSong = CurrentAudibleSong;
		int secondSongId = _bgmController.SecondSongId;
		DalamudApi.PluginLog.Debug($"[Play] Playing {songId}", Array.Empty<object>());
		InvokeSongChanged(currentAudibleSong, songId, secondSongId, currentAudibleSong, oldPlayedByOrch, playedByOrch: true);
		_bgmController.SetSong((ushort)songId);
		_isPlayingReplacement = isReplacement;
	}

	public static void Stop()
	{
		_ddPlaylist = null;
		if (PlayingSongId != 0)
		{
			DalamudApi.PluginLog.Debug($"[Stop] Stopping playing {_bgmController.PlayingSongId}...", Array.Empty<object>());
			int secondSongId = _bgmController.SecondSongId;
			InvokeSongChanged(PlayingSongId, CurrentSongId, secondSongId, secondSongId, oldPlayedByOrch: true, playedByOrch: false);
			_bgmController.SetSong(0);
		}
	}

	private static void InvokeSongChanged(int oldSongId, int newSongId, int oldSecondSongId, int newSecondSongId, bool oldPlayedByOrch, bool playedByOrch)
	{
		DalamudApi.PluginLog.Debug($"[InvokeSongChanged] Invoking SongChanged event with {oldSongId} -> {newSongId}, {oldSecondSongId} -> {newSecondSongId} | {oldPlayedByOrch} {playedByOrch}", Array.Empty<object>());
		BGMManager.OnSongChanged?.Invoke(oldSongId, newSongId, oldSecondSongId, newSecondSongId, oldPlayedByOrch, playedByOrch);
	}
}
