using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
	public static GameManager GM;

	public Text testoNpc, testoPlayer;
	public Image leftSprite, rightSprite, mainSprite, mainSpriteR;
	public AudioSource suoneria;
	public AudioClip suoneriaClip;
	[HideInInspector] public List<SlotManager> connectedSlots = new List<SlotManager>();
	public SlotManager[] rSlots, lSlots;
	public JackManager[] rJacks, lJacks;

	[System.Serializable]
	public class Diago : System.Object
	{
		public int attore;
		public float delay;
		public bool stop;
		public bool leftIcon;
		public string testo;
	}

	public Sprite[] personaggi;
	public Diago[] dialoghi;

	private List<SlotManager> callingSlots = new List<SlotManager>();

	int ranJackL,ranJackR,ranSlotL,ranSlotR, currentDialogo;
	private bool noClick;

	private void Awake()
	{
		GM = this;
	}

	private void Start()
	{
		int h = 0;

		for (h = 0; h<lSlots.Length; h++)
		{
			lSlots[h].id = h;
			lSlots[h].tipo = 0;
		}

		for (h = 0; h<rSlots.Length; h++)
		{
			rSlots[h].id = h;
			rSlots[h].tipo = 1;
		}

		for (h = 0; h<lJacks.Length; h++)
		{
			lJacks[h].id = h;
			lJacks[h].tipo = 0;
		}

		for (h = 0; h<rJacks.Length; h++)
		{
			rJacks[h].id = h;
			rJacks[h].tipo = 1;
		}

		currentDialogo = 0;
		leftSprite.sprite = null;
		leftSprite.enabled = false;
		rightSprite.sprite = null;
		rightSprite.enabled = false;
		mainSprite.sprite = null;
		mainSprite.enabled = false;
		mainSpriteR.sprite = null;
		mainSpriteR.enabled = false;

		testoNpc.text = "";
		testoPlayer.text = "";

		Invoke("CallArrive", 1f);
	}

	public void SlotActive(SlotManager slot)
	{
		if (!connectedSlots.Contains(slot))
		{
			connectedSlots.Add(slot);
			if (connectedSlots.Count == 2)
			{
				suoneria.Stop();
				for (int jj = 0; jj<2; jj++)
				{
					connectedSlots[jj].UseSlot();
				}

				noClick = true;
				StartCoroutine("StartDialogo");
			}
		}
	}

	public void RemoveSlot(SlotManager slot)
	{
		if (connectedSlots.Contains(slot))
		{
			connectedSlots.Remove(slot);
			slot.NomoreRing();
			slot.OffSlot();

			if (connectedSlots.Count == 0)
			{
				Invoke("CallArrive", 1f);
			}
		}
	}

	private void CallArrive()
	{
		callingSlots.Clear();

		suoneria.clip = suoneriaClip;
		suoneria.Play();

		Random.InitState((int)Time.time*5000);

		int ranJackL = Random.Range(0, lJacks.Length);
		int ranJackR = Random.Range(0, rJacks.Length);
		int ranSlotL = Random.Range(0, lSlots.Length);
		int ranSlotR = Random.Range(0, rSlots.Length);

		lJacks[ranJackL].RingJack(ranSlotL);
		rJacks[ranJackR].RingJack(ranSlotR);
		lSlots[ranSlotL].RingSlot();
		rSlots[ranSlotR].RingSlot();
	}

	private IEnumerator StartDialogo()
	{
		while (currentDialogo < dialoghi.Length)
		{
			if (dialoghi[currentDialogo].attore <= 1)
			{
				rightSprite.sprite = null;
				rightSprite.enabled = false;
				leftSprite.sprite = null;
				rightSprite.enabled = false;

				if (dialoghi[currentDialogo].leftIcon)
				{
					mainSprite.sprite = personaggi[dialoghi[currentDialogo].attore];
					mainSprite.enabled = true;

					mainSpriteR.sprite = null;
					mainSpriteR.enabled = false;
				}
				else 
				{
					mainSpriteR.sprite = personaggi[dialoghi[currentDialogo].attore];
					mainSpriteR.enabled = true;

					mainSprite.sprite = null;
					mainSprite.enabled = false;
				}
					
				testoPlayer.text = dialoghi[currentDialogo].testo;
				testoNpc.text = "";
			}
			else
			{
				mainSprite.sprite = null;
				mainSprite.enabled = false;
				mainSpriteR.sprite = null;
				mainSpriteR.enabled = false;

				if (dialoghi[currentDialogo].leftIcon)
				{
					leftSprite.sprite = personaggi[dialoghi[currentDialogo].attore];
					leftSprite.enabled = true;
					rightSprite.sprite = null;
					rightSprite.enabled = false;
				}
				else
				{
					leftSprite.sprite = null;
					leftSprite.enabled = false;
					rightSprite.sprite = personaggi[dialoghi[currentDialogo].attore];
					rightSprite.enabled = true;
				}

				testoPlayer.text = "";
				testoNpc.text = dialoghi[currentDialogo].testo;
			}

			yield return new WaitForSeconds(dialoghi[currentDialogo].delay);


			if (!dialoghi[currentDialogo].stop)
			{
				currentDialogo++;
			}
			else
			{
				currentDialogo++;

				int h = 0;

				for (h = 0; h<lSlots.Length; h++)
				{
					lSlots[h].RemoveJack();
					lSlots[h].RemoveAll();
				}

				for (h = 0; h<rSlots.Length; h++)
				{
					rSlots[h].RemoveJack();
					rSlots[h].RemoveAll();
				}

				for (h = 0; h<lJacks.Length; h++)
				{
					lJacks[h].OffJack();
				}

				for (h = 0; h<rJacks.Length; h++)
				{
					rJacks[h].OffJack();
				}

				leftSprite.sprite = null;
				leftSprite.enabled = false;
				rightSprite.sprite = null;
				rightSprite.enabled = false;
				mainSprite.sprite = null;
				mainSprite.enabled = false;
				testoNpc.text = "";
				testoPlayer.text = "";

				yield return new WaitForEndOfFrame();

				break;
			}
		}

		yield return new WaitForEndOfFrame();
	}
}
