using BeWo.Services; using BeWo.ViewModel.ListViewModel; using BS.Shared; using BS.Shared.Extensions.CustomInterfaces; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using DelegateCommand = DevExpress.Mvvm.DelegateCommand; namespace BeWo.ViewModel.View.Administration { public class AiPromptbausteinTreeViewModel : WindowViewModel { private IWindowService WindowService { get; set; } public AiPromptbausteinTreeViewModel() { if (BeWoApp.IsInDesignMode) { ListVM = new AiPromptbausteinTreeListVM(); } Func canAddPrompt = () => SelectedNode.IsOrdner() || SelectedNode.IsKette(); Func canAddOrdner = () => SelectedNode.IsOrdner(); Func canAddKette = () => SelectedNode.IsOrdner() && false; Func canEdit = () => SelectedNode is object; Func canCopy = () => SelectedNode.IsKette() || SelectedNode.IsOrdner(); Func canCopySingle = () => SelectedNode is object; Func canPaste = () => HasCopiedTreeNode && !Contains(CopyTreeNode, SelectedNode) && (!CopyTreeNode.IsOrdner() || canAddOrdner()) && (!CopyTreeNode.IsKette() || canAddKette()) && (!CopyTreeNode.IsPrompt() || canAddPrompt()); Func canDelete = () => SelectedNode is object; AddPromptCommand = new DelegateCommand(() => Add(AiPromptbausteinType.Prompt), canAddPrompt); AddOrdnerCommand = new DelegateCommand(() => Add(AiPromptbausteinType.Ordner), canAddOrdner); AddKetteCommand = new DelegateCommand(() => Add(AiPromptbausteinType.Kette), canAddKette); EditPromptbausteinCommand = new DelegateCommand(EditPromptbaustein, canEdit); CopyPromptbausteinCommand = new DelegateCommand(CopyPromptbaustein, canCopy); CopySinglePromptbausteinCommand = new DelegateCommand(CopySinglePromptbaustein, canCopySingle); PastePromptbausteinCommand = new DelegateCommand(PastePromptbaustein, canPaste); DeletePromptbausteinCommand = new DelegateCommand(DeletePromptbaustein, canDelete); } public AiPromptbausteinTreeViewModel(IWindowService windowService) : this() { WindowService = windowService; } public AiPromptbausteinTreeVM SelectedNode { get; set; } public AiPromptbausteinTreeListVM ListVM { get; private set; } public bool CopySingleTreeNode { get; set; } public AiPromptbausteinTreeVM CopyTreeNode { get; set; } public bool HasCopiedTreeNode => CopyTreeNode != null; public bool IsEditingMode { get; set; } public ICommand AddPromptCommand { get; set; } public ICommand AddOrdnerCommand { get; set; } public ICommand AddKetteCommand { get; set; } public ICommand EditPromptbausteinCommand { get; set; } public ICommand CopyPromptbausteinCommand { get; set; } public ICommand CopySinglePromptbausteinCommand { get; set; } public ICommand PastePromptbausteinCommand { get; set; } public ICommand DeletePromptbausteinCommand { get; set; } public override bool IsDirty { get { if (ListVM is null) return false; return ListVM.ContainsDirtyNode(); } } public void SaveVMList() { // BFS -> 1. Sobald Knoten gefunden wird mit IsNew=true: Teilbaum als neu markieren // 2. Sobald Knoten gefunden wird mit IsNew=false && IsDirty=true: Knoten als update markieren // 3. Checken, welche Knoten gelöscht wurden } public void UpdateVMList(AiPromptbausteinTreeListVM new_list) { ListVM = new_list; FirePropertyChanged(nameof(ListVM)); } public void Add(AiPromptbausteinType type) { var parent = SelectedNode?.Children ?? ListVM; parent.NewVM = null; parent.NewVM.PromptbausteinType = type; parent.NewVM.Parent = SelectedNode; WindowService.ShowAiPromptbausteinDetailWindow(AiPromptbausteinDetailViewModel.CreateAdd(this, parent.NewVM, parent)); } public void EditPromptbaustein() { WindowService.ShowAiPromptbausteinDetailWindow(AiPromptbausteinDetailViewModel.CreateEdit(this, SelectedNode)); } public void CopyPromptbaustein() { CopySingleTreeNode = false; CopyTreeNode = SelectedNode; } public void CopySinglePromptbaustein() { CopySingleTreeNode = true; CopyTreeNode = SelectedNode; } public void PastePromptbaustein() { CopyPastePromptbaustein(!CopySingleTreeNode, CopyTreeNode, SelectedNode); } private void CopyPastePromptbaustein(bool rec, AiPromptbausteinTreeVM old_node, AiPromptbausteinTreeVM new_parent) { var old_parent = old_node?.Parent; var new_node = old_node.CopyToNewViewModel(); if (new_parent is null) { ListVM.VMList.Add(new_node); } else { new_parent.Children.VMList.Add(new_node); new_node.Parent = new_parent; } if (!rec) return; foreach (var old_child in old_node.Children.VMList) { CopyPastePromptbaustein(rec, old_child, new_node); } } public void DeletePromptbaustein() { var node = SelectedNode; var parent_source = node.Parent?.Children ?? ListVM; parent_source.VMList.Remove(node); } private bool Contains(AiPromptbausteinTreeVM current_node, AiPromptbausteinTreeVM search) { if (current_node == search) return true; if (current_node.Children is null || !current_node.Children.VMList.Any()) return false; foreach (var node in current_node.Children.VMList) { if (Contains(node, search)) return true; } return false; } } }