using Thousandto.Core.Base;
using System;
using UnityEngine;
using Thousandto.Plugins.LuaType;
using UnityEngine.Gonbest.MagicCube;

public class LuaUnityUtility {

    //设置父物体
    public static void SetParent(Transform child, Transform parent)
    {
        child.SetParent(parent);
    }

    //设置父物体并重置Transform
    public static void SetParentAndReset(Transform child, Transform parent)
    {
        child.SetParent(parent);
        ResetTransform(child);
    }

    //重置Transform
    public static void ResetTransform(Transform trans)
    {
        trans.localPosition = Vector3.zero;
        trans.localEulerAngles = Vector3.zero;
        trans.localScale = Vector3.one;
    }
    //增加 LuaBehaviour 组件
    public static LuaBehaviour RequireLuaBehaviour(Transform tf)
    {
        var _componet = tf.GetComponent<LuaBehaviour>();
        if (_componet == null)
        {
            return tf.gameObject.AddComponent<LuaBehaviour>();
        }
        return _componet;
    }
    //根据类型名增加组件(strType:空间名+类名)
    public static object RequireComponent(Transform tf, string strType)
    {
        var type = AssemblyUtils.FindType(strType);
        if (type != null)
        {
            var _componet = tf.GetComponent(type);
            if (_componet == null)
            {
                return tf.gameObject.AddComponent(type);
            }
            return _componet;
        }
        return null;
    }
    //获取子节点的第一个组件
    public static object GetComponentInChildren( Transform tf, string strType ) {
        return tf.GetComponentInChildren(AssemblyUtils.FindType(strType));
    }
    public static bool USE_NEW_CFG()
    {
#if USE_NEW_CFG
            return true;
#else
        return false;
#endif
    }

    public static bool UNITY_EDITOR()
    {
#if UNITY_EDITOR
        return true;
#else
        return false;
#endif
    }

    public static bool UNITY_IOS()
    {
#if UNITY_IOS
        return true;
#else
        return false;
#endif
    }

    public static bool UNITY_ANDROID()
    {
#if UNITY_ANDROID
        return true;
#else
        return false;
#endif
    }

    public static bool UNITY_WINDOWS()
    {
#if UNITY_STANDALONE_WIN
        return true;
#else
        return false;
#endif
    }

    public static bool UNITY_LAUNCHER()
    {
#if FUNCELL_LAUNCHER
        return true;
#else
        return false;
#endif
    }

    //是否使用KeyCode
    public static bool IsUseKeyCode()
    {
#if USEKEYCODE
        return true;
#else
        return false;
#endif
    }

    //是否使用pc模式
    public static bool IsUseUsePCMOdel()
    {
#if USE_PC_MODEL
        return true;
#else
        return false;
#endif
    }

    //获取子节点的components
    public static object GetComponentsInChildren( Transform tf, string compType, bool unityEngine = false )
    {
        Type type = Type.GetType( compType );
        if( unityEngine )
        {
            var assembly = System.Reflection.Assembly.Load( "UnityEngine" );
            if( assembly != null )
            {
                type = assembly.GetType( compType );
            }
        }
        return tf.GetComponentsInChildren( type );
    }

    //获取类型
    public static string GetType(object obj)
    {
        return obj.GetType().ToString();
    }
    //object类型转int类型,枚举类型转int可以用此函数
    public static int GetObjct2Int(object obj)
    {
        return (int)obj;
    }
    //object类型转int类型,枚举类型转byte可以用此函数
    public static byte GetObjct2Byte(object obj)
    {
        return (byte)obj;
    }
    //设置Transform的localPosition
    public static void SetLocalPosition(Transform t, float x, float y, float z)
    {
        t.localPosition = new Vector3(x, y, z);
    }

    //设置Transform的localPosition.x
    public static void SetLocalPositionX(Transform t, float x)
    {
        t.localPosition = new Vector3(x, t.localPosition.y, t.localPosition.z);
    }

    //设置Transform的localPosition.y
    public static void SetLocalPositionY(Transform t, float y)
    {
        t.localPosition = new Vector3(t.localPosition.x, y, t.localPosition.z);
    }

    //设置Transform的localPosition.z
    public static void SetLocalPositionZ(Transform t, float z)
    {
        t.localPosition = new Vector3(t.localPosition.x, t.localPosition.y, z);
    }

    //设置Transform的LocalRotation
    public static void SetLocalRotation(Transform t, float x, float y, float z, float w)
    {
        t.localRotation = new Quaternion(x, y, z, w);
    }
    //设置Transform的LocalEulerAngles
    public static void SetLocalEulerAngles(Transform t, float x, float y, float z)
    {
        t.localEulerAngles = new Vector3(x, y, z);
    }
    //设置Transform的localScale
    public static void SetLocalScale(Transform t, float x, float y, float z)
    {
        if (t == null)
            return;
        t.localScale = new Vector3(x, y, z);
    }
    //设置Transform的position
    public static void SetPosition(Transform t, float x, float y, float z)
    {
        t.position = new Vector3(x, y, z);
    }
    //设置Transform的rotation
    public static void SetRotation(Transform t, float x, float y, float z, float w)
    {
        t.rotation = new Quaternion(x, y, z, w);
    }

    //设置Transform的AulerAngles
    public static void SetAulerAngles(Transform t, float x, float y, float z)
    {
        t.eulerAngles = new Vector3(x, y, z);
    }
    //设置Transform的forward
    public static void SetForward(Transform t, float x, float y, float z)
    {
        t.forward = new Vector3(x, y, z);
    }
    //设置Transform的up
    public static void SetUp(Transform t, float x, float y, float z)
    {
        t.up = new Vector3(x, y, z);
    }
    //设置Transform的right
    public static void SetRight(Transform t, float x, float y, float z)
    {
        t.right = new Vector3(x, y, z);
    }

    //设置TweenPosition的from
    public static void SetTweenPositionFrom(TweenPosition t, float x, float y, float z)
    {
        t.from = new Vector3(x, y, z);
    }

    //设置TweenPosition的to
    public static void SetTweenPositionTo(TweenPosition t, float x, float y, float z)
    {
        t.to = new Vector3(x, y, z);
    }

    // 调用UIScrollView.ResetPosition()
    public static void ScrollResetPosition( Transform trans )
    {
        trans.GetComponent<UIScrollView>().ResetPosition();
    }

    // 调用UIGrid.Reposition()
    public static void GridResetPosition( Transform trans )
    {
        trans.GetComponent<UIGrid>().Reposition();
    }

    //设置层
    public static void SetLayer(Transform trans, int layer, bool recursive = true)
    {
        UnityUtils.SetLayer(trans, layer, recursive);
    }

    // 获得目标位置到UICamera显示位置
    public static Vector3 WorldPosToHudPos(Camera mainCamera, Camera uiCamera, Vector3 worldPos)
    {
        Vector3 hudPos = new Vector3(-Screen.width * 100.0f, -Screen.height * 100.0f, 0.0f);
        if (mainCamera == null || uiCamera == null)
        {
            return hudPos;
        }
        Vector3 viewportPos = mainCamera.WorldToViewportPoint(worldPos);
        if (viewportPos.x >= 0 && viewportPos.x <= 1
            && viewportPos.y >= 0 && viewportPos.y <= 1
            && viewportPos.z > mainCamera.nearClipPlane)
        {
            Vector3 tempPos = uiCamera.ViewportToWorldPoint(viewportPos);
            hudPos.x = tempPos.x;
            hudPos.y = tempPos.y;
            hudPos.z = 0.0f;
        }
        return hudPos;
    }
}