using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

public static class Lay
{
    public static void Row(Action action)
    {
        GUILayout.BeginHorizontal();
        action();
        GUILayout.EndHorizontal();
    }

    public static void Row(GUIStyle style, Action action)
    {
        GUILayout.BeginHorizontal(style);
        action();
        GUILayout.EndHorizontal();
    }

    public static void Row(Action action, params GUILayoutOption[] options)
    {
        GUILayout.BeginHorizontal(options);
        action();
        GUILayout.EndHorizontal();
    }

    public static void RowFlexible(Action action)
    {
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        action();
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
    }

    public static void Column(Action action)
    {
        GUILayout.BeginVertical();
        action();
        GUILayout.EndVertical();
    }

    public static void Column(GUIStyle style, Action action)
    {
        GUILayout.BeginVertical(style);
        action();
        GUILayout.EndVertical();
    }

    public static void Column(Action action, params GUILayoutOption[] options)
    {
        GUILayout.BeginVertical(options);
        action();
        GUILayout.EndVertical();
    }

    public static void Image(Sprite sprite, int maxWidth, int maxHeight, Color? color = null)
    {
        if (sprite == null)
            return;
        Image(sprite.texture, maxWidth, maxHeight, color);
    }

    public static void Image(Texture texture, int maxWidth, int maxHeight, Color? color = null)
    {
        var tempColor = GUI.color;
        if (color != null)
            GUI.color = color.Value;
        GUILayout.Label(texture, GUILayout.MaxWidth(maxWidth), GUILayout.MaxHeight(maxHeight));
        GUI.color = tempColor;
    }

    public static bool TabToggle(bool active, GUIContent content, Action becomeActiveCallback = null, params GUILayoutOption[] options)
    {
        TryInitStyles();
        var wasActive = active;
        var nowActive = GUILayout.Toggle(active,
            content,
            STYLE_PAGE_TAB, 
            options);
        if (!wasActive && nowActive)
            becomeActiveCallback?.Invoke();
        return nowActive;
    }
    public static bool TabToggle(bool active, string content, Action becomeActiveCallback = null, params GUILayoutOption[] options)
    {
        return TabToggle(active, new GUIContent(content), becomeActiveCallback, options);
    }

    #region STYLES

    private static GUIStyle STYLE_PAGE_TAB;
    private static void TryInitStyles()
    {
        if (STYLE_PAGE_TAB != null)
            return;
#if UNITY_EDITOR
        STYLE_PAGE_TAB = new GUIStyle(EditorStyles.toolbarButton)
        {
            fixedHeight = 30
        };
#endif

        //place for other styles
    }

    #endregion
}
