// Start is called before the first frame update
    void OnEnable()
    {
        if (soData == null)
        {
            soData = Resources.Load("ModelRecords") as SoModelRecords;
        }

        if (myBareMesh == null)
        {
            myBareMesh = new BareDaggerfallMeshStats();
        }

        if (FilteredModels == null)
        {
            FilteredModels = soData.record;
        }

        if (soData.LastClickedIndex >= FilteredModels.Count)
        {
            soData.LastClickedIndex = 0;
        }


        Texture2D txBlue = new Texture2D(1, 1);

        txBlue.SetPixel(0, 0, Color.blue);
        txBlue.Apply();
        gssSelected            = new GUIStyleState();
        gssSelected.background = txBlue;


        PreviewRenderInit();

        UpdateFilteredList();
    }
    /*
     * private void PrepMesh()
     * {
     *  int[] textureTable = new int[] { 119, 120, 122, 123, 124, 168 };
     *  uint _ModelId = (uint)ModelId;
     *
     *  DaggerfallUnity dfUnity = DaggerfallUnity.Instance;
     *
     *  DFBlock.RdbObject obj = new DFBlock.RdbObject();
     *
     *  Matrix4x4 modelMatrix = GetModelMatrix(obj);
     *
     *  // Get model data
     *  DaggerfallWorkshop.ModelData modelData = new ModelData();
     *  dfUnity.MeshReader.GetModelData(_ModelId, out modelData);
     * }
     */

    public static BareDaggerfallMeshStats GetBareMeshFromId(int ModelID)
    {
        BareDaggerfallMeshStats MeshDat = new BareDaggerfallMeshStats();

        // ---------------
        int[] textureTable = new int[] { 119, 120, 122, 123, 124, 168 };
        uint  _ModelId     = (uint)ModelID;

        DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

        DFBlock.RdbObject obj = new DFBlock.RdbObject();

        Matrix4x4 modelMatrix = GetModelMatrix(obj);

        // Get model data
        DaggerfallWorkshop.ModelData modelData = new ModelData();
        dfUnity.MeshReader.GetModelData(_ModelId, out modelData);
        // ---------------


        // DIVERT MESH FUNCTIONALITY HERE
        // Maybe model ID, or makestatic
        CachedMaterial[] cachedMaterials;
        int[]            textureKeys;
        //textureKeys = DungeonTextureTables.DefaultTextureTable;
        bool hasAnimations;

        MeshDat.mesh = dfUnity.MeshReader.GetMesh(
            dfUnity,
            (uint)ModelID,
            out cachedMaterials,
            out textureKeys,
            out hasAnimations,
            dfUnity.MeshReader.AddMeshTangents,
            dfUnity.MeshReader.AddMeshLightmapUVs);

        MeshDat.matrix       = modelMatrix;
        MeshDat.SubMeshIndex = modelData.SubMeshes.Length;      // May be only drawing one submesh

        MeshDat.mat   = new Material[cachedMaterials.Length];
        MeshDat.mProp = new MaterialPropertyBlock();
        for (int i = 0; i < cachedMaterials.Length; i++)
        {
            MeshDat.mProp.SetTexture(i, cachedMaterials[i].material.mainTexture);
            MeshDat.mat[i] = cachedMaterials[i].material;
        }
        //meshRenderer.sharedMaterials = GetMaterialArray(cachedMaterials);
        //dfMesh.SetDefaultTextures(textureKeys);


        //SetDefaultTextures(textureKeys);
        //MeshDat.mat = SetBaseMeshTextures(textureTable);

        return(MeshDat);
    }
    /// <summary>
    /// Called by PrintSelectedModelsToPreview . Actually draws the model on the window.
    /// </summary>
    /// <param name="bareMesh"></param>
    /// <param name="r"></param>
    /// <param name="gstyle"></param>
    public void DrawRenderPreview(BareDaggerfallMeshStats bareMesh, Rect r, GUIStyle gstyle)
    {
        if (previewRenderUtility == null)
        {
            previewRenderUtility = new PreviewRenderUtility();
        }

        if (bareMesh == null)
        {
            Debug.Log("No Mesh Found.");
            return;
        }

        if (previewRenderUtility.camera == null)
        {
            return;
        }

        previewRenderUtility.BeginPreview(r, gstyle);

        InputPreviewWindowControls(r);

        if (bareMesh.mesh != null)
        {
            for (int i = 0; i < bareMesh.SubMeshIndex; i++)
            {
                previewRenderUtility.DrawMesh(bareMesh.mesh, Vector3.zero, Quaternion.Euler(-30f, 0f, 0f) * Quaternion.Euler(0f, 60, 0f), bareMesh.mat[i], i, bareMesh.mProp);
            }
        }

        bool fog = false;

        Unsupported.SetRenderSettingsUseFogNoDirty(false);
        previewRenderUtility.camera.Render();
        Unsupported.SetRenderSettingsUseFogNoDirty(fog);
        Texture texture = previewRenderUtility.EndPreview();

        GUI.DrawTexture(r, texture);
    }
    /// <summary>
    /// Load the proper mesh of the last clicked selected item.
    /// </summary>
    /// <param name="NowClickedIndex"></param>
    void UpdateMeshLoaded(int NowClickedIndex)
    {
        if (soData.record.Count <= 0)
        {
            return;
        }
        if (FilteredModels.Count <= 0)
        {
            return;
        }
        if (selectedIndex.Count <= 0)
        {
            return;
        }

        //Debug.Log("Try to Check Mesh" + FilteredModels.Count.ToString() + " " + NowClickedIndex.ToString());

        if (NowClickedIndex == -1)
        {
            NowClickedIndex = soData.LastClickedIndex;
        }

        //Debug.Log("Try to Check Mesh" + FilteredModels[NowClickedIndex].ModelID.ToString());

        bool LikelyLostFocus   = (selectedIndex.Count == 1 && soData.LastClickedIndex == 0);
        bool ForceUpdate       = (NowClickedIndex == -1);
        bool ClickedOnSameItem = (NowClickedIndex == soData.LastClickedIndex);

        //  if ((!LikelyLostFocus && !ClickedOnSameItem) || ForceUpdate)
        {
            //Debug.Log("Loading Display Mesh ID: " + FilteredModels[NowClickedIndex].ModelID.ToString());
            myBareMesh = SpawnDfModel.GetBareMeshFromId(FilteredModels[NowClickedIndex].ModelID);
        }

        bNeedsRedraw            = true;
        soData.LastClickedIndex = NowClickedIndex;
        EditorUtility.SetDirty(this);
    }