using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using LitJson;
using System.IO;

public class ImportMaterial : MonoBehaviour
{
    static int id = 0;
    static int tempId = 0;
    static string notImportMat;
    static string savePath = "Assets/Bundles/Materials/";
    static string loadPath = "Assets/GameResources/d3Assets/cloth/texture/";
    static string json;
    [MenuItem("Tools/�������")]
    public static void Import()
    {
        json = File.ReadAllText("Assets/GameResources/d3Assets/cloth/DressItemConfig.txt");
        Data data = JsonMapper.ToObject<Data>(json);
        for (int i = 0; i < data.list.Count; i++)
        {
            if (data.list[i].dressType != 1)
            {
                continue;
            }
            if (data.list[i]._id / 10000 == 8)
            {
                id = data.list[i]._id;
            }
            else if (data.list[i]._id / 100000 == 1)
            {
                if (id != data.list[i]._id / 100 * 100 + 1)
                {
                    id = data.list[i]._id / 100 * 100 + 1;
                }
            }
            else if (data.list[i]._id / 10000 == 31)
            {

                id = data.list[i]._id/10*10+1;
            }
            else
            {
                continue;
            }
            if (id == 0) continue;
            if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + "_" + data.list[i]._id + "_BaseMap.jpg"))
            {
                string fileName = id + "_" + data.list[i]._id;
                notImportMat += fileName + "_BaseMap\n" + fileName + "_MaskMap\n" + fileName + "_Normal\n";

                continue;
            }
            Texture2D baseMap = AssetDatabase.LoadAssetAtPath(loadPath + id + "_" + data.list[i]._id + "_BaseMap.jpg", typeof(Texture2D)) as Texture2D;
            Texture2D maskMap = AssetDatabase.LoadAssetAtPath(loadPath + id + "_" + data.list[i]._id + "_MaskMap.png", typeof(Texture2D)) as Texture2D;
            Texture2D normalMap = AssetDatabase.LoadAssetAtPath(loadPath + id + "_" + data.list[i]._id + "_Normal.png", typeof(Texture2D)) as Texture2D;
            Material material = new Material(Shader.Find("Universal Render Pipeline/Lit"));
            material.SetTexture("_BaseMap", baseMap);
            material.SetTexture("_DetailMask", maskMap);
            material.SetTexture("_DetailNormalMap", normalMap);

            AssetDatabase.CreateAsset(material, savePath + data.list[i]._id +"_mat"+ ".mat");

        }
        WriteTextFile("Assets/GameResources/d3Assets/cloth/NotImportMat.txt", notImportMat);
        // ˢ����Դ���ݿ�
        AssetDatabase.Refresh();
    }
    //public static void LoadTexture(string path,out Texture2D texture)
    //{
    //    Texture2D texture = new Texture2D(100,100);
    //    byte[] imageData = File.ReadAllBytes(path);
    //    texture.LoadImage(imageData);
    //    Debug.LogWarning(texture.LoadImage(imageData));
    //    //Debug.Log(texture.ToString());
    //}
    public static void WriteTextFile(string path, string text)
    {
        StreamWriter writer = new StreamWriter(path, false);
        writer.Write(text);
        writer.Close();

        Debug.Log("Text file written and saved at: " + path);
    }
    [MenuItem("Tools/��ʧ")]
    static void SaveName()
    {
        json = File.ReadAllText("Assets/GameResources/d3Assets/cloth/DressItemConfig.txt");
        Data data = JsonMapper.ToObject<Data>(json);
        for (int i = 0; i < data.list.Count; i++)
        {


            //if (data.list[i].dressType != 1)
            //{
            //    continue;
            //}
            if (data.list[i]._id / 10000 == 8)
            {
                id = data.list[i]._id;
            }
            else if (data.list[i]._id / 100000 == 1)
            {
                if (id != data.list[i]._id / 100 * 100 + 1)
                {
                    id = data.list[i]._id / 100 * 100 + 1;
                }
            }
            else if(data.list[i]._id / 10000 == 4)
            {
                id = data.list[i]._id;
            }
            else if (data.list[i].dressType == 3)
            {
                id = data.list[i]._id;
            }
            else if(data.list[i]._id / 10000 == 2)
            {
                continue;
            }
            else
            {
                id = data.list[i]._id;
            }

            switch (data.list[i].dressType)
            {
                case 1:
                    if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + "_" + data.list[i]._id + "_BaseMap.jpg"))
                    {
                        notImportMat += $"{id}\n";
                    }
                    break;
                case 2:
                    break;
                case 3:
                    if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + ".jpg"))
                    {
                        notImportMat += $"{id}\n";
                    }
                    break;
                case 4:
                    if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id +"L.jpg"))
                    {
                        notImportMat += $"{id}\n";
                    }
                    break;
                case 5:
                    if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + ".jpg"))
                    {
                        notImportMat += $"{id}\n";
                    }
                    break;
            }
        }
        WriteTextFile("Assets/GameResources/d3Assets/cloth/NotImportMat.txt", notImportMat);
        // ˢ����Դ���ݿ�
        AssetDatabase.Refresh();

    }


}


public class Data
{
    public List<DressItem> list;
}
public class DressItem
{
    public int _id { get; set; }

    public int dressType { get; set; }

}