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

namespace Game
{
	[CreateAssetMenu(menuName = "Game Screens/Corridor Screen")]
	public class CorridorScreenAsset : GameScreenAsset
	{
		[Header("Rooms")]
		[InfoBox("Wrong items count", InfoMessageType.Error, "DifferentCountWarning")]
		[SerializeField] private List<GameScreenAsset> rooms;
		[SerializeField] private string[] downstairsLabels;
		[SerializeField] private string[] upstairsLabels;
		[SerializeField] private string upstairsLabel;
		[SerializeField] private string downstairsLabel;
		[SerializeField] private string goUpstairsLabel;
		[SerializeField] private string goDownStairsLabel;
		[SerializeField] private TextChoice bedroomChoice;
		[SerializeField] private VisitedScreensManager visitedScreensManager;
		[SerializeField] private AudioClip disturbingMusic;
		
		private bool upstairs;
		private List<TextChoice> downstairsChoices;
		private List<TextChoice> upstairsChoices;
		
		// ReSharper disable once UnusedMember.Local
		private bool DifferentCountWarning
		{
			get
			{
				return rooms.Count != 6 || downstairsLabels.Length != 3 || upstairsLabels.Length != 3;
			}
		}
		
		public override bool UseTextChoice
		{
			get { return true; }
		}

		public override bool UseTextInfo
		{
			get { return true; }
		}

		public override string TextInfo
		{
			get
			{
				if (upstairs)
					return upstairsLabel;
				return downstairsLabel;
			}
		}

		public override List<TextChoice> Choices
		{
			get
			{
				if (upstairs)
					return upstairsChoices;
				
				return downstairsChoices;
			}
		}

		public override bool HideTextInfoInspector { get { return true; } }
		public override bool HideTextChoiceInspector { get { return true; } }
		public override bool HideInteractiveModuleInspector { get { return true; } }

		public override void OnEnable()
		{
			base.OnEnable();
			
			upstairs = false;
			downstairsChoices = new List<TextChoice>();
			upstairsChoices = new List<TextChoice>();
			rooms.Shuffle();
			
			for (int i = 0; i < rooms.Count; i++)
			{
				TextChoice textChoice;
				if (i < 3)
				{
					textChoice = new TextChoice(downstairsLabels[i], rooms[i]);
					downstairsChoices.Add(textChoice);
				}
				else
				{
					textChoice = new TextChoice(upstairsLabels[i - 3], rooms[i]);
					upstairsChoices.Add(textChoice);
				}

				textChoice.Selected += RemoveChoice;
			}
			
			var goUpstairsChoice = new TextChoice(goUpstairsLabel, this);
			goUpstairsChoice.Selected += MoveUpstairs;
			downstairsChoices.Add(goUpstairsChoice);

			var goDownstairsChoice = new TextChoice(goDownStairsLabel, this);
			goDownstairsChoice.Selected += MoveDownstairs;
			upstairsChoices.Add(goDownstairsChoice);
		}

		private void RemoveChoice(TextChoice textChoice)
		{
			if (upstairsChoices.Contains(textChoice))
				upstairsChoices.Remove(textChoice);
			
			if (downstairsChoices.Contains(textChoice))
				downstairsChoices.Remove(textChoice);
		}
		
		private void MoveUpstairs(TextChoice textChoice)
		{
			upstairs = true;
		}

		private void MoveDownstairs(TextChoice textChoice)
		{
			upstairs = false;
		}

		public override void Enter()
		{
			base.Enter();

			if (visitedScreensManager.CanUnlockDisturbingMusic() && music != disturbingMusic)
				music = disturbingMusic;
			
			if ((!upstairsChoices.Contains(bedroomChoice) || !upstairsChoices.Contains(bedroomChoice)) &&
			    visitedScreensManager.CanUnlockBedroom())
			{
				upstairsChoices.Add(bedroomChoice);
				downstairsChoices.Add(bedroomChoice);
			}
		}
	}
}