using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.AI;
using System.IO;
using System.Text;
using System;

public class SceneToolFloor
{
    [MenuItem("SceneTool/SetFloorMessage")]//添加FloorMessage脚本绑定楼层信息
    public static void SetFloorMessage()
    {
        Transform neiParent = GameObject.Find("shinei").transform.GetChild(0);
        for (int i = 0; i < neiParent.childCount; i++)
        {
            //Debug.Log(neiParent.GetChild(i).name);
            if (!neiParent.GetChild(i).GetComponent<FloorMessage>())
            {
                neiParent.GetChild(i).gameObject.AddComponent<FloorMessage>();
            }
            FloorMessage msg = neiParent.GetChild(i).GetComponent<FloorMessage>();
            msg.targetTrans = GetTarget(neiParent.GetChild(i));
            msg.buildNum = neiParent.name;
            if (!neiParent.GetChild(i).name.ToLower().Contains("m"))
            {
                string floornum = neiParent.GetChild(i).name.Substring
                    (neiParent.GetChild(i).name.ToLower().IndexOf("nei") + 3/*, neiParent.GetChild(i).name.Length - 4*/);
               
                msg.floorNum = int.Parse(floornum);
                msg.interlayerNum = 0;
            }
            else
            {
                msg.floorNum = int.Parse(neiParent.GetChild(i).name.Substring
                    (neiParent.GetChild(i).name.ToLower().IndexOf("nei") + 3, neiParent.GetChild(i).name.ToLower().IndexOf("m")
                    - neiParent.GetChild(i).name.ToLower().IndexOf("nei") - 4));
                msg.interlayerNum = int.Parse(neiParent.GetChild(i).name.Substring
                    (neiParent.GetChild(i).name.ToLower().IndexOf("m") + 1, neiParent.GetChild(i).name.Length -
                    neiParent.GetChild(i).name.ToLower().IndexOf("m") - 1));
            }
        }
        Transform waiParent = GameObject.Find("shiwai").transform.GetChild(0);
        for (int i = 0; i < waiParent.childCount; i++)
        {
            //Debug.Log(waiParent.GetChild(i).name);
            if (!waiParent.GetChild(i).GetComponent<FloorMessage>())
            {
                waiParent.GetChild(i).gameObject.AddComponent<FloorMessage>();
            }
            FloorMessage msg = waiParent.GetChild(i).GetComponent<FloorMessage>();
            msg.buildNum = waiParent.name;
            if (!waiParent.GetChild(i).name.ToLower().Contains("m"))
            {
                string floornum = waiParent.GetChild(i).name.Substring
                    (waiParent.GetChild(i).name.ToLower().IndexOf("wai") + 3/*, waiParent.GetChild(i).name.Length - 4*/);
                msg.floorNum = int.Parse(floornum);
                msg.interlayerNum = 0;
            }
            else
            {
                msg.floorNum = int.Parse(waiParent.GetChild(i).name.Substring
                    (waiParent.GetChild(i).name.ToLower().IndexOf("wai") + 4, waiParent.GetChild(i).name.ToLower().IndexOf("m")
                    - waiParent.GetChild(i).name.ToLower().IndexOf("wai") - 4));
                msg.interlayerNum = int.Parse(waiParent.GetChild(i).name.Substring
                    (waiParent.GetChild(i).name.ToLower().IndexOf("m") + 1, waiParent.GetChild(i).name.Length -
                    waiParent.GetChild(i).name.ToLower().IndexOf("m") - 1));
            }
        }
    }
    static Transform GetTarget(Transform parent)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            if (parent.GetChild(i).name.ToLower().Contains("_floor")
                || parent.GetChild(i).name.ToLower().Contains("-floor")
                )
            {
                return parent.GetChild(i);
            }
        }

        return parent;
    }
    [MenuItem("SceneTool/SetNavLayershinei")]//添加寻路层(选中shinei对象设置,注:扶梯情况特殊没有包含)
    public static void SetNavLayer()
    {
        Transform[] obj = Selection.GetTransforms(SelectionMode.Deep);
        for (int i = 0; i < obj.Length; i++)
        {
            if (obj[i].name.ToLower().Contains("_floor") ||//地面
                obj[i].name.ToLower().Contains("_floor"))
            {
                Debug.Log(obj[i].name);
            //    obj[i].gameObject.layer = LayerMask.NameToLayer("SoldierRoad");
            //    if (!obj[i].gameObject.GetComponent<MeshCollider>())
            //    {
            //        obj[i].gameObject.AddComponent<MeshCollider>();
            //    }
            //    if (!obj[i].gameObject.GetComponent<PathFindable>())
            //    {
            //        obj[i].gameObject.AddComponent<PathFindable>();
            //    }
            //    if (!obj[i].gameObject.GetComponent<CloneGameObjInfo>())
            //    {
            //        obj[i].gameObject.AddComponent<CloneGameObjInfo>();
            //    }
            //    if (!obj[i].gameObject.GetComponent<CloneableEnums>())
            //    {
            //        obj[i].gameObject.AddComponent<CloneableEnums>();
            //    }
            //    CloneableEnums enumParent = obj[i].gameObject.GetComponent<CloneableEnums>();
            //    GameObject CarRoad = GameObject.Find("FloorNei");
            //    var CloneableEnums = CarRoad.GetComponent<CloneableEnums>();
            //    enumParent.CloneableTypes = CloneableEnums.CloneableTypes;
            //    if (obj[i].parent.GetComponent<FloorMessage>())
            //    {
            //        FloorMessage fmg = obj[i].parent.GetComponent<FloorMessage>();
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().gameObjType = CloneObjType.StaticGameObject;
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().buildNum = fmg.buildNum;
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().floorNum = fmg.floorNum;
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().interlayerNum = fmg.interlayerNum;
            //    }
            //    if (obj[i].parent.parent.GetComponent<FloorMessage>())
            //    {
            //        FloorMessage fmg = obj[i].parent.parent.GetComponent<FloorMessage>();
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().gameObjType = CloneObjType.StaticGameObject;
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().buildNum = fmg.buildNum;
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().floorNum = fmg.floorNum;
            //        obj[i].gameObject.GetComponent<CloneGameObjInfo>().interlayerNum = fmg.interlayerNum;
            //    }
            //}
            //if (obj[i].name.ToLower().Contains("_wall") ||//墙面
            //    obj[i].name.ToLower().Contains("_wall"))
            //{
            //    obj[i].gameObject.layer = LayerMask.NameToLayer("SoldierRoad");
            //    if (!obj[i].gameObject.GetComponent<NavMeshModifier>())
            //    {
            //        obj[i].gameObject.AddComponent<NavMeshModifier>();
            //    }
            //    NavMeshModifier mod = obj[i].gameObject.GetComponent<NavMeshModifier>();
            //    mod.overrideArea = true;
            //    mod.area = 1;
            //    if (!obj[i].gameObject.GetComponent<MeshCollider>())
            //    {
            //        obj[i].gameObject.AddComponent<MeshCollider>();
            //    }
            //}
            //if (obj[i].name.ToLower().Contains("lt"))//楼梯
            //{
            //    obj[i].gameObject.layer = LayerMask.NameToLayer("SoldierRoad");
            //    if (!obj[i].gameObject.GetComponent<MeshCollider>())
            //    {
            //        obj[i].gameObject.AddComponent<MeshCollider>();
            //    }
            //    if (!obj[i].gameObject.GetComponent<PathFindable>())
            //    {
            //        obj[i].gameObject.AddComponent<PathFindable>();
            //    }
            }
        }
    }
    [MenuItem("SceneTool/GetNavLayerCarRoad")]
    public static void GetNavLayerCarRoad()
    {
        Transform[] obj = Selection.GetTransforms(SelectionMode.Deep);
        {
            for (int i = 0; i < obj.Length; i++)
            {
                if (obj[i].gameObject.layer == LayerMask.NameToLayer("CarRoad"))
                {
                   // if (!obj[i].gameObject.GetComponent<NavMeshModifier>())
                    {
                        Debug.Log(obj[i].name);
                    }
                }
            }
        }
    }
    [MenuItem("SceneTool/ReadCloneTypes")]
    static void ReadCloneTypes()
    {
        string path = Application.dataPath + "/Floors.txt";
        string str = File.ReadAllText(path, Encoding.UTF8);
        string[] EnumStrList = str.Split(',');
        //List<CloneObjType> EnmuList = new List<CloneObjType>();
        //foreach (var item in EnumStrList)
        //{
        //    if (!string.IsNullOrEmpty(item))
        //    {
        //        CloneObjType type = (CloneObjType)Enum.Parse(typeof(CloneObjType), item);
        //        EnmuList.Add(type);
        //    }
        //}
        //GameObject CarRoad = GameObject.Find("FloorNei");
        //if (!CarRoad.GetComponent<CloneableEnums>())
        //{
        //    CarRoad.AddComponent<CloneableEnums>();
        //}
        //var CloneableEnums = CarRoad.GetComponent<CloneableEnums>();
        //CloneableEnums.CloneableTypes = EnmuList;
    }
    [MenuItem("SceneTool/AddColider")]
    static void AddColider()
    {
        Transform[] obj = Selection.GetTransforms(SelectionMode.Unfiltered);
        {
            Debug.Log(obj[0].name);
        }
    }
}