package application;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;

import javax.naming.ConfigurationException;
import javax.swing.BorderFactory;
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.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

import de.lcag.common.ExcelWorkbook;
import de.lcag.common.LcagProperties;
import de.lcag.common.MappingTable;
import de.lcag.common.Table;
import de.lcag.common.WorkbookComparisonResult;
import routines.LcagFileTools;
import routines.LcagStringTools;

public class FileActions extends JFrame {
	private static final int FRAME_HEIGHT = 150;
	private static String MASTER_FILE_LABEL = "Main Excel File";
	private static String CONFIG_FILE_LABEL = "Configuration File";

	private JLabel masterFileLabel, clientFileLabel, configFileLabel;
	private JButton selectMasterFile, selectClientFile, selectConfigFile;
	private Path masterFile, clientFile, configFile;
	private JMenuBar menuBar;
	private JMenu actionsMenu;
	private JMenuItem mergeAction;
	private JPopupMenu contextMenuMasterFile, contextMenuClientFile;
	private File configFileDirectoryPath = null;
	private LcagProperties config = null;
	private File masterDirecory = null;

	public FileActions() {
		setTitle("Compare 2 Excel Files");
		setSize(800, FRAME_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel mainPanel = new JPanel(new BorderLayout());
		JPanel leftButtonPanel = new JPanel(new GridLayout(3, 1, 5, 5));
		JPanel rightTextPanel = new JPanel(new GridLayout(3, 1, 5, 5));
		// setLayout(new GridLayout(4, 2));

		leftButtonPanel.setPreferredSize(new Dimension(200, FRAME_HEIGHT)); // Fixed width

		// Add an empty border to create a gap on the right side of leftPanel
		leftButtonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10)); // right gap: 10px

		rightTextPanel.setPreferredSize(new Dimension(500, FRAME_HEIGHT));
		mainPanel.add(leftButtonPanel, BorderLayout.WEST);
		mainPanel.add(rightTextPanel, BorderLayout.EAST);

		configFileLabel = new JLabel(String.format(" %s: Not selected", CONFIG_FILE_LABEL));

		selectConfigFile = new JButton("Select " + CONFIG_FILE_LABEL);
		leftButtonPanel.add(selectConfigFile);
		rightTextPanel.add(configFileLabel);

