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

public class GuiScrollChoice {

    Dictionary<int, string> m_choices = new Dictionary<int, string>();

    bool m_hasChosen;
    int m_chosenEntry;
    string m_caption;

    private Vector2 m_scrollPos = new Vector2(0, 0);


    public void NewChoiceMenuCaption( string caption )
    {
        m_choices.Clear();
        m_scrollPos = new Vector2(0, 0);
        m_hasChosen = false;
        m_chosenEntry = -1;
        m_caption = caption;

        Add(-1, "< Go Back >");
    }

    public void Add( int index, string name )
    {
        m_choices.Add(index, name);
    }

    public bool HasChosen( out int indexChosen )
    {
        indexChosen = m_chosenEntry;
        return m_hasChosen;
    }

    public void OnGUI()
    {
        if ( m_choices.Count > 0 )
        {
            int width = 250;
            int height = Screen.height / 2;
            const int ITEM_SPACING = 30;

            int totalHeight = m_choices.Count * ITEM_SPACING;

            {
                Rect scrArea = new Rect((Screen.width - width) / 2, (Screen.height - height) / 2, width, height);
                GUI.Box( scrArea, m_caption );
            }

            width -= 40;
            height -= 40;

            {
                Rect scrArea = new Rect((Screen.width - width) / 2, (Screen.height - height) / 2, width, height);
                m_scrollPos = GUI.BeginScrollView(scrArea, m_scrollPos, new Rect(0, 0, width - 20, totalHeight));

                int yPos = 0;

                foreach ( KeyValuePair<int, string> choice in m_choices )
                {
                    if (GUI.Button(new Rect(0, yPos, width - 30, 20), choice.Value))
                    {
                        m_hasChosen = true;
                        m_chosenEntry = choice.Key;
                    }
                    yPos += ITEM_SPACING;
                }
                // End the scroll view that we began above.
                GUI.EndScrollView();

            }
        }
    }

}
