package application;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import de.lcag.common.CsvTable;

public class ShowDevelopmentHints extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private DefaultTableModel model;
	private String gitBranchName = null;

	CsvTable hints = new CsvTable(CommentExtractor.DOC_FILE + ".csv");
	Object[] columnNames = null;
	List<Object[]> data = new ArrayList<>();

	public ShowDevelopmentHints() throws Exception {
		setTitle("Tabelle mit externem Programm");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 300);
		setLocationRelativeTo(null);

		gitBranchName = CommentExtractor.getCurrentGitBranch();

		createDataTable();

		// Hinzufügen des MouseListeners für Zellklicks
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					int column = table.columnAtPoint(e.getPoint());
					String colName = (String) columnNames[column];

					if (row >= 0 && "LINENO".equals(colName)) {
						String filePath = (String) table.getValueAt(row, column - 1);
						int lineNo = Integer.parseInt((String) table.getValueAt(row, column));
						String gitURL = String.format(CommentExtractor.CODE_BASE_GITHUB_DIR, filePath, gitBranchName,
								lineNo, lineNo + 1);

						runExternalProgram(gitURL);
					}
				}
			}
		});

		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
	}

	private void createDataTable() throws IOException {
		hints.read();
		columnNames = hints.getColumnLabels().toArray();

		final int noRows = hints.lenght();

		model = new DefaultTableModel(columnNames, 0);

		for (int i = 0; i < noRows; i++) {
			List<Object> valuesInRow = new ArrayList<>();
			String filePath = null;

			for (Object col : columnNames) {
				final Object cellValue = hints.getCellValue(i, (String) col);

				valuesInRow.add(cellValue);
			}

			model.addRow(valuesInRow.toArray());
		}

		table = new JTable(model);
	}

	private void runExternalProgram(String value) {
		// Does not work: Opens a new Eclipse.
		// String prg = "eclipsec.exe -name Eclipse --launcher.openFile
		// %USERPROFILE%\\workspace\\MyClass.java:50";
		String prg = "notepad++ <filePath> -n50";
		Desktop desktop = Desktop.getDesktop();
		try {
			// Open the URL in the default browser
			desktop.browse(new URI(value));
		} catch (IOException | URISyntaxException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> {
			ShowDevelopmentHints frame;
			try {
				frame = new ShowDevelopmentHints();
				frame.setVisible(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
	}
}
