using System;

namespace SharpQuake
{
	internal static class cd_audio
	{
		public static bool Init()
		{
			if (client.cls.state == cactive_t.ca_dedicated)
			{
				return false;
			}
			if (common.HasParam("-nocdaudio"))
			{
				return false;
			}
			cd_audio._Controller.Init();
			if (cd_audio._Controller.IsInitialized)
			{
				cmd.Add("cd", new xcommand_t(cd_audio.CD_f));
				Con.Print("CD Audio (Fallback) Initialized\n", Array.Empty<object>());
			}
			return cd_audio._Controller.IsInitialized;
		}

		public static void Play(byte track, bool looping)
		{
			cd_audio._Controller.Play(track, looping);
		}

		public static void Stop()
		{
			cd_audio._Controller.Stop();
		}

		public static void Pause()
		{
			cd_audio._Controller.Pause();
		}

		public static void Resume()
		{
			cd_audio._Controller.Resume();
		}

		public static void Shutdown()
		{
			cd_audio._Controller.Shutdown();
		}

		public static void Update()
		{
			cd_audio._Controller.Update();
		}

		private static void CD_f()
		{
			if (cmd.Argc < 2)
			{
				return;
			}
			string a = cmd.Argv(1);
			if (common.SameText(a, "on"))
			{
				cd_audio._Controller.IsEnabled = true;
				return;
			}
			if (common.SameText(a, "off"))
			{
				if (cd_audio._Controller.IsPlaying)
				{
					cd_audio._Controller.Stop();
				}
				cd_audio._Controller.IsEnabled = false;
				return;
			}
			if (common.SameText(a, "reset"))
			{
				cd_audio._Controller.IsEnabled = true;
				if (cd_audio._Controller.IsPlaying)
				{
					cd_audio._Controller.Stop();
				}
				cd_audio._Controller.ReloadDiskInfo();
				return;
			}
			if (common.SameText(a, "remap"))
			{
				int num = cmd.Argc - 2;
				byte[] remap = cd_audio._Controller.Remap;
				if (num <= 0)
				{
					for (int i = 1; i < 100; i++)
					{
						if ((int)remap[i] != i)
						{
							Con.Print("  {0} -> {1}\n", new object[]
							{
								i,
								remap[i]
							});
						}
					}
					return;
				}
				for (int j = 1; j <= num; j++)
				{
					remap[j] = (byte)common.atoi(cmd.Argv(j + 1));
				}
				return;
			}
			else
			{
				if (common.SameText(a, "close"))
				{
					cd_audio._Controller.CloseDoor();
					return;
				}
				if (!cd_audio._Controller.IsValidCD)
				{
					cd_audio._Controller.ReloadDiskInfo();
					if (!cd_audio._Controller.IsValidCD)
					{
						Con.Print("No CD in player.\n", Array.Empty<object>());
						return;
					}
				}
				if (common.SameText(a, "play"))
				{
					cd_audio._Controller.Play((byte)common.atoi(cmd.Argv(2)), false);
					return;
				}
				if (common.SameText(a, "loop"))
				{
					cd_audio._Controller.Play((byte)common.atoi(cmd.Argv(2)), true);
					return;
				}
				if (common.SameText(a, "stop"))
				{
					cd_audio._Controller.Stop();
					return;
				}
				if (common.SameText(a, "pause"))
				{
					cd_audio._Controller.Pause();
					return;
				}
				if (common.SameText(a, "resume"))
				{
					cd_audio._Controller.Resume();
					return;
				}
				if (common.SameText(a, "eject"))
				{
					if (cd_audio._Controller.IsPlaying)
					{
						cd_audio._Controller.Stop();
					}
					cd_audio._Controller.Eject();
					return;
				}
				if (common.SameText(a, "info"))
				{
					Con.Print("%u tracks\n", new object[]
					{
						cd_audio._Controller.MaxTrack
					});
					if (cd_audio._Controller.IsPlaying)
					{
						Con.Print("Currently {0} track {1}\n", new object[]
						{
							cd_audio._Controller.IsLooping ? "looping" : "playing",
							cd_audio._Controller.CurrentTrack
						});
					}
					else if (cd_audio._Controller.IsPaused)
					{
						Con.Print("Paused {0} track {1}\n", new object[]
						{
							cd_audio._Controller.IsLooping ? "looping" : "playing",
							cd_audio._Controller.CurrentTrack
						});
					}
					Con.Print("Volume is {0}\n", new object[]
					{
						cd_audio._Controller.Volume
					});
					return;
				}
				return;
			}
		}

		private static NullCDAudioController _Controller = new NullCDAudioController();
	}
}
