using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Collections;
using System.Collections.Generic;

public class AddChildAsset : MonoBehaviour
{
    public enum BUNDLETYPE
    {
        None,
        Model,
        Effect,
        Raw,
        Image,
    }

    [System.Serializable]
    public struct BundleAsset
    {
        public BUNDLETYPE type;
        public string assetName;
        public string componentType;
        public bool UnUseParentLayer;
    }

    [System.Serializable]
    public struct BundleAssetOBj
    {
        public GameObject assetObj;
        public string componentType;
        public bool UnUseParentLayer;
    }

    [System.Serializable]
    public struct ProfessionAssetOBj
    {
        public GameObject assetObj;
        public string componentType;
        public bool UnUseParentLayer;
        public Games.GlobeDefine.CharacterDefine.PROFESSION profession;
    }

    public BundleAssetOBj[] AllChildAssetRes;
    public BundleAsset[] AllChildAssetPath;
    public ProfessionAssetOBj[] _ProfessionAssetOBjs;

    public void LoadAsset()
    {
        if(AllChildAssetRes!=null)
        {
            for (int i = 0; i < AllChildAssetRes.Length; i++)
            {
                if (AllChildAssetRes[i].assetObj == null)
                    continue;
                GameObject newObj = GameObject.Instantiate(AllChildAssetRes[i].assetObj);
                if (newObj)
                    CloneChildAsset(newObj, AllChildAssetRes[i].componentType, AllChildAssetRes[i].UnUseParentLayer);
            }
        }

        if (_ProfessionAssetOBjs != null)
        {
            for (int i = 0; i < _ProfessionAssetOBjs.Length; i++)
            {
                if (_ProfessionAssetOBjs[i].assetObj == null)
                    continue;
                if(GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Profession == (int)_ProfessionAssetOBjs[i].profession)
                {
                    GameObject newObj = GameObject.Instantiate(_ProfessionAssetOBjs[i].assetObj);
                    if (newObj)
                        CloneChildAsset(newObj, _ProfessionAssetOBjs[i].componentType, _ProfessionAssetOBjs[i].UnUseParentLayer);
                }
            }
        }

        AllChildAssetRes = new BundleAssetOBj[0];
        if(AllChildAssetPath!=null)
        {
            for (int i = 0; i < AllChildAssetPath.Length; i++)
            {
                LoadChildAsset(AllChildAssetPath[i]);
            }
        }
        
        AllChildAssetPath = new BundleAsset[0];
    }

    void OnEnable()
    {
        LoadAsset();
    }

    void LoadChildAsset(BundleAsset asset)
    {
        if (string.IsNullOrEmpty(asset.assetName))
            return;
        Hashtable table = null;
        table = new Hashtable();
        table["componentType"] = asset.componentType;
        table["UnUseParentLayer"] = asset.UnUseParentLayer;
        switch (asset.type)
        {
            case BUNDLETYPE.Effect:
                {
                    LoadAssetBundle.Instance.LoadGameObject(LoadAssetBundle.BUNDLE_PATH_EFFECT, asset.assetName, LoadGameObjectCallback, table);
                }
                break;
            case BUNDLETYPE.Model:
                {
                    LoadAssetBundle.Instance.LoadGameObject(LoadAssetBundle.BUNDLE_PATH_MODEL, asset.assetName, LoadGameObjectCallback, table);
                }
                break;
            case BUNDLETYPE.Raw:
                {
                    RawImage raw = gameObject.GetComponent<RawImage>();
                    if(raw)
                    {
                        string imagePath = asset.assetName.Replace("\\", "/");
                        var name = Path.GetFileNameWithoutExtension(imagePath);
                        LoadAssetBundle.Instance.SetRawTexture(raw, name, imagePath);
                    }
                }
                break;
            case BUNDLETYPE.Image:
                {
                    Image image = gameObject.GetComponent<Image>();
                    if (image)
                    {
                        LoadAssetBundle.Instance.SetImageSprite(image, asset.assetName);
                    }
                }
                break;
        }
    }

    void LoadGameObjectCallback(string modelName, GameObject resObj, Hashtable hashParam)
    {
        GameObject newObj = GameObject.Instantiate(resObj);

        if (newObj)
        {
            string componentType = "";
            bool UnUseParentLayer = true;
            if(hashParam!=null)
            {
                if (hashParam.ContainsKey("componentType"))
                    componentType = (string)hashParam["componentType"];
                if (hashParam.ContainsKey("UnUseParentLayer"))
                    UnUseParentLayer = (bool)hashParam["UnUseParentLayer"];
            }

            CloneChildAsset(newObj, componentType, UnUseParentLayer);
        }
    }

    void CloneChildAsset(GameObject childObj,string componentType ,bool UnUseParentLayer)
    {
        childObj.SetActive(true);
        if (UnUseParentLayer == false)
        {
            childObj.transform.SetParent(transform);
            Transform[] childs = childObj.GetComponentsInChildren<Transform>();
            for (int i = 0; i < childs.Length; i++)
            {
                childs[i].gameObject.layer = gameObject.layer;
            }
        }
        
        childObj.transform.localEulerAngles = Vector3.zero;
        childObj.transform.localPosition = Vector3.zero;
        childObj.transform.localScale = Vector3.one;
        childObj.layer = gameObject.layer;
        
        if(string.IsNullOrEmpty(componentType)==false)
        {
            System.Type type = System.Type.GetType(componentType);
            Component component = childObj.GetComponent(type);
            if(component==null)
            {
                childObj.AddComponent(type);
            }
        }
    }
}