using System.Collections;
using UnityEngine;
public class ConversationManager : Singleton<ConversationManager > {
    protected ConversationManager() { }     //Garantuje nam da će biti samo singlton
    //Indikator da li je razgovor zapocet
    bool talking = false;

    //Trenutna linija razgovora koja ce biti prikazana
    ConversationEnrtz currentConversationLine;

    //Sirina slova u konverzaciji u px
    int fontSpacing = 7;

    //Sirina dialoga
    int conversationTextWidth;

    //Visina dialoga
    int dialogHeight = 70;

    //Prosotr rezervisan za sliku
    public int displayTextureOffset = 70;

    //Skaliran prvouganik za sliku
    Rect scaledTextureRect;

    public void StartConversation(Conversation conversation)
    {
        //Pocni konverzaciju
        if (!talking)
        {
            StartCoroutine(DisplayConversation(conversation));
        }
    }

    // korutina
    IEnumerator DisplayConversation(Conversation conversation)
    {
        talking = true;
       
      
      

        foreach (var conversationLine in conversation.ConversationLines)
        {
            // Za svaku liniju konverzacije postavlajamo pokazaivač na trenutni element lsite currentConversationLine. 
            currentConversationLine = conversationLine;
            // Uzimamo koliko je veliki tekst zbog crtanja na ekranu
            conversationTextWidth = currentConversationLine.ConversationText.Length * fontSpacing;
            scaledTextureRect = new Rect(
                currentConversationLine.DisplayPic.textureRect.x / currentConversationLine.DisplayPic.texture.width,
                currentConversationLine.DisplayPic.textureRect.y / currentConversationLine.DisplayPic.texture.height,
                currentConversationLine.DisplayPic.textureRect.width / currentConversationLine.DisplayPic.texture.width,
                currentConversationLine.DisplayPic.textureRect.height / currentConversationLine.DisplayPic.texture.height);
            // I čekamo 3 sekunde izmedju svako dela konverzacije
            yield return new WaitForSeconds(3);
        }
        talking = false;
    }

    void OnGUI()
    {
        if (talking)
        {
            //pocenti layout
            GUI.BeginGroup(new Rect(Screen.width / 2 - conversationTextWidth / 2, 50, conversationTextWidth + displayTextureOffset + 10, dialogHeight));

            // pozadinska kutija
            GUI.Box(new Rect(0, 0, conversationTextWidth + displayTextureOffset + 10, dialogHeight), "");

            //ime sagovornika
            GUI.Label(new Rect(displayTextureOffset, 10, conversationTextWidth + 30, 20), currentConversationLine.SpeakingCharacterName);

            // tekst
            GUI.Label(new Rect(displayTextureOffset, 30, conversationTextWidth + 30, 20), currentConversationLine.ConversationText);

            //Slkia sagovornika
            GUI.DrawTextureWithTexCoords(new Rect(10, 10, 50, 50), currentConversationLine.DisplayPic.texture, scaledTextureRect);

            //kraj layouta
            GUI.EndGroup(); 
        }
    }
}

