﻿using UnityEditor;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;
using Sirenix.Utilities;
using UnityEngine;
using System.Collections.Generic;

public class VertexGradientEditor : OdinEditorWindow
{
    [MenuItem("Tools/VertexGradientEditor")]
    private static void OpenWindow()
    {
        var window = GetWindow<VertexGradientEditor>();

        // Nifty little trick to quickly position the window in the middle of the editor.
        window.position = GUIHelper.GetEditorWindowRect().AlignCenter(380, 200);
    }

    [MinMaxSlider(0, 1, true)]
    public Vector2 VertexGradientR;
    [MinMaxSlider(0, 1, true)]
    public Vector2 VertexGradientG;
    [MinMaxSlider(0, 1, true)]
    public Vector2 VertexGradientB;
    [MinMaxSlider(0, 1, true)]
    public Vector2 VertexGradientA;

    [HorizontalGroup("Split", 0.5f)]
    [Button(ButtonSizes.Large), GUIColor(0.4f, 0.8f, 1)]
    void BakeGradient()
    {
        var test = Selection.GetTransforms(SelectionMode.Deep);
        foreach (var t in test)
        {
            MeshFilter mf = t.GetComponent<MeshFilter>();
            if (mf != null)
            {
                Mesh m = (Mesh)Instantiate(mf.sharedMesh);
                //m.name = mf.sharedMesh.name + "VC";
                
                Vector3[] vertices = m.vertices;

                // create new colors array where the colors will be created.
                Color[] colors = new Color[vertices.Length];

                float highestVert = -9999;
                float lowestVert = 9999;

                for (int i = 0; i < vertices.Length; i++)
                {
                    if (vertices[i].y < lowestVert)
                    {
                        lowestVert = vertices[i].y;
                    }
                    if (vertices[i].y > highestVert)
                    {
                        highestVert = vertices[i].y;
                    }
                }

                for (int i = 0; i < vertices.Length; i++)
                {
                    if (m.colors != null && m.colors.Length > 1)
                    {
                        colors[i] = m.colors[i];
                    }
                    else
                    {
                        colors[i] = Color.white;
                    }
                    colors[i].r = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientR.x, VertexGradientR.y);
                    colors[i].g = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientG.x, VertexGradientG.y);
                    colors[i].b = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientB.x, VertexGradientB.y);
                    colors[i].a = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientA.x, VertexGradientA.y);
                }

                // assign the array of colors to the Mesh.
                m.colors = colors;
                mf.mesh = m;
            }
        }
    }

    [HorizontalGroup("Split", 0.5f)]
    [Button(ButtonSizes.Large), GUIColor(0.8f, 0.4f, 1)]
    public void SaveMesh()
    {
        Transform[] gos = Selection.activeTransform.GetComponentsInChildren<Transform>();

        foreach (Transform t in gos)
        {
            string savingString = "Assets/SavedModels/" + t.name + ".asset";
            if (t.gameObject.GetComponent<MeshFilter>())
            {

                try
                {
                    AssetDatabase.CreateAsset(t.gameObject.GetComponent<MeshFilter>().sharedMesh, savingString);
                }
                catch (UnityException e)
                {
                    Debug.Log(e);
                }
                AssetDatabase.SaveAssets();
            }
            else
            {
                Debug.Log("Mesh filter not found");
            }

        }
        AssetDatabase.Refresh();
    }


    public float DoRemap(float value, float from1, float to1, float from2, float to2)
    {
        return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
    }
}