package application;

import java.awt.Desktop;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileActionsV2 extends JFrame {
	private JLabel file1Label, file2Label;
	private JButton selectFile1, selectFile2;
	private File file1, file2;
	private JMenuBar menuBar;
	private JMenu actionsMenu;
	private JMenuItem openInExcelItem;
	private JPopupMenu contextMenuFile1, contextMenuFile2;

	public FileActionsV2() {
		setTitle("Datei-Auswahl und Aktionen");
		setSize(400, 250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new GridLayout(3, 2));

		file1Label = new JLabel("Datei 1: Keine ausgewählt");
		file2Label = new JLabel("Datei 2: Keine ausgewählt");
		selectFile1 = new JButton("Datei 1 auswählen");
		selectFile2 = new JButton("Datei 2 auswählen");

		// Kontextmenüs erstellen
		contextMenuFile1 = new JPopupMenu();
		contextMenuFile2 = new JPopupMenu();
		JMenuItem renameFile1 = new JMenuItem("Datei umbenennen");
		JMenuItem renameFile2 = new JMenuItem("Datei umbenennen");
		contextMenuFile1.add(renameFile1);
		contextMenuFile2.add(renameFile2);

		// MouseListener für Kontextmenü auf den Labels
		file1Label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e) && file1 != null) {
					contextMenuFile1.show(file1Label, e.getX(), e.getY());
				}
			}
		});
		file2Label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e) && file2 != null) {
					contextMenuFile2.show(file2Label, e.getX(), e.getY());
				}
			}
		});

		renameFile1.addActionListener(e -> renameFile(file1, file1Label, "Datei 1"));
		renameFile2.addActionListener(e -> renameFile(file2, file2Label, "Datei 2"));

		// File Chooser für Datei 1 (nur Excel-Dateien)
		selectFile1.addActionListener(e -> {
			JFileChooser fileChooser = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
			fileChooser.setFileFilter(filter);
			if (file1 != null)
				fileChooser.setCurrentDirectory(file1.getParentFile());
			if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				file1 = fileChooser.getSelectedFile();
				file1Label.setText("Datei 1: " + file1.getName());
				selectFile2.setEnabled(true);
			}
		});

		// File Chooser für Datei 2 (im selben Verzeichnis wie Datei 1, nur
		// Excel-Dateien)
		selectFile2.setEnabled(false);
		selectFile2.addActionListener(e -> {
			if (file1 != null) {
				JFileChooser fileChooser = new JFileChooser(file1.getParentFile());
				FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
				fileChooser.setFileFilter(filter);
				if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
					file2 = fileChooser.getSelectedFile();
					file2Label.setText("Datei 2: " + file2.getName());
				}
			}
		});

		add(selectFile1);
		add(file1Label);
		add(selectFile2);
		add(file2Label);

		// Menü erstellen
		menuBar = new JMenuBar();
		actionsMenu = new JMenu("Aktionen");
		openInExcelItem = new JMenuItem("Erste Datei in Excel öffnen");

		actionsMenu.add(openInExcelItem);
		menuBar.add(actionsMenu);
		setJMenuBar(menuBar);

		// Aktion: Erste Datei in Excel öffnen
		openInExcelItem.addActionListener(e -> {
			if (file1 != null) {
				try {
					Desktop.getDesktop().open(file1);
				} catch (IOException ex) {
					JOptionPane.showMessageDialog(this, "Fehler beim Öffnen der Datei in Excel.", "Fehler",
							JOptionPane.ERROR_MESSAGE);
				}
			} else {
				JOptionPane.showMessageDialog(this, "Bitte wähle zuerst eine Datei aus!", "Fehler",
						JOptionPane.ERROR_MESSAGE);
			}
		});
	}

	private void renameFile(File file, JLabel label, String prefix) {
		if (file == null)
			return;
		String newName = JOptionPane.showInputDialog(this, "Neuer Dateiname:", file.getName());
		if (newName != null && !newName.trim().isEmpty()) {
			File newFile = new File(file.getParentFile(), newName);
			if (file.renameTo(newFile)) {
				file = newFile;
				label.setText(prefix + ": " + newFile.getName());
				if (prefix.equals("Datei 1"))
					this.file1 = newFile;
				else
					this.file2 = newFile;
			} else {
				JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> {
			FileActionsV2 gui = new FileActionsV2();
			gui.setVisible(true);
		});
	}
}
