using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using GCGame;
using System;
using Module.Log;

[CustomEditor(typeof(NPCEditorRoot))]
public class NPCEditorRootEditor : Editor
{
    private NPCEditorRoot script;

    public bool placing = false;

    void OnEnable()
    {
        script = (NPCEditorRoot)target;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        //enter placement mode
        if (!placing && GUILayout.Button("Edit Mode: Off"))
        {
            Undo.RegisterCompleteObjectUndo(script, "Edit On");

            placing = true;
        }

        GUI.color = Color.yellow;

        //leave placement mode and try to combine submeshes
        if (placing && GUILayout.Button("Edit Mode: On"))
        {
            Undo.RegisterCompleteObjectUndo(script, "Edit Off");

            placing = false;
        }

        GUI.color = Color.white;
    }

    public void OnSceneGUI()
    {

        if (!placing)
            return;

        Event e = Event.current;
        //create a ray to get where we clicked in the scene view and pass in mouse position
        Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
        RaycastHit hitInfo;
        int layerMask = 1 << 12;
        if (Physics.Raycast(worldRay, out hitInfo, 1000, layerMask))
        {
            if (e.type == EventType.MouseUp && e.button == 0)
            {
                script.AddNPC(hitInfo.point);
            }
        }

    }
}