		// File Chooser for config file
		selectConfigFile.setEnabled(true);
		selectConfigFile.addActionListener(e -> {
			if (!configFileDirectoryPath.isDirectory()) {
				System.err.println(String.format("ERROR: Config directory %s does not exist",
						configFileDirectoryPath.getAbsolutePath()));
				System.exit(1);
			}

			JFileChooser fileChooser = new JFileChooser(configFileDirectoryPath);
			FileNameExtensionFilter filter = new FileNameExtensionFilter("Config Files", "properties");
			fileChooser.setFileFilter(filter);
			if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				String clientDirString = null;

				configFile = selectFile(fileChooser);
				config = new LcagProperties(configFile.toString());

				configFileLabel.setText(String.format(" %s: %s", CONFIG_FILE_LABEL, configFile));

				masterFile = config.getOptionalFilePathProperty("main_file");
				clientFile = config.getOptionalFilePathProperty("compare_file");

				if (clientFile.isAbsolute()) {
					clientDirString = clientFile.getParent().toString();
				} else {
					// client file is relative expect it is same directory as master
					Path dirPath = masterFile.getParent();

					clientDirString = "same directory as 1st file";
					clientFile = dirPath.resolve(clientFile);
				}

				// After user selected a config file enable buttons to set files to compare (can
				// overwrite files listed in config file):
				selectMasterFile.setEnabled(true);

				if (clientFile != null) {
					String fileName = clientFile.getFileName().toString();
					clientFileLabel
							.setText(String.format("<html>Selected: %s<br>in: %s</html>", fileName, clientDirString));
					selectClientFile.setEnabled(true);
				}

				if (masterFile != null) {
					String fileName = masterFile.getFileName().toString();
					masterDirecory = new File(masterFile.getParent().toString());
					masterFileLabel.setText(String.format("<html>Selected: %s<br>in: %s</html>", fileName,
							masterFile.getParent().toString()));
					selectClientFile.setEnabled(true);
				}
			}
		});

		// Kontextmenüs erstellen
		contextMenuMasterFile = new JPopupMenu();
		contextMenuClientFile = new JPopupMenu();
		JMenuItem renameFile1 = new JMenuItem("Rename File");
		JMenuItem renameFile2 = new JMenuItem("Rename File");
		contextMenuMasterFile.add(renameFile1);
		contextMenuClientFile.add(renameFile2);

		masterFileLabel = new JLabel(String.format(" %s: %s", MASTER_FILE_LABEL,
				masterFile == null ? "Not selected" : masterFile.getFileName()));

		selectMasterFile = new JButton("Select " + MASTER_FILE_LABEL);
		leftButtonPanel.add(selectMasterFile);
		rightTextPanel.add(masterFileLabel);

		clientFileLabel = new JLabel(String.format(" %s: %s", "Excel to compare",
				clientFile == null ? "Not selected" : clientFile.getFileName()));

		selectClientFile = new JButton("Select Excel to Compare");

		// MouseListener für Kontextmenü auf den Labels
		masterFileLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e) && masterFile != null) {
					contextMenuMasterFile.show(masterFileLabel, e.getX(), e.getY());
				}
			}
		});
		clientFileLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e) && clientFile != null) {
					contextMenuClientFile.show(clientFileLabel, e.getX(), e.getY());
				}
			}
		});

		leftButtonPanel.add(selectClientFile);
		rightTextPanel.add(clientFileLabel);

		renameFile1.addActionListener(e -> renameFile(masterFile, masterFileLabel, "Datei 1"));
		renameFile2.addActionListener(e -> renameFile(clientFile, clientFileLabel, "Datei 2"));

		// File Chooser für Datei 1 (nur Excel-Dateien)
		selectMasterFile.setEnabled(false);
		selectMasterFile.addActionListener(e -> {
			JFileChooser fileChooser = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Files", "xls", "xlsx");
			fileChooser.setFileFilter(filter);
			if (masterFile != null) {
				masterDirecory = new File(masterFile.getParent().toString());
				fileChooser.setCurrentDirectory(masterDirecory);
			}
			if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				masterFile = selectFile(fileChooser);
				masterDirecory = new File(masterFile.getParent().toString());
				masterFileLabel.setText(String.format(" %s: %s", MASTER_FILE_LABEL, masterFile.getFileName()));
				selectClientFile.setEnabled(true);
			}
		});

		// File Chooser für Datei 2 (im selben Verzeichnis wie Datei 1, nur
		// Excel-Dateien)
		selectClientFile.setEnabled(false);
		selectClientFile.addActionListener(e -> {
			if (masterDirecory != null) {
				JFileChooser fileChooser = new JFileChooser(masterDirecory);
				FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
				fileChooser.setFileFilter(filter);
				if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
					clientFile = selectFile(fileChooser);
					clientFileLabel.setText("Datei 2: " + clientFile.getFileName());
				}
			}
		});

		// Menü erstellen
		menuBar = new JMenuBar();
		actionsMenu = new JMenu("Actions");
		mergeAction = new JMenuItem("Merge 2nd into Main file");

		mergeAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
		mergeAction.addActionListener(e -> {
			System.out.println("Ctrl-C pressed on menu");
		});

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

		// Aktion: Erste Datei in Excel öffnen
		mergeAction.addActionListener(e -> {
			mergeFiles(config, masterFile, clientFile);
		});

		add(mainPanel);
		pack();
		setVisible(true);
	}

	private Path selectFile(JFileChooser fileChooser) {
		File result = fileChooser.getSelectedFile();
		return Path.of(result.getAbsolutePath());
	}

	private void mergeFiles(LcagProperties pConfig, Path pMasterFilePath, Path pClientFilePath) {
		if (pConfig != null) {
			String keyFieldString = pConfig.getProperty("list_of_keys");

			List<String> keyFields = LcagStringTools.asList(keyFieldString, ",\\s*");

			if (pMasterFilePath != null && pClientFilePath != null)
				try {
					String masterFileName = pMasterFilePath.getFileName().toString();
					Path resultFilePath = LcagFileTools.appendStringToFilePath(pMasterFilePath, "-merged");
					String fieldMapping = pConfig.getProperty("column.mapping.main2compare");

					Files.copy(pMasterFilePath, resultFilePath, StandardCopyOption.REPLACE_EXISTING);

					ExcelWorkbook excel1 = new ExcelWorkbook(resultFilePath.toString());
					ExcelWorkbook excel2 = new ExcelWorkbook(pClientFilePath.toString());

					excel1.setKeyColumns(keyFields);
					excel1.read();
					excel2.setKeyColumns(keyFields);
					excel2.read();

					excel1.setOption(keyFieldString, FRAME_HEIGHT);

					if (fieldMapping != null) {
						MappingTable mappingTable = new MappingTable(fieldMapping, ";", "->");

						excel2.setOption(Table.OPTION_USE_MAPPING, mappingTable);
					}

					WorkbookComparisonResult compareResult = excel1.compareTo(excel2);
					
					if (compareResult.tablesAreEquals()) {
						System.out.printf("RESULT: Tables %s and %s are equal.\n", resultFilePath, pClientFilePath);
					} else {
						ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster();
	
						integratedTable.save();
	
						Desktop.getDesktop().open(resultFilePath.toFile());
					}
				} catch (IOException ex) {
					JOptionPane.showMessageDialog(this, "Cannot open comparison result file.", "Error",
							JOptionPane.ERROR_MESSAGE);
				} catch (ConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		} else {
			JOptionPane.showMessageDialog(this, "Pls select at least a config file!", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private Path getPath(LcagProperties config, String pProperty, File pMasterFile) {
		Path result = null;
		String filePathString = (pMasterFile == null) ? config.getProperty(pProperty) : pMasterFile.getAbsolutePath();

		if (!LcagStringTools.isEmpty(filePathString)) {
			result = Path.of(filePathString);
		}

		return result;
	}

	private Path renameFile(Path pFile, JLabel label, String prefix) {
		Path newPath = null;
		if (pFile == null)
			return newPath;

		File file = new File(pFile.toString());
		String newName = JOptionPane.showInputDialog(this, "Enter new name:", file.getName());

		if (!LcagStringTools.isEmpty(newName)) {
			newPath = pFile.getParent().resolve(newName);

			File newFile = new File(newPath.toString());
			if (file.renameTo(newFile)) {
				file = newFile;
				label.setText(prefix + ": " + newFile.getName());
				if (prefix.equals("Datei 1"))
					this.masterFile = newPath;
				else
					this.clientFile = newPath;
			} else {
				JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE);
			}
		}

		return newPath;
	}

	public static void main(String[] args) {
		String cfgFileDirectory = args.length < 1 ? System.getenv("_MY_DATA") + "/Tools" : args[0];

		SwingUtilities.invokeLater(() -> {
			FileActions gui = new FileActions();

			gui.configFileDirectoryPath = new File(cfgFileDirectory);
			gui.setVisible(true);
		});
	}
}
