private static LogSelectorResult AddLogSelectorComponent(ChirpQuantumConsole chirpConsole)
        {
            if (chirpConsole == null)
            {
                return(LogSelectorResult.UnableToAdd);
            }

            SerializedObject   so = new SerializedObject(chirpConsole);
            SerializedProperty _consoleLogText      = so.FindProperty("_consoleLogText");
            TextMeshProUGUI    consoleTextContainer = _consoleLogText.objectReferenceValue as TextMeshProUGUI;

            bool createdNew = false;

            var allLogSelectorsFound = chirpConsole.GetComponentsInChildren <QuantumConsoleLogSelector>();
            QuantumConsoleLogSelector targetLogSelector = null;

            if (allLogSelectorsFound.Length > 1)
            {
                for (int i = 0; i < allLogSelectorsFound.Length; i++)
                {
                    if (Application.isPlaying)
                    {
                        Object.Destroy(allLogSelectorsFound[i]);
                    }
                    else
                    {
                        Object.DestroyImmediate(allLogSelectorsFound[i]);
                    }
                }
            }
            else if (allLogSelectorsFound.Length == 1)
            {
                targetLogSelector = allLogSelectorsFound[0];
            }
            else
            {
                // no log selector found, we need to create one
                ScrollRect    consoleTextScrollRect = consoleTextContainer.GetComponentInParent <ScrollRect>();
                RectTransform targetTransform       = consoleTextScrollRect.viewport;

                targetLogSelector = targetTransform.gameObject.AddComponent <QuantumConsoleLogSelector>();
                createdNew        = true;
            }

            if (targetLogSelector == null)
            {
                return(LogSelectorResult.UnableToAdd);
            }

            SerializedObject   targetSo             = new SerializedObject(targetLogSelector);
            SerializedProperty _textComponent       = targetSo.FindProperty("_textComponent");
            SerializedProperty _chirpQuantumConsole = targetSo.FindProperty("_chirpQuantumConsole");

            _textComponent.objectReferenceValue       = consoleTextContainer;
            _chirpQuantumConsole.objectReferenceValue = chirpConsole;

            targetSo.ApplyModifiedPropertiesWithoutUndo();

            return(createdNew ? LogSelectorResult.Success : LogSelectorResult.Updated);
        }
        private static ConsoleReplacementResult ReplaceQuantumConsole(QuantumConsole source, out ChirpQuantumConsole chirpQuantumConsole)
        {
            if (source is ChirpQuantumConsole sourceAsChirp)
            {
                chirpQuantumConsole = sourceAsChirp;
                return(ConsoleReplacementResult.AlreadyExists);
            }

            GameObject sourceGameObject = source.gameObject;

            Component[] allComponent   = sourceGameObject.GetComponents <Component>();
            int         componentCount = allComponent.Length;
            int         componentIndex = Array.IndexOf(allComponent, source);


            GameObject          temporaryGameObject     = new GameObject();
            ChirpQuantumConsole temporaryQuantumConsole = temporaryGameObject.AddComponent <ChirpQuantumConsole>();

            EditorUtility.CopySerialized(source, temporaryQuantumConsole);

            if (Application.isPlaying)
            {
                Object.Destroy(source);
            }
            else
            {
                Object.DestroyImmediate(source);
            }

            chirpQuantumConsole = sourceGameObject.AddComponent <ChirpQuantumConsole>();
            EditorUtility.CopySerialized(temporaryQuantumConsole, chirpQuantumConsole);

            var prefabStatus = PrefabUtility.GetPrefabInstanceStatus(sourceGameObject);

            if (prefabStatus == PrefabInstanceStatus.NotAPrefab)
            {
                if (componentIndex != -1 && componentCount - 1 > componentIndex)
                {
                    for (int i = componentCount - 1; i >= componentIndex; i--)
                    {
                        UnityEditorInternal.ComponentUtility.MoveComponentUp(chirpQuantumConsole);
                    }
                }
            }

            if (Application.isPlaying)
            {
                Object.Destroy(temporaryGameObject);
            }
            else
            {
                Object.DestroyImmediate(temporaryGameObject);
            }

            return(ConsoleReplacementResult.Success);
        }
        internal static ChirpQuantumConsole ConvertQuantumConsole(QuantumConsole source)
        {
            ChirpQuantumConsole chirpConsole = null;
            var allQuantumConsoleReferences  = FindAllReferences(source);
            var consoleReplacement           = ReplaceQuantumConsole(source, out chirpConsole);

            ReplaceAllReferences(allQuantumConsoleReferences, chirpConsole);
            var logSelector     = AddLogSelectorComponent(chirpConsole);
            var singletonUpdate = UpdateConsoleSingleton(chirpConsole);

            EditorUtility.DisplayDialog("Chirp: Convert Quantum Console",
                                        $"Quantum console update: {consoleReplacement},\nLog selector creation: {logSelector}\nConsole set to Singleton Mode: {singletonUpdate}", "Ok");
            return(chirpConsole);
        }