using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 透明管理
/// </summary>
public class TransparentGlowManage : MonoBehaviour
{
    static TransparentGlowManage _inst;
    public static TransparentGlowManage Inst
    {
        get
        {
            if (_inst == null)
            {
                _inst = FindObjectOfType<TransparentGlowManage>();
                if (_inst == null)
                {
                    GameObject singletonObject = new GameObject();
                    _inst = singletonObject.AddComponent<TransparentGlowManage>();
                    singletonObject.name = typeof(TransparentGlowManage).ToString() + " (透明管理)";
                }
            }
            return _inst;
        }
    }

    public TransparentGlow[] transparentGlows;
    /// <summary>
    /// 可以透明机器
    /// </summary>
    public List<Transform> points = new List<Transform>();
    public ClickEvent[] clickEvents;
    /// <summary>
    /// 是否进入透明模式
    /// </summary>
    public bool is_transparency = false;

    public Vector3 MainCamera_pos;
    public Vector3 MainCamera_rot;

    private void Awake()
    {
        if (_inst != null && _inst != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            _inst = this;
            DontDestroyOnLoad(this.gameObject);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        transparentGlows = FindObjectsOfType<TransparentGlow>();

        foreach (var item in points)
        {
            ClickEvent clickEvent = item.GetComponent<ClickEvent>();
            BoxCollider boxCollider = item.GetComponent<BoxCollider>();
            CabinetInfor cabinetInfor = item.GetComponent<CabinetInfor>();

            if (item.gameObject.activeSelf)
            {
                if (!clickEvent)
                {
                    //加点击脚本,处理模型名字,new 设备编号管理
                    item.gameObject.AddComponent<ClickEvent>();
                }

                if (!boxCollider)
                {
                    var renderer = item.GetComponent<Renderer>();
                    if (renderer)
                    {
                        var bounds = renderer.bounds;
                        var a = item.gameObject.AddComponent<BoxCollider>();
                        a.isTrigger = false;
                        a.center = bounds.center - item.transform.position;
                        a.center = new Vector3(
                            a.center.x * AdjustColliderSize(10, 10, 10).x,
                            a.center.y * AdjustColliderSize(10, 10, 10).y,
                            a.center.z * AdjustColliderSize(10, 10, 10).z);
                        a.size = bounds.size;
                        a.size = new Vector3(
                            a.size.x * AdjustColliderSize(10, 10, 10).x,
                            a.size.y * AdjustColliderSize(10, 10, 10).y,
                            a.size.z * AdjustColliderSize(10, 10, 10).z);
                        a.transform.rotation = Quaternion.identity; //重置其旋转为默认值
                    }
                }

                if (!cabinetInfor)
                {
                    item.gameObject.AddComponent<CabinetInfor>();
                    cabinetInfor.cabinet.name = item.name;
                }
                else
                {
                    cabinetInfor.cabinet.name = item.name;
                }
            }
        }

        clickEvents = FindObjectsOfType<ClickEvent>();
        for (int i = 0; i < clickEvents.Length; i++)
        {
            if (clickEvents[i].Materials.Count >= 0)
            {
                for (int j = 0; j < clickEvents[i].Materials.Count; j++)
                {
                    break;
                }
            }
        }
    }

    /// <summary>
    /// 获取正确的缩放比例
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="z"></param>
    /// <returns></returns>
    private Vector3 AdjustColliderSize(float x, float y, float z)
    {
        Vector3 scaleRatio = new Vector3(1f / x, 1f / y, 1f / z);
        return scaleRatio;
    }

    // Update is called once per frame
    void Update()
    {
        if (!is_transparency)
        {
            MainCamera_pos = Camera.main.transform.localPosition;
            MainCamera_rot = Camera.main.transform.localEulerAngles;
        }


    }

    /// <summary>
    /// 透明全部物体
    /// </summary>
    [ContextMenu("透明全部物体")]
    public void transparencyALL(bool is_transparency = true)
    {
        for (int i = 0; i < transparentGlows.Length; i++)
        {
            transparentGlows[i].F1();
        }
        for (int i = 0; i < clickEvents.Length; i++)
        {
            clickEvents[i].isTransparentGlow = true;
        }
        this.is_transparency = is_transparency;
    }

    /// <summary>
    /// 恢复全部物体
    /// </summary>
    [ContextMenu("恢复全部物体")]
    public void renewALL(bool is_transparency = false)
    {
        for (int i = 0; i < transparentGlows.Length; i++)
        {
            transparentGlows[i].F2();
        }
        for (int i = 0; i < clickEvents.Length; i++)
        {
            clickEvents[i].isTransparentGlow = false;
        }
        this.is_transparency = is_transparency;
    }
}
