using System.Collections;
using System.IO;
using MindPowerSdk;
using UnityEditor;
using UnityEngine;

namespace Assets.Sandbox.Scripts
{
    public class MonoKopSceneObjTest : MonoBehaviour
    {
        public string obj_file = @"D:\项目\海盗王\138原版一键端\海盗王Online\map\hell5.obj";

        public MonoKopTest monoKopTest;

        void Start()
        {
            CSceneObjSet sceneObjSet = new CSceneObjSet();
            sceneObjSet.LoadBin("Assets/Resources/sceneobjinfo.bin");

            SceneObjFile sceneObjFile = new SceneObjFile();
            sceneObjFile.Load(obj_file);

            for (int y = 0; y < sceneObjFile.FileHeader.SectionCntY; y++)
            {
                for (int x = 0; x < sceneObjFile.FileHeader.SectionCntX; x++)
                {
                    var list = sceneObjFile.objInfos[x, y];
                    if (list is null) continue;

                    foreach (var sceneObjInfo in list)
                    {
                        if (sceneObjInfo.GetTypeId() == 1)
                            continue;

                        int id = sceneObjInfo.GetID();

                        CSceneObjInfo modeInfo = sceneObjSet.Get(id);

                        Vector3 pos = new Vector3(sceneObjInfo.X / 100f, sceneObjInfo.HeightOff / 100f, sceneObjInfo.Y / 100f * -1f);
                        float mapHeight = monoKopTest.map.GetHeight(pos.x, (sceneObjInfo.Y / 100f));
                        pos.y += mapHeight;

                        //Quaternion rot = Quaternion.AngleAxis(sceneObjInfo.YawAngle, Vector3.up);
                        string modePath = "Assets/Resources/Model/Scene/" + Path.GetFileNameWithoutExtension(modeInfo.szDataName) + ".lmo.obj";
                        Object mode = AssetDatabase.LoadAssetAtPath<Object>(modePath);
                        if (mode)
                        {
                            GameObject goModel = (GameObject)GameObject.Instantiate(mode);
                            goModel.transform.position = pos;
                            goModel.transform.localScale = new Vector3(1, 1, -1);
                            Vector3 e = goModel.transform.eulerAngles;
                            goModel.transform.rotation = Quaternion.Euler(e.x, sceneObjInfo.YawAngle, e.z);
                            goModel.name = $"[{id}]{mode.name}";
                            // Debug.Log(goModel);
                        }
                        else
                        {
                            Debug.LogError($"mode not found: {modePath}");
                        }
                    }
                }
            }
        }
    }
}