using System.Collections.Generic;
using InteractiveModules;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Game
{
	[CreateAssetMenu(menuName = "Game Screens/Game Screen")]
	public class GameScreenAsset : ScriptableObject
	{
		[Header("Next screen"), HideIf("IsInteractive"), HideIf("HideNextScreenInspector")]
		public GameScreenAsset nextScreen;
		
		[InfoBox("No module active!", "ShowModuleMinWarning", InfoMessageType = InfoMessageType.Error)]
		[InfoBox("Do not use more than 2 modules!", "ShowModuleMaxWarning", InfoMessageType = InfoMessageType.Error)]
		[InfoBox("Dead End!", "ShowDeadEndWarning", InfoMessageType = InfoMessageType.Error)]
		[HideIf("HideTextInfoInspector")] public string[] textInfo;
		
		[HideIf("HideTextInfoInspector")]public Sprite imageInfo;

		[HideIf("HideInteractiveModuleInspector")] public InteractiveModule interactiveModule;
		
		[HideIf("HideTextChoiceInspector")] public List<TextChoice> choices;

		public bool overrideColor;
		[ShowIf("overrideColor")] public ColorType backgroundColor = ColorType.Black;
		[ShowIf("overrideColor")] public ColorType textColor = ColorType.White;

		public AudioClip music;
		public AudioClip[] audioOnOpen;
		
		public virtual GameScreenAsset NextScreen { get { return nextScreen; } }
		public virtual bool HideNextScreenInspector { get { return false; } }
		public virtual bool UseTextInfo { get { return textInfo != null && textInfo.Length > 0; } }
		public virtual bool HideTextInfoInspector { get { return false; } }
		public virtual string TextInfo { get { return textInfo[Mathf.Min(VisitsCount, textInfo.Length - 1)]; } }
		public virtual bool UseImageInfo { get { return imageInfo != null; } }
		public virtual bool HideImageInfoInspector { get { return false; } }
		public virtual Sprite ImageInfo { get { return imageInfo; } }
		public virtual bool UseTextChoice { get { return choices != null && choices.Count > 0; }}
		public virtual bool HideTextChoiceInspector { get { return false; }}
		public virtual List<TextChoice> Choices { get { return choices; } }
		public virtual bool UseInteractiveModule { get { return interactiveModule != null; } }
		public virtual bool HideInteractiveModuleInspector { get { return false; } }
		public virtual InteractiveModule InteractiveModule { get { return interactiveModule; } }

		public int VisitsCount { get; private set; }

		public int ModulesCount
		{
			get
			{
				var i = 0;

				if (UseTextInfo) i++;
				if (UseImageInfo) i++;
				if (UseTextChoice) i++;
				if (UseInteractiveModule) i++;

				return i;
			}
		}

		private bool ShowModuleMaxWarning
		{
			get { return ModulesCount > 2; }
		}
		
		private bool ShowModuleMinWarning
		{
			get { return ModulesCount == 0; }
		}
		
		private bool ShowDeadEndWarning
		{
			get { return NextScreen == null && !IsInteractive; }
		}

		public bool IsInteractive
		{
			get { return UseTextChoice || UseInteractiveModule; }
		}

		public void Visited()
		{
			VisitsCount++;
		}

		public virtual void Enter()
		{
			
		}

		public virtual void OnEnable()
		{
			VisitsCount = 0;
			
			if(ShowModuleMinWarning)
				Debug.LogError(string.Format("No module on: {0}", this), this);
			if(ShowModuleMaxWarning)
				Debug.LogError(string.Format("More then 2 modules on: {0}", this), this);
			if(ShowDeadEndWarning)
				Debug.LogError(string.Format("Dead end on: {0}", this), this);
		}
	}
}