using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using TagLib;
using UnityEngine;
using UnityEngine.UIElements;
using DiscordPresence;
using UnityEditor;
using MenuAndSongs;
using BangerTypes;
using System.Diagnostics;


namespace UITypes
{

	public class MainMenu : MonoBehaviour
	{
		private UIDocument document;

		private Button play;
		private Button soon;
		private Button quit;

		private Button back;
		private Button open;
		private Button reload;

		private ScrollView scrollView;

		private VisualElement mainMenu;
		private VisualElement levelSelect;
		public GameElement GameElem { get; set; }
		private LevelMaker makerElem;
		private Game game;

		public string MusicPath { get; set; }
		string hintFile = "Hint_place_songs_here.txt";

		public Sprite defaultImage;

		private bool songsLoaded = false;

		List<SongEntry> songs = new();

		const string reloadText = "RELOAD ⟳";
		const string reloadDone = "RELOAD ✔";

		private void AddSongEntry(Song song)
		{
			SongEntry entry = new()
			{
				Song = song
			};
			

			UnityEngine.Debug.Log(song.Clip);

			VisualElement entryToBeAdded = entry.InitEntry();
			UnityEngine.Debug.Log(song.Clip + " Clip");

			UnityEngine.Debug.Log(entryToBeAdded);
			songs.Add(entry);
			scrollView.Add(entryToBeAdded);
		}

		private void Awake()
		{
			MusicPath = Application.persistentDataPath + "/Music/";

			document = GetComponent<UIDocument>();
			game = GetComponent<Game>();

			play = document.rootVisualElement.Q("Play") as Button;
			soon = document.rootVisualElement.Q("Soon") as Button;
			quit = document.rootVisualElement.Q("Quit") as Button;
			back = document.rootVisualElement.Q("Back") as Button;
			open = document.rootVisualElement.Q("Open") as Button;
			reload = document.rootVisualElement.Q("Reload") as Button;

			play.style.backgroundColor = new Color(0, 0, 0, 0);
			soon.style.backgroundColor = new Color(0, 0, 0, 0);
			quit.style.backgroundColor = new Color(0, 0, 0, 0);
			back.style.backgroundColor = new Color(0, 0, 0, 0);
			open.style.backgroundColor = new Color(0, 0, 0, 0);
			reload.style.backgroundColor = new Color(0, 0, 0, 0);

			scrollView = document.rootVisualElement.Q("scroll") as ScrollView;

			mainMenu = document.rootVisualElement.Q("menu");
			levelSelect = document.rootVisualElement.Q("levels");
			GameElem = document.rootVisualElement.Q("game") as GameElement;


			play.RegisterCallback<ClickEvent>(Play);
			quit.RegisterCallback<ClickEvent>(Quit);

			back.RegisterCallback<ClickEvent>(Back);
			open.RegisterCallback<ClickEvent>(Open);
			reload.RegisterCallback<ClickEvent>(Reload);

			// DateTimeOffset dto = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

			// PresenceManager.UpdatePresence("In Main Menu", state: "Picking a banger", start: 100000, end: 1000000000);
		}

		private void Open(ClickEvent evt)
		{
			Process.Start(MusicPath);
		}

		// Start is called before the first frame update
		void Start()
		{
			GameElem.InitGame(game);

			mainMenu.style.display = DisplayStyle.Flex;
			levelSelect.style.display = DisplayStyle.None;
			GameElem.style.display = DisplayStyle.None;

		}

		private void Play(ClickEvent evt)
		{
			mainMenu.style.display = DisplayStyle.None;
			levelSelect.style.display = DisplayStyle.Flex;

			LoadSongs();
		}

		public void PlaySong(UITypes.SongEntry song)
		{
			GameElem.style.display = DisplayStyle.Flex;
			levelSelect.style.display = DisplayStyle.None;

			game.LoadSong(song);
			game.Play();
		}

		public void Quit(ClickEvent evt)
		{
			Application.Quit();
		}

		public void Escape()
		{
			GameElem.style.display = DisplayStyle.None;
			levelSelect.style.display = DisplayStyle.Flex;
		}

		public void Back(ClickEvent evt)
		{
			mainMenu.style.display = DisplayStyle.Flex;
			levelSelect.style.display = DisplayStyle.None;
		}

		public void Reload(ClickEvent evt)
		{
			songsLoaded = false;
			DisposeOldSongs();
			LoadSongs();
			StartCoroutine(ChangeReloadButtonText());
		}

		private void LoadSongs()
		{
			if (songsLoaded)
			{
				return;
			}

			if (!Directory.Exists(MusicPath))
			{
				Directory.CreateDirectory(MusicPath);
			}

			if (!System.IO.File.Exists(MusicPath + "/" + hintFile))
			{
				System.IO.File.Create(MusicPath + "/" + hintFile);
			}

			List<string> ls = Directory
				.GetFiles(MusicPath)
				.Where(file => Regex.IsMatch(file, ".*\\.rhys"))
				.ToList();

			foreach (string file in ls)
			{
				Song song = FindSongs.ParseRHYS(file, defaultImage);
				AddSongEntry(song);
			}

			songsLoaded = true;
		}

		private void DisposeOldSongs()
		{
			foreach (SongEntry song in songs)
			{
				song.RemoveFromHierarchy();
			}

			songs = new();
		}

		IEnumerator ChangeReloadButtonText()
		{
			float timeoutDuration = 0.5f;

			reload.text = reloadDone;
			reload.style.color = Color.green;
			// Wait for the specified timeout duration
			yield return new WaitForSeconds(timeoutDuration);

			reload.text = reloadText;
			reload.style.color = Color.white;
		}
	}
}
