using UnityEngine;

using UnityEditor;
using System.Collections;

public class CreatePlane : ScriptableWizard
{
    public int horizontalCount = 1;
    public int verticalCount = 1;
    public string optionalName;
    public bool addCollider = false;
    public bool XYPlane = true;
   
    [MenuItem ("GameObject/Create Other/Custom Plane")]
    static void CreateWizard()
    {
        ScriptableWizard.DisplayWizard("Create Plane", typeof(CreatePlane));
    }
   
    void OnWizardCreate()
    {
        if (horizontalCount < 1 || verticalCount < 1)
        {
            Debug.LogError("Count must be greater than 0 for horizontal and vertical");
            return;
        }
       
        GameObject plane = new GameObject();
        Vector3 pos = plane.transform.position;
        //pos.x -= 0.5f;
        //pos.z -= 0.5f;
        plane.transform.position = pos;
       
        if (!string.IsNullOrEmpty(optionalName))
            plane.name = optionalName;
        else
            plane.name = "Plane";
           
            string meshPrefabPath = "Assets/Editor/" + plane.name + horizontalCount + "x" + verticalCount;
            string meshPrefabName = meshPrefabPath + ".asset";

        MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
        plane.AddComponent(typeof(MeshRenderer));

        Mesh m = (Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabName, typeof(Mesh));
               
        if (m == null)
        {
            m = new Mesh();
            m.name = plane.name;
       
            int numTriangles = horizontalCount * verticalCount * 2;
            int numVertices = numTriangles * 3;
       
            Vector3[] vertices = new Vector3[numVertices];
            Vector3[] normals = new Vector3[numVertices];
            Vector2[] uvs = new Vector2[numVertices];
            int[] triangles = new int[numVertices];
       
            for (int i = 0; i < numVertices; ++i)
            {
                triangles[i] = i;
                normals[i] = Vector3.one;
            }
               
            for (int i = 0; i < horizontalCount; ++i)
            {
                for (int j = 0; j < verticalCount; ++j)
                {
                    int current = (j + i*verticalCount)*6;
               
                    if(XYPlane)
                    {
                        Vector3 bottomLeft = new Vector3((float)i / horizontalCount, (float)j / verticalCount, 0);
                        Vector3 bottomRight = new Vector3((float)(i + 1) / horizontalCount, (float)j / verticalCount, 0);
                        Vector3 topLeft = new Vector3((float)i / horizontalCount, (float)(j + 1) / verticalCount, 0);
                        Vector3 topRight = new Vector3((float)(i + 1) / horizontalCount, (float)(j + 1) / verticalCount, 0);
                   
                        Vector2 bottomLeftV2 = new Vector2((float)i/horizontalCount, (float)j/verticalCount);
                        Vector2 bottomRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)j/verticalCount);
                        Vector2 topLeftV2 = new Vector2((float)i/horizontalCount, (float)(j+1)/verticalCount);
                        Vector2 topRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)(j+1)/verticalCount);
                        Debug.Log(bottomLeftV2 + "  " + topLeftV2);
                        vertices[current] = bottomLeft; //triangle 1
                        vertices[current + 1] = topLeft;
                        vertices[current + 2] = topRight;
                        vertices[current + 3] = topRight; //triangle 2
                        vertices[current + 4] = bottomRight;
                        vertices[current + 5] = bottomLeft;

                        uvs[current] = bottomLeftV2; //triangle 1
                        uvs[current + 1] = topLeftV2;
                        uvs[current + 2] = topRightV2;
                        uvs[current + 3] = topRightV2; //triangle 2
                        uvs[current + 4] = bottomRightV2;
                        uvs[current + 5] = bottomLeftV2;
                    }
                    else
                    {
                        
                        Vector3 bottomLeft = new Vector3((float)i / horizontalCount, 0, (float)j / verticalCount);
                        Vector3 bottomRight = new Vector3((float)(i + 1) / horizontalCount, 0, (float)j / verticalCount);
                        Vector3 topLeft = new Vector3((float)i / horizontalCount, 0, (float)(j + 1) / verticalCount);
                        Vector3 topRight = new Vector3((float)(i + 1) / horizontalCount, 0, (float)(j + 1) / verticalCount);

                        Vector2 bottomLeftV2 = new Vector2((float)i / horizontalCount, (float)j / verticalCount);
                        Vector2 bottomRightV2 = new Vector2((float)(i + 1) / horizontalCount, (float)j / verticalCount);
                        Vector2 topLeftV2 = new Vector2((float)i / horizontalCount, (float)(j + 1) / verticalCount);
                        Vector2 topRightV2 = new Vector2((float)(i + 1) / horizontalCount, (float)(j + 1) / verticalCount);
                        Debug.Log(bottomLeftV2 + "  " + topLeftV2);
                        vertices[current] = bottomLeft; //triangle 1
                        vertices[current + 1] = topLeft;
                        vertices[current + 2] = topRight;
                        vertices[current + 3] = topRight; //triangle 2
                        vertices[current + 4] = bottomRight;
                        vertices[current + 5] = bottomLeft;

                        uvs[current] = bottomLeftV2; //triangle 1
                        uvs[current + 1] = topLeftV2;
                        uvs[current + 2] = topRightV2;
                        uvs[current + 3] = topRightV2; //triangle 2
                        uvs[current + 4] = bottomRightV2;
                        uvs[current + 5] = bottomLeftV2;                        
                    }
                    
                }
            }
       
            m.vertices = vertices;
            m.normals = normals;
            m.uv = uvs;
            m.triangles = triangles;
           
            AssetDatabase.CreateAsset(m, meshPrefabName);
            AssetDatabase.SaveAssets();
        }
       
            meshFilter.sharedMesh = m;
       
        if (addCollider)
            plane.AddComponent(typeof(BoxCollider));
       
        Selection.activeObject = plane;
    }
}