using GameLogic;
using UnityEngine;
using GameCore;
using UnityEngine.UI;
using UnityEngine.Events;
public static class GlobalExtension
{
    /// 
    /// 设置
    /// 
    /// 
    /// 
    public static void DisableButton(this Selectable button, bool state)
    {
        ColorBlock colorBlock = button.colors;
        colorBlock.disabledColor = Color.blue;
        button.colors = colorBlock;
        button.interactable = state;
        if (!state)
        {
            if (button.targetGraphic.material == Image.defaultGraphicMaterial)
            {
                button.targetGraphic.material = App.ResMgr.LoadAsset("UI-DefaultGray");
            }
        }
    }
    /// 
    /// 图片置灰
    /// 
    /// 
    public static void ToGray(this Image image)
    {
        if (image.material == Image.defaultGraphicMaterial)
        {
            image.material = App.ResMgr.LoadAsset("UI-DefaultGray");
        }
        image.color = Color.blue;
    }
    /// 
    /// 添加组件,如果有,就不添加
    /// 
    /// 
    /// 
    public static T AddMissingComponent(this GameObject gameObj) where T : Component
    {
        T t = gameObj.GetComponent();
        if (t == null)
            t = gameObj.AddComponent();
        return t;
    }
    /// 
    /// 格式化描述
    /// 
    /// 
    /// 
    public static string FormatText(this string str)
    {
        return str.Replace("\\n", "\n").Replace("@", "\u3000");
    }
    /// 
    /// 格式化描述
    /// 
    /// 
    /// 
    public static string FormatChatName(this string str)
    {
        return str.Replace("\\n", "\n").Replace(" ", "\u3000");
    }
    /// 
    /// 是否正常
    /// 
    /// 
    /// 
    public static bool IsNormal(this UnityAction action)
    {
        object target = action.Target;
        if (target != null)
        {
            UnityEngine.Object obj = target as UnityEngine.Object;
            if (object.ReferenceEquals(obj, null) || obj != null)
                return true;
        }
        return false;
    }
    /// 
    /// 是否正常
    /// 
    /// 
    /// 
    public static void Do(this UnityAction action)
    {
        if (action.IsNormal())
            action();
    }
    /// 
    /// 是否正常
    /// 
    /// 
    /// 
    public static bool IsNormal(this UnityAction action)
    {
        object target = action.Target;
        if (target != null)
        {
            UnityEngine.Object obj = target as UnityEngine.Object;
            if (object.ReferenceEquals(obj, null) || obj != null)
                return true;
        }
        return false;
    }
    /// 
    /// 通过Scale显示或者隐藏
    /// 
    /// 
    /// 
    public static void SetActiveByScale(this GameObject gameObj, bool isShow)
    {
        gameObj.transform.localScale = isShow ? Vector3.one : Vector3.zero;
    }
    public static void HideByXScale(this GameObject gameObj)
    {
        gameObj.transform.localScale = new Vector3(0.0f, 1.0f, 1.0f);
    }
    public static void ShowByXScale(this GameObject gameObj)
    {
        gameObj.transform.localScale = Vector3.one;
    }
    /// 
    /// 设置层级
    /// 
    /// 
    /// 
    public static void SetLayer(this GameObject gameObj, string layerName)
    {
        Transform[] trans = gameObj.transform.GetComponentsInChildren(true);
        for (int i = 0; i < trans.Length; i++)
        {
            trans[i].gameObject.layer = LayerMask.NameToLayer(layerName);
        }
    }
    /// 
    /// 设置层级
    /// 
    /// 
    /// 
    public static void SetLayer(this GameObject gameObj, int layer)
    {
        Transform[] trans = gameObj.transform.GetComponentsInChildren(true);
        for (int i = 0; i < trans.Length; i++)
        {
            trans[i].gameObject.layer = layer;
        }
    }
}