using Cryville.Common;
using Cryville.Common.Unity.UI;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Cryville.Crtr.Browsing {
	public class ResourceBrowserMaster : MonoBehaviour {
		[SerializeField]
		private Button m_playButton;
		[SerializeField]
		private Button m_configButton;

		private DockLayoutGroup _group;
		public ResourceBrowser MainBrowser { get; private set; }
		private DetailPanel _detailPanel;
		private SettingsPanel _settingsPanel;
		readonly List<ResourceBrowserUnit> _units = new List<ResourceBrowserUnit>();

#pragma warning disable IDE0051
		void Awake() {
			_group = GetComponent<DockLayoutGroup>();
			MainBrowser = transform.GetChild(0).GetComponent<ResourceBrowser>();
			MainBrowser.ResourceManager = new LegacyResourceManager(Settings.Default.GameDataPath);
			_detailPanel = transform.GetChild(1).GetComponent<DetailPanel>();
			_settingsPanel = transform.GetChild(2).GetComponent<SettingsPanel>();
			_units.Add(MainBrowser);
			_units.Add(_detailPanel);
		}

		int _slideDest = 0;
		void Update() {
			var cv = _group.SlideIndex;
			_group.SlideIndex = (cv - _slideDest) * 0.86f + _slideDest;
		}
#pragma warning restore IDE0051

		public void ShowDetail(int id, ChartDetail detail) {
			SlideIntoView(1);
			_detailPanel.Load(id, detail);
			m_playButton.gameObject.SetActive(true);
			m_configButton.gameObject.SetActive(true);
		}

		/*[Obsolete]
		public void ShowSettings(int id, ChartDetail detail) {
			SlideIntoView(2);
			_settingsPanel.Load(id, detail);
		}*/

		public bool Back() {
			if (_slideDest == 0) return false;
			SlideIntoView(_slideDest - 1);
			return true;
		}

		private void SlideIntoView(int v) {
			v = Mathf.Clamp(v, 0, _units.Count - 1);
			var cv = _group.SlideIndex;
			if (cv < v) _slideDest = v - 1;
			else _slideDest = v;
			_units[_slideDest].SlideToLeft();
			_units[_slideDest + 1].SlideToRight();
		}

		public void Open(int id, ChartDetail detail) {
			SetDataSettings(id, detail);
#if UNITY_5_3_OR_NEWER
			SceneManager.LoadScene("Play", LoadSceneMode.Additive);
#else
			Application.LoadLevelAdditive("Play");
#endif
			GameObject.Find("/Master").GetComponent<Master>().HideMenu();
		}

		public void OpenConfig(int id, ChartDetail detail) {
			SetDataSettings(id, detail);
#if UNITY_5_3_OR_NEWER
			SceneManager.LoadScene("Config", LoadSceneMode.Additive);
#else
			Application.LoadLevelAdditive("Config");
#endif
			GameObject.Find("/Master").GetComponent<Master>().HideMenu();
		}

		void SetDataSettings(int id, ChartDetail detail) {
			Settings.Default.LoadRuleset = detail.Meta.ruleset + "/.umgr";
			Settings.Default.LoadRulesetConfig = detail.Meta.ruleset + ".json";
			Settings.Default.LoadSkin = detail.Meta.ruleset + "/Old KeyUI/.umgs";
			Settings.Default.LoadChart = MainBrowser.ResourceManager.GetItemPath(id);
		}
	}

	public struct ChartDetail {
		public AsyncDelivery<Texture2D> Cover { get; set; }
		public ChartMeta Meta { get; set; }
	}

#pragma warning disable IDE1006
	public struct ChartMeta {
		public MetaInfo song { get; set; }
		public MetaInfo chart { get; set; }
		public struct MetaInfo {
			public string name { get; set; }
			public string author { get; set; }
			public float length { get; set; }
		}
		public string ruleset { get; set; }
		public int note_count { get; set; }
	}
#pragma warning restore IDE1006
}
