using System.Threading.Tasks; using System.Windows.Input; using PipefyAddCompanies.Core.UseCases; using PipefyAddCompanies.UI.Views; using System.ComponentModel; using PipefyAddCompanies.Core.Models; using System.Collections.ObjectModel; using PipefyAddCompanies.Core.Services; using System.Linq; using System.Windows; using System; using System.IO; using Microsoft.Extensions.Configuration; namespace PipefyAddCompanies.UI.ViewModels { public class MainWindowViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; private readonly ProcessadorDeEmpresas _processadorDeEmpresas; public ICommand SelecionarArquivoCommand { get; } public ICommand ProcessarCommand { get; } public ICommand LimparCommand { get; } private string? _caminhoPlanilha; private bool _isPlanilhaSelecionada; private readonly string _templateName = string.Empty; public ObservableCollection Empresas { get; } = new ObservableCollection(); public ObservableCollection EmpresasExistentes { get; } = new ObservableCollection(); protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public MainWindowViewModel(ProcessadorDeEmpresas processadorDeEmpresas) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); _templateName = config["FileTemplate:Name"] ?? string.Empty; _processadorDeEmpresas = processadorDeEmpresas; SelecionarArquivoCommand = new RelayCommand(SelecionarArquivoPlanilha); ProcessarCommand = new RelayCommand(async () => await ProcessarEmpresasAsync()); LimparCommand = new RelayCommand(LimparEmpresas); } public void OnWindowClosing() { // Limpa as listas de empresas ao fechar a janela Empresas.Clear(); EmpresasExistentes.Clear(); } // Método para limpar empresas private void LimparEmpresas() { Empresas.Clear(); EmpresasExistentes.Clear(); CaminhoPlanilha = null; // Opcional: Reseta o caminho da planilha PodeProcessar = false; // Desabilita o botão de processamento } public void DefinirCaminhoPlanilha(string caminho) { if (Path.GetExtension(caminho).Equals(".xlsx", StringComparison.OrdinalIgnoreCase) && Path.GetFileNameWithoutExtension(caminho).Contains(_templateName, StringComparison.OrdinalIgnoreCase)) { CaminhoPlanilha = caminho; CarregarEmpresas(); PodeProcessar = true; // Habilita o botão de processamento } else { MessageBox.Show("Por favor, selecione um arquivo Excel válido."); } } public string? CaminhoPlanilha { get => _caminhoPlanilha; set { _caminhoPlanilha = value; OnPropertyChanged(nameof(CaminhoPlanilha)); } } public bool PodeProcessar { get => _isPlanilhaSelecionada; set { _isPlanilhaSelecionada = value; OnPropertyChanged(nameof(PodeProcessar)); } } private void SelecionarArquivoPlanilha() { _ = new MainWindow(); _caminhoPlanilha = MainWindow.SelecionarArquivoPlanilha(); if (!string.IsNullOrEmpty(_caminhoPlanilha)) { DefinirCaminhoPlanilha(_caminhoPlanilha); } } private void CarregarEmpresas() { Empresas.Clear(); EmpresasExistentes.Clear(); if (string.IsNullOrEmpty(_caminhoPlanilha)) return; var empresasCarregadas = PlanilhaService.LerPlanilha(_caminhoPlanilha); var pipefyService = new PipefyService(); foreach (var empresa in empresasCarregadas) { empresa.AtualizarExistenciaAsync(pipefyService).ContinueWith(task => { Application.Current.Dispatcher.Invoke(() => { if (empresa.ExisteNoPipefy == true) { EmpresasExistentes.Add(empresa); } else { Empresas.Add(empresa); } }); }); } } public async Task ProcessarEmpresasAsync() { if (!Empresas.Where(x => (x.ExisteNoPipefy == null)).Any()) { await _processadorDeEmpresas.ProcessarAsync(Empresas.Where(x => !(x.ExisteNoPipefy ?? false))); } } } }