<commit_before>﻿namespace Macabresoft.Macabre2D.Editor.UI.Controls.ValueEditors {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Avalonia;
    using Avalonia.Markup.Xaml;
    using Avalonia.Threading;
    using Macabresoft.Core;
    using Macabresoft.Macabre2D.Editor.Library.Models;
    using Macabresoft.Macabre2D.Editor.Library.Services;
    using Macabresoft.Macabre2D.Framework;

    public class ColliderEditor : ValueEditorControl<Collider> {
        public static readonly DirectProperty<ColliderEditor, IReadOnlyCollection<Type>> DerivedTypesProperty =
            AvaloniaProperty.RegisterDirect<ColliderEditor, IReadOnlyCollection<Type>>(
                nameof(DerivedTypes),
                editor => editor.DerivedTypes);

        public static readonly DirectProperty<ColliderEditor, Type> SelectedTypeProperty =
            AvaloniaProperty.RegisterDirect<ColliderEditor, Type>(
                nameof(SelectedType),
                editor => editor.SelectedType,
                (editor, value) => editor.SelectedType = value);

        private readonly IAssemblyService _assemblyService = Resolver.Resolve<IAssemblyService>();

        private readonly HashSet<IValueEditor> _childEditors = new();
        private readonly ObservableCollectionExtended<Type> _derivedTypes = new();
        private readonly IValueEditorService _valueEditorService = Resolver.Resolve<IValueEditorService>();

        private ValueEditorCollection _editorCollection;
        private Type _selectedType;

        public ColliderEditor() {
            this.InitializeComponent();
        }

        public IReadOnlyCollection<Type> DerivedTypes => this._derivedTypes;

        public Type SelectedType {
            get => this._selectedType;
            set {
                this.SetAndRaise(SelectedTypeProperty, ref this._selectedType, value);
                Dispatcher.UIThread.Post(() => {
                    if (value != null) {
                        this.SetEditorValue(this.Value, Activator.CreateInstance(value) as Collider);
                    }
                    else {
                        this.SetEditorValue(this.Value, null);
                    }
                });
            }
        }

        public override void Initialize(object value, Type valueType, string valuePropertyName, string title, object owner) {
            base.Initialize(value, valueType, valuePropertyName, title, owner);

            var types = this._assemblyService.LoadTypes(typeof(Collider));
            this._derivedTypes.Reset(types);
            this._derivedTypes.Remove(typeof(PolygonCollider));

            if (this.Value != null) {
                this.SetAndRaise(SelectedTypeProperty, ref this._selectedType, this.Value.GetType());
                this.ResetColliderEditors();
            }
        }

        protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change) {
            base.OnPropertyChanged(change);

            if (change.Property.Name == nameof(IValueEditor.Collection)) {
                this.ResetColliderEditors();
            }
        }

        protected override void OnValueChanged(Collider updatedValue) {
            base.OnValueChanged(updatedValue);
            this.ResetColliderEditors();
        }

        private void ClearEditors() {
            if (this._editorCollection != null) {
                this._editorCollection.OwnedValueChanged -= this.ColliderEditor_ValueChanged;
            }

            if (this._childEditors.Any()) {
                this.Collection.RemoveEditors(this._childEditors);
                this._childEditors.Clear();
                this._valueEditorService.ReturnEditors(this._editorCollection);
                this._editorCollection = null;
            }
        }

        private void ColliderEditor_ValueChanged(object sender, ValueChangedEventArgs<object> e) {
            this.RaiseValueChanged(sender, e);
        }

        private void InitializeComponent() {
            AvaloniaXamlLoader.Load(this);
        }

        private void ResetColliderEditors() {
            this.ClearEditors();

            if (this.Value is Collider value && this._valueEditorService != null) {<commit_msg>Fixed collider editor on reselection.<commit_after>﻿namespace Macabresoft.Macabre2D.Editor.UI.Controls.ValueEditors {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Avalonia;
    using Avalonia.Markup.Xaml;
    using Avalonia.Threading;
    using Macabresoft.Core;
    using Macabresoft.Macabre2D.Editor.Library.Models;
    using Macabresoft.Macabre2D.Editor.Library.Services;
    using Macabresoft.Macabre2D.Framework;

    public class ColliderEditor : ValueEditorControl<Collider> {
        public static readonly DirectProperty<ColliderEditor, IReadOnlyCollection<Type>> DerivedTypesProperty =
            AvaloniaProperty.RegisterDirect<ColliderEditor, IReadOnlyCollection<Type>>(
                nameof(DerivedTypes),
                editor => editor.DerivedTypes);

        public static readonly DirectProperty<ColliderEditor, Type> SelectedTypeProperty =
            AvaloniaProperty.RegisterDirect<ColliderEditor, Type>(
                nameof(SelectedType),
                editor => editor.SelectedType,
                (editor, value) => editor.SelectedType = value);

        private readonly IAssemblyService _assemblyService = Resolver.Resolve<IAssemblyService>();

        private readonly HashSet<IValueEditor> _childEditors = new();
        private readonly ObservableCollectionExtended<Type> _derivedTypes = new();
        private readonly IValueEditorService _valueEditorService = Resolver.Resolve<IValueEditorService>();

        private ValueEditorCollection _editorCollection;
        private Type _selectedType;

        public ColliderEditor() {
            this.InitializeComponent();
        }

        public IReadOnlyCollection<Type> DerivedTypes => this._derivedTypes;

        public Type SelectedType {
            get => this._selectedType;
            set {
                this.SetAndRaise(SelectedTypeProperty, ref this._selectedType, value);
                Dispatcher.UIThread.Post(() => {
                    if (value != null) {
                        this.SetEditorValue(this.Value, Activator.CreateInstance(value) as Collider);
                    }
                    else {
                        this.SetEditorValue(this.Value, null);
                    }
                });
            }
        }

        public override void Initialize(object value, Type valueType, string valuePropertyName, string title, object owner) {
            var types = this._assemblyService.LoadTypes(typeof(Collider));
            this._derivedTypes.Reset(types);
            this._derivedTypes.Remove(typeof(PolygonCollider));

            base.Initialize(value, valueType, valuePropertyName, title, owner);

            this.ResetColliderEditors();
        }

        protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change) {
            base.OnPropertyChanged(change);

            if (change.Property.Name == nameof(IValueEditor.Collection)) {
                this.ResetColliderEditors();
            }
        }

        protected override void OnValueChanged(Collider updatedValue) {
            base.OnValueChanged(updatedValue);
            this.ResetColliderEditors();
        }

        private void ClearEditors() {
            if (this._editorCollection != null) {
                this._editorCollection.OwnedValueChanged -= this.ColliderEditor_ValueChanged;
            }

            if (this._childEditors.Any()) {
                this.Collection.RemoveEditors(this._childEditors);
                this._childEditors.Clear();
                this._valueEditorService.ReturnEditors(this._editorCollection);
                this._editorCollection = null;
            }
        }

        private void ColliderEditor_ValueChanged(object sender, ValueChangedEventArgs<object> e) {
            this.RaiseValueChanged(sender, e);
        }

        private void InitializeComponent() {
            AvaloniaXamlLoader.Load(this);
        }

        private void ResetColliderEditors() {
            this.ClearEditors();

            if (this._valueEditorService != null && this.Collection != null && this.Value is Collider value) {
                this.SetAndRaise(SelectedTypeProperty, ref this._selectedType, value.GetType());
{'fraction_non_alphanumeric': 0.08354907036949871, 'fraction_numerical': 0.0009413979759943516, 'mean_word_length': 2.3316346530772245, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '823440', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2032, 'n_words': 387}