<commit_before>﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using Autofac;
using BurnSystems.Logging;
using DatenMeister.Core.EMOF.Implementation;
using DatenMeister.Core.EMOF.Interface.Identifiers;
using DatenMeister.Core.EMOF.Interface.Reflection;
using DatenMeister.Integration;
using DatenMeister.Models.Forms;
using DatenMeister.Models.ManagementProvider;
using DatenMeister.Modules.Forms.FormFinder;
using DatenMeister.Modules.ZipExample;
using DatenMeister.Provider.ManagementProviders;
using DatenMeister.Provider.ManagementProviders.Model;
using DatenMeister.Runtime;
using DatenMeister.Runtime.Extents;
using DatenMeister.Runtime.ExtentStorage;
using DatenMeister.Runtime.ExtentStorage.Configuration;
using DatenMeister.Runtime.ExtentStorage.Interfaces;
using DatenMeister.Runtime.Workspaces;
using DatenMeister.Uml.Helper;
using DatenMeister.WPF.Forms.Base;
using DatenMeister.WPF.Forms.Specific;
using DatenMeister.WPF.Modules;
using DatenMeister.WPF.Modules.ViewExtensions.Definition;
using DatenMeister.WPF.Modules.ViewExtensions.Definition.Buttons;
using DatenMeister.WPF.Modules.ViewExtensions.Definition.GuiElements;
using DatenMeister.WPF.Modules.ViewExtensions.Definition.TreeView;
using DatenMeister.WPF.Navigation;
using DatenMeister.WPF.Windows;
using MessageBox = System.Windows.MessageBox;

namespace DatenMeister.WPF.Forms.Lists
{
    public static class WorkspaceExtentFormGenerator
    {
        /// <summary>
        /// Defines the logger for the class
        /// </summary>
        private static readonly ClassLogger Logger = new ClassLogger(typeof(WorkspaceExtentFormGenerator));

