private void OnEnable()
    {
        defaultGUIColor = GUI.color;

        lableStyle                  = new GUIStyle();
        lableStyle.fontSize         = 13;
        lableStyle.fontStyle        = FontStyle.Bold;
        lableStyle.normal.textColor = new Color(0.2f, 0.2f, 0.2f);

        selectedLevelID = -1;

        levelsDatabase = EditorUtils.GetAsset <LevelsDatabase>();
        if (levelsDatabase != null)
        {
            levelDatabaseSerializedObject = new SerializedObject(levelsDatabase);

            InitItemsDatabase();

            levelsSerializedProperty = levelDatabaseSerializedObject.FindProperty(LEVELS_PROPERTY_NAME);

            pagination.Init(levelsSerializedProperty.arraySize);
        }
        else
        {
            Debug.Log("Levels db is null.");
        }

        EditorSceneManager.activeSceneChangedInEditMode += OnActiveSceneChanged;

        cameraTransform = Camera.main.transform;
    }
    public void OnGUI()
    {
        if (levelsDatabase == null)
        {
            EditorGUILayout.HelpBox("Levels Database can't be found.", MessageType.Error, true);

            if (GUILayout.Button("Create Levels Database"))
            {
                levelsDatabase = EditorUtils.CreateAsset <LevelsDatabase>(LEVELS_DATABASE_PATH + "Levels Database", true);

                OnEnable();
            }

            return;
        }


        int arrayList = levelsSerializedProperty.arraySize;

        if (arrayList == 0)
        {
            EditorGUILayout.BeginVertical(GUI.skin.box);
            EditorGUILayout.HelpBox("Levels Database is empty.", MessageType.Warning, true);
            EditorGUILayout.EndHorizontal();

            if (GUILayout.Button("Add Level"))
            {
                AddNewLevel();

                return;
            }

            return;
        }


        EditorGUILayout.BeginVertical(GUI.skin.box, GUILayout.Height(264));
        int paginationEnd = pagination.GetMaxElementNumber();

        for (int i = pagination.GetMinElementNumber(); i < paginationEnd; i++)
        {
            int  index           = i;
            bool isLevelSelected = selectedLevelID == i;

            if (isLevelSelected)
            {
                GUI.color = selectedColor;
            }

            Rect clickRect = (Rect)EditorGUILayout.BeginHorizontal(GUI.skin.box);

            EditorGUILayout.LabelField(levelsDatabase.Levels[i].EditorName + "   |   Items: " + (levelsDatabase.Levels[i].Items != null ? levelsDatabase.Levels[i].Items.Length : 0));

            GUILayout.FlexibleSpace();

            GUI.color = Color.grey;
            if (GUILayout.Button("=", EditorStyles.miniButton, GUILayout.Width(16), GUILayout.Height(16)))
            {
                GenericMenu menu = new GenericMenu();

                if (isLevelSelected)
                {
                    menu.AddItem(new GUIContent("Unselect"), false, delegate
                    {
                        Unselect();
                    });
                }
                else
                {
                    menu.AddItem(new GUIContent("Select"), false, delegate
                    {
                        Select(index);
                    });
                }

                menu.AddItem(new GUIContent("Remove"), false, delegate
                {
                    if (EditorUtility.DisplayDialog("Are you sure?", "This level will be removed!", "Remove", "Cancel"))
                    {
                        if (isLevelSelected)
                        {
                            Unselect();
                        }

                        levelsSerializedProperty.DeleteArrayElementAtIndex(index);

                        pagination.Init(levelsSerializedProperty.arraySize);

                        return;
                    }
                });

                menu.AddSeparator("");

                if (i > 0)
                {
                    menu.AddItem(new GUIContent("Move up"), false, delegate
                    {
                        levelsSerializedProperty.MoveArrayElement(index, index - 1);

                        if (isLevelSelected)
                        {
                            selectedLevelID -= 1;
                        }
                        else if (selectedLevelID == index - 1)
                        {
                            selectedLevelID = index;
                        }
                    });
                }
                else
                {
                    menu.AddDisabledItem(new GUIContent("Move up"));
                }

                if (i + 1 < arrayList)
                {
                    menu.AddItem(new GUIContent("Move down"), false, delegate
                    {
                        levelsSerializedProperty.MoveArrayElement(index, index + 1);

                        if (isLevelSelected)
                        {
                            selectedLevelID += 1;
                        }
                        else if (selectedLevelID == index + 1)
                        {
                            selectedLevelID = index;
                        }
                    });
                }
                else
                {
                    menu.AddDisabledItem(new GUIContent("Move down"));
                }

                menu.ShowAsContext();
            }

            GUI.color = Color.white;

            GUILayout.Space(5);

            if (GUI.Button(clickRect, GUIContent.none, GUIStyle.none))
            {
                if (isLevelSelected)
                {
                    Unselect();
                }
                else
                {
                    Select(i);
                }

                return;
            }

            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndHorizontal();

        pagination.DrawPagination();

        EditorGUILayout.Space();
        if (levelsDatabase == null)
        {
            EditorGUILayout.HelpBox("Level Database is not found.", MessageType.Error, true);

            if (GUILayout.Button("Create Levels Database"))
            {
                levelsDatabase = EditorUtils.GetAsset <LevelsDatabase>();

                return;
            }
        }
        else
        {
            UnityEngine.SceneManagement.Scene currentScene = EditorSceneManager.GetActiveScene();

            if (selectedLevelID != -1)
            {
                bool rightSceneSelected = currentScene.name == "LevelEditor";

                EditorGUILayout.PropertyField(levelNameProperty);

                GUILayout.Space(5);

                if (!rightSceneSelected)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.HelpBox("LevelEditor scene is required for editing.", MessageType.Warning);
                    if (GUILayout.Button("Open", GUILayout.Height(40f)))
                    {
                        EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
                        EditorSceneManager.OpenScene(SCENES_PATH + "LevelEditor.unity");

                        LoadObjects();

                        cameraTransform = Camera.main.transform;

                        cameraTransform.position = CameraController.STAGE_ONE_CAM_POS_EDITOR;
                        currentStageIsFirst      = true;
                    }
                    EditorGUILayout.EndHorizontal();

                    GUI.enabled = false;
                }

                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Objects management:", lableStyle);

                EditorGUILayout.BeginHorizontal();

                if (levelsDatabase.Levels[selectedLevelID].Items != null && levelsDatabase.Levels[selectedLevelID].Items.Length == 0)
                {
                    GUI.enabled = false;
                }

                GUI.color = redColor;
                if (GUILayout.Button("Clear Objects", GUILayout.MinWidth(80f), GUILayout.MaxWidth(90f), GUILayout.Height(30f)))
                {
                    if (EditorUtility.DisplayDialog("Are you sure?", "Level parts will be removed!", "Remove", "Cancel"))
                    {
                        ClearObjects();
                    }
                }
                GUI.color = defaultGUIColor;

                GUILayout.FlexibleSpace();



                if (GUILayout.Button("Load Objects", GUILayout.MinWidth(90f), GUILayout.MaxWidth(150f), GUILayout.Height(30f)))
                {
                    LoadObjects();
                }

                GUI.enabled = true;


                if (GUILayout.Button("Save Objects", GUILayout.MinWidth(90f), GUILayout.MaxWidth(150f), GUILayout.Height(30f)))
                {
                    SaveObjects();
                }

                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Camera:", lableStyle);

                EditorGUILayout.BeginHorizontal();


                if (currentStageIsFirst)
                {
                    GUI.color = selectedColor;
                }

                if (GUILayout.Button("Stage One Position", GUILayout.MinWidth(90f), GUILayout.MaxWidth(150f), GUILayout.Height(30f)))
                {
                    cameraTransform.position = CameraController.STAGE_ONE_CAM_POS_EDITOR;
                    currentStageIsFirst      = true;
                }

                GUI.color = defaultGUIColor;

                if (!currentStageIsFirst)
                {
                    GUI.color = selectedColor;
                }

                if (GUILayout.Button("Stage Two Position", GUILayout.MinWidth(90f), GUILayout.MaxWidth(150f), GUILayout.Height(30f)))
                {
                    cameraTransform.position = CameraController.STAGE_TWO_CAM_POS_EDITOR;
                    currentStageIsFirst      = false;
                }

                GUI.color = defaultGUIColor;

                EditorGUILayout.EndHorizontal();

                if (!rightSceneSelected)
                {
                    GUI.enabled = true;
                }
            }
            else
            {
                if (GUILayout.Button("Add Level", GUILayout.Height(40f)))
                {
                    AddNewLevel();

                    return;
                }
            }
        }

        levelDatabaseSerializedObject.ApplyModifiedProperties();
    }