Files
BeWoPlaner/BeWo/ViewModel/View/Administration/AiPromptbausteinTreeViewModel.cs

151 lines
4.3 KiB
C#

using BeWo.Services;
using BeWo.ViewModel.ListViewModel;
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<bool> isObject = () => SelectedNode is object;
Func<bool> isKategorie = () => SelectedNode?.IsKategorie ?? false;
Func<bool> canAdd = () => !isObject() || isKategorie();
Func<bool> isNoLeaf = () => isObject() && isKategorie();
Func<bool> isNotSame = () => !Contains(CopyTreeNode, SelectedNode);
Func<bool> canCopy = () => HasCopiedTreeNode && canAdd() && isNotSame();
AddPromptCommand = new DelegateCommand(() => Add(false), canAdd);
AddKategorieCommand = new DelegateCommand(() => Add(true), canAdd);
EditPromptbausteinCommand = new DelegateCommand(EditPromptbaustein, isObject);
CopyPromptbausteinCommand = new DelegateCommand(CopyPromptbaustein, isNoLeaf);
CopySinglePromptbausteinCommand = new DelegateCommand(CopySinglePromptbaustein, isObject);
PastePromptbausteinCommand = new DelegateCommand(PastePromptbaustein, canCopy);
DeletePromptbausteinCommand = new DelegateCommand(DeletePromptbaustein, isObject);
}
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 AddKategorieCommand { 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 bool IsDirty => ListVM.IsDirty;
public void UpdateVMList(AiPromptbausteinTreeListVM new_list)
{
ListVM = new_list;
FirePropertyChanged(nameof(ListVM));
}
public void Add(bool isKategorie)
{
var parent = SelectedNode?.Children ?? ListVM;
parent.NewVM = null;
parent.NewVM.IsKategorie = isKategorie;
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;
}
}
}