﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;

public class SurfaceSpawnerWindow : EditorWindow
{
    [MenuItem("Arugula/Surface Spawner")]
    public static void Open()
    {
        SurfaceSpawnerWindow.GetWindow<SurfaceSpawnerWindow>().Show();
    }

    static SurfaceSpawnerPalette palette;
    static int selectedPaletteIndex;
    static int layer;
    static bool setParent;
    static bool random;

    Vector3 hitPoint;
    Vector3 hitNormal;
    bool validHit;

    private void OnEnable()
    {
        this.autoRepaintOnSceneChange = true;

        SceneView.onSceneGUIDelegate += OnSceneGUI;

    }

    private void OnDisable()
    {
        SceneView.onSceneGUIDelegate -= OnSceneGUI;
    }

    private void OnGUI()
    {

        palette = (SurfaceSpawnerPalette)EditorGUILayout.ObjectField(palette, typeof(SurfaceSpawnerPalette), false);

        layer = EditorGUILayout.LayerField("Layer", layer);
        setParent = EditorGUILayout.Toggle("Set Parent", setParent);
        random = EditorGUILayout.Toggle("Random", random);

        if (palette == null)
            return;


        List<string> names = new List<string>();
        foreach (var s in palette.spawnables)
        {
            names.Add(s.Name);
        }

        EditorGUI.BeginDisabledGroup(random);
        selectedPaletteIndex = GUILayout.SelectionGrid(selectedPaletteIndex, names.ToArray(), 4);
        EditorGUI.EndDisabledGroup();

    }

    private void Update()
    {
        SceneView.RepaintAll();
    }

    private void OnInspectorUpdate()
    {

    }

    void OnSceneGUI(SceneView sv)
    {
        var ev = Event.current;
        var mousePos = ev.mousePosition;
        mousePos.y = sv.position.height - mousePos.y;
        var ray = sv.camera.ScreenPointToRay(mousePos);

        var mask = new LayerMask();
        mask += 1 << layer;

        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(ray, out hit, 500, mask))
        {
            hitPoint = hit.point;
            hitNormal = hit.normal;
            validHit = true;
        }
        else
        {
            validHit = false;
        }

        if (validHit)
        {
            Handles.color = Color.red;
            Handles.DrawWireDisc(hit.point, hit.normal, 0.25f);
            Handles.DrawLine(hit.point, hitPoint + hit.normal);

            if (palette == null)
                return;

            if (ev.isKey && ev.type == EventType.KeyUp && ev.keyCode == KeyCode.Space)
            {
                ev.Use();

                var s = palette[random ? Random.Range(0, palette.spawnables.Length) : selectedPaletteIndex];
                if (s == null)
                    return;

                var go = s.Spawn(hit.point, hit.normal);
                if (go != null)
                {
                    if (setParent)
                    {
                        go.transform.parent = hit.transform;
                    }
                }
            }
        }

    }
}

