CodeMergeNG/ViewModels/MainWindowViewModel.cs

597 lines
18 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using CommunityToolkit.Mvvm.Input;
using DocumentFormat.OpenXml.Wordprocessing;
using ICSharpCode.AvalonEdit.Document;
using Microsoft.Win32;
namespace CodeMerger.ViewModels
{
public class MainWindowViewModel : INotifyPropertyChanged
{
#region Campos Privados
private readonly cCodeMerger _codeMerger;
private readonly Settings _settings;
private string _searchText;
private double _firstColumnWidth;
private double _secondColumnWidth;
private double _thirdColumnWidth;
private TextDocument _originalDocument;
private TextDocument _llmDocument;
private TextDocument _mergedDocument;
private CodeElement _originalStructure;
private CodeElement _llmStructure;
private CodeElement _mergedStructure;
private ObservableCollection<string> _originalNamespaces;
private ObservableCollection<string> _originalClasses;
private string _selectedLLMNamespace;
private string _selectedLLMClass;
#endregion
#region Constructores
public MainWindowViewModel()
{
// Cargar configuración
_settings = Settings.Load();
// Inicializar CodeMerger
_codeMerger = new cCodeMerger();
// Inicializar documentos
_originalDocument = new TextDocument();
_llmDocument = new TextDocument();
_mergedDocument = new TextDocument();
// Inicializar anchos de columnas
_firstColumnWidth = _settings.FirstColumnWidth;
_secondColumnWidth = _settings.SecondColumnWidth;
_thirdColumnWidth = _settings.ThirdColumnWidth;
// Inicializar colecciones
_originalNamespaces = new ObservableCollection<string>();
_originalClasses = new ObservableCollection<string>();
// Inicializar comandos
InitializeCommands();
}
#endregion
#region Propiedades
public Settings Settings => _settings;
public string SearchText
{
get => _searchText;
set
{
if (_searchText != value)
{
_searchText = value;
OnPropertyChanged();
}
}
}
public double FirstColumnWidth
{
get => _firstColumnWidth;
set
{
if (_firstColumnWidth != value)
{
_firstColumnWidth = value;
_settings.FirstColumnWidth = value;
OnPropertyChanged();
}
}
}
public double SecondColumnWidth
{
get => _secondColumnWidth;
set
{
if (_secondColumnWidth != value)
{
_secondColumnWidth = value;
_settings.SecondColumnWidth = value;
OnPropertyChanged();
}
}
}
public double ThirdColumnWidth
{
get => _thirdColumnWidth;
set
{
if (_thirdColumnWidth != value)
{
_thirdColumnWidth = value;
_settings.ThirdColumnWidth = value;
OnPropertyChanged();
}
}
}
public TextDocument OriginalDocument
{
get => _originalDocument;
set
{
if (_originalDocument != value)
{
_originalDocument = value;
OnPropertyChanged();
}
}
}
public TextDocument LLMDocument
{
get => _llmDocument;
set
{
if (_llmDocument != value)
{
_llmDocument = value;
OnPropertyChanged();
}
}
}
public TextDocument MergedDocument
{
get => _mergedDocument;
set
{
if (_mergedDocument != value)
{
_mergedDocument = value;
OnPropertyChanged();
}
}
}
public CodeElement OriginalStructure
{
get => _originalStructure;
set
{
if (_originalStructure != value)
{
_originalStructure = value;
OnPropertyChanged();
}
}
}
public CodeElement LLMStructure
{
get => _llmStructure;
set
{
if (_llmStructure != value)
{
_llmStructure = value;
OnPropertyChanged();
}
}
}
public CodeElement MergedStructure
{
get => _mergedStructure;
set
{
if (_mergedStructure != value)
{
_mergedStructure = value;
OnPropertyChanged();
}
}
}
public ObservableCollection<string> OriginalNamespaces
{
get => _originalNamespaces;
set
{
if (_originalNamespaces != value)
{
_originalNamespaces = value;
OnPropertyChanged();
}
}
}
public ObservableCollection<string> OriginalClasses
{
get => _originalClasses;
set
{
if (_originalClasses != value)
{
_originalClasses = value;
OnPropertyChanged();
}
}
}
public string SelectedLLMNamespace
{
get => _selectedLLMNamespace;
set
{
if (_selectedLLMNamespace != value)
{
_selectedLLMNamespace = value;
OnPropertyChanged();
// Actualizar namespace por defecto en CodeMerger
if (!string.IsNullOrEmpty(value))
_codeMerger.DefaultNamespace = value;
}
}
}
public string SelectedLLMClass
{
get => _selectedLLMClass;
set
{
if (_selectedLLMClass != value)
{
_selectedLLMClass = value;
OnPropertyChanged();
// Actualizar clase por defecto en CodeMerger
if (!string.IsNullOrEmpty(value))
_codeMerger.DefaultClass = value;
}
}
}
#endregion
#region Comandos
public ICommand PasteOriginalCommand { get; private set; }
public ICommand CopyOriginalCommand { get; private set; }
public ICommand PasteLLMCommand { get; private set; }
public ICommand CopyLLMCommand { get; private set; }
public ICommand CopyMergedCommand { get; private set; }
public ICommand SearchCommand { get; private set; }
public ICommand ClearAllCommand { get; private set; }
public ICommand AnalyzeCodeCommand { get; private set; }
public ICommand MergeCodeCommand { get; private set; }
public ICommand ShowOriginalStructureCommand { get; private set; }
public ICommand ShowLLMStructureCommand { get; private set; }
public ICommand ShowMergedStructureCommand { get; private set; }
private void InitializeCommands()
{
PasteOriginalCommand = new RelayCommand(PasteOriginal);
CopyOriginalCommand = new RelayCommand(CopyOriginal);
PasteLLMCommand = new RelayCommand(PasteLLM);
CopyLLMCommand = new RelayCommand(CopyLLM);
CopyMergedCommand = new RelayCommand(CopyMerged);
SearchCommand = new RelayCommand(Search);
ClearAllCommand = new RelayCommand(ClearAll);
AnalyzeCodeCommand = new RelayCommand(AnalyzeCode);
MergeCodeCommand = new RelayCommand(MergeCode);
ShowOriginalStructureCommand = new RelayCommand(ShowOriginalStructure);
ShowLLMStructureCommand = new RelayCommand(ShowLLMStructure);
ShowMergedStructureCommand = new RelayCommand(ShowMergedStructure);
}
#endregion
#region Métodos de Comando
private void PasteOriginal()
{
if (Clipboard.ContainsText())
{
OriginalDocument.Text = Clipboard.GetText();
LogInfo("Código original pegado desde el portapapeles");
}
}
private void CopyOriginal()
{
if (!string.IsNullOrEmpty(OriginalDocument.Text))
{
Clipboard.SetText(OriginalDocument.Text);
LogInfo("Código original copiado al portapapeles");
}
}
private void PasteLLM()
{
if (Clipboard.ContainsText())
{
LLMDocument.Text = Clipboard.GetText();
LogInfo("Código LLM pegado desde el portapapeles");
}
}
private void CopyLLM()
{
if (!string.IsNullOrEmpty(LLMDocument.Text))
{
Clipboard.SetText(LLMDocument.Text);
LogInfo("Código LLM copiado al portapapeles");
}
}
private void CopyMerged()
{
if (!string.IsNullOrEmpty(MergedDocument.Text))
{
Clipboard.SetText(MergedDocument.Text);
LogInfo("Código fusionado copiado al portapapeles");
}
}
private void Search()
{
if (string.IsNullOrEmpty(SearchText))
return;
MessageBox.Show($"Función de búsqueda por implementar: {SearchText}");
}
private void ClearAll()
{
OriginalDocument.Text = string.Empty;
LLMDocument.Text = string.Empty;
MergedDocument.Text = string.Empty;
OriginalStructure = null;
LLMStructure = null;
MergedStructure = null;
LogInfo("Todos los editores han sido limpiados");
}
private void AnalyzeCode()
{
try
{
// Analizar código original
if (!string.IsNullOrEmpty(OriginalDocument.Text))
{
OriginalStructure = _codeMerger.AnalyzeCode(OriginalDocument.Text);
LogInfo("Código original analizado correctamente");
// Extraer namespaces y clases para las listas desplegables
UpdateNamespacesAndClasses();
}
// Analizar código LLM
if (!string.IsNullOrEmpty(LLMDocument.Text))
{
LLMStructure = _codeMerger.AnalyzeCode(LLMDocument.Text);
LogInfo("Código LLM analizado correctamente");
}
// Analizar código fusionado (si existe)
if (!string.IsNullOrEmpty(MergedDocument.Text))
{
MergedStructure = _codeMerger.AnalyzeCode(MergedDocument.Text);
LogInfo("Código fusionado analizado correctamente");
}
}
catch (Exception ex)
{
LogError($"Error al analizar código: {ex.Message}");
MessageBox.Show($"Error al analizar código: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void MergeCode()
{
try
{
if (string.IsNullOrEmpty(OriginalDocument.Text))
{
LogWarning("El código original está vacío");
return;
}
if (string.IsNullOrEmpty(LLMDocument.Text))
{
LogWarning("El código LLM está vacío");
return;
}
// Realizar el merge
string mergedCode = _codeMerger.MergeCode(OriginalDocument.Text, LLMDocument.Text);
MergedDocument.Text = mergedCode;
// Analizar el código fusionado
MergedStructure = _codeMerger.AnalyzeCode(mergedCode);
// Mostrar log del merge
string mergeLog = _codeMerger.GetLog();
LogInfo("Código fusionado generado correctamente");
LogMergeDetails(mergeLog);
}
catch (Exception ex)
{
LogError($"Error al fusionar código: {ex.Message}");
MessageBox.Show($"Error al fusionar código: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ShowOriginalStructure()
{
if (OriginalStructure == null)
{
if (!string.IsNullOrEmpty(OriginalDocument.Text))
{
OriginalStructure = _codeMerger.AnalyzeCode(OriginalDocument.Text);
}
else
{
LogWarning("No hay código original para mostrar estructura");
return;
}
}
ShowStructureWindow("Estructura del Código Original", OriginalStructure);
}
private void ShowLLMStructure()
{
if (LLMStructure == null)
{
if (!string.IsNullOrEmpty(LLMDocument.Text))
{
LLMStructure = _codeMerger.AnalyzeCode(LLMDocument.Text);
}
else
{
LogWarning("No hay código LLM para mostrar estructura");
return;
}
}
ShowStructureWindow("Estructura del Código LLM", LLMStructure);
}
private void ShowMergedStructure()
{
if (MergedStructure == null)
{
if (!string.IsNullOrEmpty(MergedDocument.Text))
{
MergedStructure = _codeMerger.AnalyzeCode(MergedDocument.Text);
}
else
{
LogWarning("No hay código fusionado para mostrar estructura");
return;
}
}
ShowStructureWindow("Estructura del Código Fusionado", MergedStructure);
}
#endregion
#region Métodos Auxiliares
private void UpdateNamespacesAndClasses()
{
OriginalNamespaces.Clear();
OriginalClasses.Clear();
if (OriginalStructure != null)
{
// Extraer namespaces
var namespaces = OriginalStructure.GetAllDescendantsOfType(CodeElement.ElementType.Namespace)
.Where(n => !n.IsVirtualNamespace)
.Select(n => n.Name)
.ToList();
foreach (var ns in namespaces)
{
OriginalNamespaces.Add(ns);
}
// Extraer clases
var classes = OriginalStructure.GetAllDescendantsOfType(CodeElement.ElementType.Class)
.Where(c => !c.IsVirtualClass)
.Select(c => c.Name)
.ToList();
foreach (var cls in classes)
{
OriginalClasses.Add(cls);
}
// Seleccionar valores por defecto si hay opciones disponibles
if (OriginalNamespaces.Count > 0 && string.IsNullOrEmpty(SelectedLLMNamespace))
{
SelectedLLMNamespace = OriginalNamespaces[0];
}
if (OriginalClasses.Count > 0 && string.IsNullOrEmpty(SelectedLLMClass))
{
SelectedLLMClass = OriginalClasses[0];
}
}
}
private void ShowStructureWindow(string title, CodeElement structure)
{
var structureWindow = new Controls.CodeStructureTreeView();
structureWindow.Title = title;
structureWindow.LoadCodeStructure(structure);
structureWindow.Owner = Application.Current.MainWindow;
structureWindow.Show();
}
private void LogInfo(string message)
{
Application.Current.Dispatcher.Invoke(() =>
{
var logControl = (Application.Current.MainWindow as MainWindow)?.logControl;
logControl?.LogInfo(message);
});
}
private void LogWarning(string message)
{
Application.Current.Dispatcher.Invoke(() =>
{
var logControl = (Application.Current.MainWindow as MainWindow)?.logControl;
logControl?.LogWarning(message);
});
}
private void LogError(string message)
{
Application.Current.Dispatcher.Invoke(() =>
{
var logControl = (Application.Current.MainWindow as MainWindow)?.logControl;
logControl?.LogError(message);
});
}
private void LogMergeDetails(string mergeLog)
{
Application.Current.Dispatcher.Invoke(() =>
{
var logControl = (Application.Current.MainWindow as MainWindow)?.logControl;
logControl?.LogMergeDetails(mergeLog);
});
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}