using UnityEngine;

public class ComboBox2
{
    private static bool forceToUnShow;
    private static int useControlID = -1;
    public float height;
    public bool isClickedComboButton;
    public Vector2 scrollPos = new Vector2(0.0f, 0.0f);
    public int selectedItemIndex;
    private Vector2 scrollPosOld = new Vector2(0.0f, 0.0f);

    public int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle listStyle)
    {
        return List(rect, new GUIContent(buttonText), listContent, "button", "box", listStyle);
    }

    public int List(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle)
    {
        return List(rect, buttonContent, listContent, "button", "box", listStyle);
    }

    public int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle)
    {
        return List(rect, new GUIContent(buttonText), listContent, buttonStyle, boxStyle, listStyle);
    }

    public int List(Rect rect,
                    GUIContent buttonContent,
                    GUIContent[] listContent,
                    GUIStyle buttonStyle,
                    GUIStyle boxStyle,
                    GUIStyle listStyle)
    {
        if (forceToUnShow)
        {
            forceToUnShow = false;
            isClickedComboButton = false;
        }

        bool flag = false;
        int controlId = GUIUtility.GetControlID(FocusType.Passive);
        if (Event.current.GetTypeForControl(controlId) == EventType.MouseUp && isClickedComboButton && scrollPosOld == scrollPos)
        {
            flag = true;
        }

        if (GUI.Button(rect, buttonContent, buttonStyle))
        {
            if (useControlID == -1)
            {
                useControlID = controlId;
                isClickedComboButton = false;
            }

            if (useControlID != controlId)
            {
                forceToUnShow = true;
                useControlID = controlId;
            }

            isClickedComboButton = true;
        }

        if (isClickedComboButton)
        {
            Rect position = new Rect(rect.x, rect.y + GetPix(23), rect.width, listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
            if (position.y + (double)position.height > height)
            {
                position.height = (float)(height - (double)position.y - 2.0);
                position.width += 16f;
            }

            GUI.Box(position, "", boxStyle);
            if (Input.GetMouseButtonDown(0))
            {
                scrollPosOld = scrollPos;
            }

            Rect rect1 = new Rect(rect.x,
                                 rect.y + listStyle.CalcHeight(listContent[0], 1f),
                                 rect.width,
                                 listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
            scrollPos = GUI.BeginScrollView(position, scrollPos, rect1);
            int num = GUI.SelectionGrid(rect1, selectedItemIndex, listContent, 1, listStyle);
            if (num != selectedItemIndex)
            {
                selectedItemIndex = num;
            }

            GUI.EndScrollView();
        }

        if (flag)
        {
            isClickedComboButton = false;
        }

        return GetSelectedItemIndex();
    }

    public int GetSelectedItemIndex()
    {
        return selectedItemIndex;
    }

    private int GetPix(int i)
    {
        return (int)((1.0 + (Screen.width / 1280.0 - 1.0) * 0.600000023841858) * i);
    }
}