        /// <summary>
        /// Requests the form for the workspace
        /// </summary>
        /// <returns>Requested form</returns>
        internal static FormDefinition RequestFormForWorkspaces(IExtent extent, INavigationHost navigationHost)
        {
            // Finds the view
            var viewLogic = GiveMe.Scope.Resolve<FormLogic>();
            var formElement = viewLogic.GetInternalFormExtent().element($"#{ManagementViewDefinitions.IdWorkspaceListView}");

            if (formElement == null)
            {
                // The form was not found, so the form is created automatically
                // Creates the form out of the properties of the workspace
                var managementProvider = GiveMe.Scope.WorkspaceLogic.GetTypesWorkspace().Require<_ManagementProvider>();
                var listForm = viewLogic.GetListFormForExtent(extent, managementProvider.__Workspace,
                                   FormDefinitionMode.Default)
                               ?? throw new InvalidOperationException("List form could not be created");
                listForm.set(_FormAndFields._ListForm.inhibitNewItems, true);

                formElement = viewLogic.GetExtentFormForSubforms(listForm);
            }

            var formDefinition = new FormDefinition("Workspaces", formElement)
            {
                TabViewExtensionsFunction = form =>
                {
                    var result = new List<ViewExtension>
                    {
                        new RowItemButtonDefinition(
                            "Show Extents",
                            ShowExtents,
                            ItemListViewControl.ButtonPosition.Before),
                        new RowItemButtonDefinition(
                            "Delete Workspace",
                            DeleteWorkspace)
                    };

                    return result;
                }
            };

            formDefinition.ViewExtensions.Add(
                new ItemMenuButtonDefinition(
                    "Add Workspace",
                    NewWorkspace,
                    "workspaces-new",
                    NavigationCategories.DatenMeister + "." + "Workspaces"));

            formDefinition.ViewExtensions.Add(
                new ItemMenuButtonDefinition(
                    "Open Workspace-Folder",
                    (x) => NavigatorForWorkspaces.OpenFolder(navigationHost),
                    null,
                    NavigationCategories.DatenMeister + ".Workspaces"));

            formDefinition.ViewExtensions.Add(
                new ItemMenuButtonDefinition(
                    "Reset DatenMeister",
                    (x) => NavigatorForWorkspaces.ResetDatenMeister(navigationHost),
                    null,
                    NavigationCategories.DatenMeister + ".Workspaces"));

            return formDefinition;

            void NewWorkspace(IObject workspaceObject)
            {
                _ = NavigatorForWorkspaces.CreateNewWorkspace(navigationHost);
            }

            void ShowExtents(INavigationGuest navigationGuest, IObject workspace)
            {
                var workspaceId = workspace.getOrDefault<string>("id");
                if (workspaceId == null)
                {
                    return;
                }

                NavigatorForExtents.NavigateToExtentList(navigationGuest.NavigationHost, workspaceId);
            }

            void DeleteWorkspace(INavigationGuest navigationGuest, IObject workspace)
            {
                var workspaceId = workspace.getOrDefault<string>("id");
                
                if (MessageBox.Show(
                        $"Are you sure to delete the workspace '{workspaceId}'? " +
                        $"All included extents will also be deleted.", 
                        "Confirmation",
                        MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    var workspaceLogic = GiveMe.Scope.Resolve<IWorkspaceLogic>();
                    workspaceLogic.RemoveWorkspace(workspaceId);
                }
            }
        }

        /// <summary>
        /// Requests the form for extent elements
        /// </summary>
        /// <param name="extent">Extent being used</param>
        /// <param name="workspaceId">The Id of the workspace</param>
        /// <param name="navigationHost">Defines the navigation host being used for the window</param>
        /// <returns>The created form</returns>
        internal static FormDefinition RequestFormForExtents(IExtent extent, string workspaceId, INavigationHost navigationHost)
        {
            var viewLogic = GiveMe.Scope.Resolve<FormLogic>();
            var viewExtent = viewLogic.GetInternalFormExtent();
            var result =
                NamedElementMethods.GetByFullName(
                    viewExtent,
                    ManagementViewDefinitions.PathExtentListView);

            if (result == null)
            {
                // result = viewLogic.GetExtentForm(control.Items, ViewDefinitionMode.Default);
                var formAndFields = GiveMe.Scope.WorkspaceLogic.GetTypesWorkspace().Require<_ManagementProvider>();
                var listForm = viewLogic.GetListFormForExtent(
                                   extent,
                                   formAndFields.__Extent,
                                   FormDefinitionMode.Default) ??
                               throw new InvalidOperationException("listForm == null");
                listForm.set(_FormAndFields._ListForm.inhibitDeleteItems, true);
                listForm.set(_FormAndFields._ListForm.inhibitNewItems, true);

                result = viewLogic.GetExtentFormForSubforms(listForm);
            }

            var viewDefinition = new FormDefinition("Extents", result)
            {
                TabViewExtensionsFunction = (form) => new List<ViewExtension>
                {
                    new RowItemButtonDefinition("Show Items", ShowItems, ItemListViewControl.ButtonPosition.Before),
                    new RowItemButtonDefinition("Save", SaveExtent),
                    new RowItemButtonDefinition("Delete", DeleteExtent)
                }
            };

            viewDefinition.ViewExtensions.Add(
                new TreeViewItemCommandDefinition(
                    "New Extent",
                    LoadExtent));<commit_msg>Fixed bug of showing Extents in Workspaces<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using Autofac;
using BurnSystems.Logging;
using DatenMeister.Core.EMOF.Implementation;
using DatenMeister.Core.EMOF.Interface.Identifiers;
using DatenMeister.Core.EMOF.Interface.Reflection;
using DatenMeister.Integration;
using DatenMeister.Models.Forms;
using DatenMeister.Models.ManagementProvider;
using DatenMeister.Modules.Forms.FormFinder;
using DatenMeister.Modules.ZipExample;
using DatenMeister.Provider.ManagementProviders;
using DatenMeister.Provider.ManagementProviders.Model;
using DatenMeister.Runtime;
using DatenMeister.Runtime.Extents;
using DatenMeister.Runtime.ExtentStorage;
using DatenMeister.Runtime.ExtentStorage.Configuration;
using DatenMeister.Runtime.ExtentStorage.Interfaces;
using DatenMeister.Runtime.Workspaces;
using DatenMeister.Uml.Helper;
using DatenMeister.WPF.Forms.Base;
using DatenMeister.WPF.Forms.Specific;
using DatenMeister.WPF.Modules;
using DatenMeister.WPF.Modules.ViewExtensions.Definition;
using DatenMeister.WPF.Modules.ViewExtensions.Definition.Buttons;
using DatenMeister.WPF.Modules.ViewExtensions.Definition.GuiElements;
using DatenMeister.WPF.Modules.ViewExtensions.Definition.TreeView;
using DatenMeister.WPF.Navigation;
using DatenMeister.WPF.Windows;
using MessageBox = System.Windows.MessageBox;

namespace DatenMeister.WPF.Forms.Lists
{
    public static class WorkspaceExtentFormGenerator
    {
        /// <summary>
        /// Defines the logger for the class
        /// </summary>
        private static readonly ClassLogger Logger = new ClassLogger(typeof(WorkspaceExtentFormGenerator));

        /// <summary>
        /// Requests the form for the workspace
        /// </summary>
        /// <returns>Requested form</returns>
        internal static FormDefinition RequestFormForWorkspaces(IExtent extent, INavigationHost navigationHost)
        {
            // Finds the view
            var viewLogic = GiveMe.Scope.Resolve<FormLogic>();
            var formElement = viewLogic.GetInternalFormExtent().element($"#{ManagementViewDefinitions.IdWorkspaceListView}");

            if (formElement == null)
            {
                // The form was not found, so the form is created automatically
                // Creates the form out of the properties of the workspace
                var managementProvider = GiveMe.Scope.WorkspaceLogic.GetTypesWorkspace().Require<_ManagementProvider>();
                var listForm = viewLogic.GetListFormForExtent(extent, managementProvider.__Workspace,
                                   FormDefinitionMode.Default)
                               ?? throw new InvalidOperationException("List form could not be created");
                listForm.set(_FormAndFields._ListForm.inhibitNewItems, true);

                formElement = viewLogic.GetExtentFormForSubforms(listForm);
            }

            var formDefinition = new FormDefinition("Workspaces", formElement)
            {
                TabViewExtensionsFunction = form =>
                {
                    var result = new List<ViewExtension>
                    {
                        new RowItemButtonDefinition(
                            "Show Extents",
                            ShowExtents,
                            ItemListViewControl.ButtonPosition.Before),
                        new RowItemButtonDefinition(
                            "Delete Workspace",
                            DeleteWorkspace)
                    };

                    return result;
                }
            };

            formDefinition.ViewExtensions.Add(
                new ItemMenuButtonDefinition(
                    "Add Workspace",
                    NewWorkspace,
                    "workspaces-new",
                    NavigationCategories.DatenMeister + "." + "Workspaces"));

            formDefinition.ViewExtensions.Add(
                new ItemMenuButtonDefinition(
                    "Open Workspace-Folder",
                    (x) => NavigatorForWorkspaces.OpenFolder(navigationHost),
                    null,
                    NavigationCategories.DatenMeister + ".Workspaces"));

            formDefinition.ViewExtensions.Add(
                new ItemMenuButtonDefinition(
                    "Reset DatenMeister",
                    (x) => NavigatorForWorkspaces.ResetDatenMeister(navigationHost),
                    null,
                    NavigationCategories.DatenMeister + ".Workspaces"));

            return formDefinition;

            void NewWorkspace(IObject workspaceObject)
            {
                _ = NavigatorForWorkspaces.CreateNewWorkspace(navigationHost);
            }

            void ShowExtents(INavigationGuest navigationGuest, IObject workspace)
            {
                var workspaceId = workspace.getOrDefault<string>("id");
                if (workspaceId == null)
                {
                    return;
                }

                NavigatorForExtents.NavigateToExtentList(navigationGuest.NavigationHost, workspaceId);
            }

            void DeleteWorkspace(INavigationGuest navigationGuest, IObject workspace)
            {
                var workspaceId = workspace.getOrDefault<string>("id");
                
                if (MessageBox.Show(
                        $"Are you sure to delete the workspace '{workspaceId}'? " +
                        $"All included extents will also be deleted.", 
                        "Confirmation",
                        MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    var workspaceLogic = GiveMe.Scope.Resolve<IWorkspaceLogic>();
                    workspaceLogic.RemoveWorkspace(workspaceId);
                }
            }
        }

        /// <summary>
        /// Requests the form for extent elements
        /// </summary>
        /// <param name="extent">Extent being used</param>
        /// <param name="workspaceId">The Id of the workspace</param>
        /// <param name="navigationHost">Defines the navigation host being used for the window</param>
        /// <returns>The created form</returns>
        internal static FormDefinition RequestFormForExtents(IExtent extent, string workspaceId, INavigationHost navigationHost)
        {
            var viewLogic = GiveMe.Scope.Resolve<FormLogic>();
            var viewExtent = viewLogic.GetInternalFormExtent();
            var result =
                NamedElementMethods.GetByFullName(
                    viewExtent,
                    ManagementViewDefinitions.PathExtentListView);

            if (result == null)
            {
                // result = viewLogic.GetExtentForm(control.Items, ViewDefinitionMode.Default);
                var formAndFields = GiveMe.Scope.WorkspaceLogic.GetTypesWorkspace().Require<_ManagementProvider>();
                var listForm = viewLogic.GetListFormForExtent(
                                   extent,
                                   formAndFields.__Extent,
                                   FormDefinitionMode.Default) ??
                               throw new InvalidOperationException("listForm == null");
                listForm.set(_FormAndFields._ListForm.inhibitDeleteItems, true);
                listForm.set(_FormAndFields._ListForm.inhibitNewItems, true);
                listForm.set(_FormAndFields._ListForm.property, nameof(_ManagementProvider._Workspace.extents));

                result = viewLogic.GetExtentFormForSubforms(listForm);
            }

            var viewDefinition = new FormDefinition("Extents", result)
            {
                TabViewExtensionsFunction = (form) => new List<ViewExtension>
                {
                    new RowItemButtonDefinition("Show Items", ShowItems, ItemListViewControl.ButtonPosition.Before),
                    new RowItemButtonDefinition("Save", SaveExtent),
                    new RowItemButtonDefinition("Delete", DeleteExtent)
                }
            };

            viewDefinition.ViewExtensions.Add(
                new TreeViewItemCommandDefinition(
                    "New Extent",
                    LoadExtent));
{'fraction_non_alphanumeric': 0.07851789168278529, 'fraction_numerical': 0.0, 'mean_word_length': 1.9376775568181819, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2275912', 'n_tokens_mistral': 4024, 'n_tokens_neox': 3826, 'n_words': 749}