/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package com.elex_project.examples.components.table;

import lombok.AllArgsConstructor;
import lombok.Getter;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Table2Example extends JPanel {

	public Table2Example() {
		super(new BorderLayout());


		final JTable table = new JTable(new MyTableModel());
		table.setFillsViewportHeight(true);
		table.setRowSelectionAllowed(true);
		table.getColumnModel().getColumn(0).setPreferredWidth(80);
		table.getColumnModel().getColumn(1).setPreferredWidth(40);
		table.getColumnModel().getColumn(2).setPreferredWidth(180);

		table.getModel().addTableModelListener(new TableModelListener() {
			@Override
			public void tableChanged(TableModelEvent tableModelEvent) {
				int row = tableModelEvent.getFirstRow();
				int column = tableModelEvent.getColumn();
				TableModel model = (TableModel)tableModelEvent.getSource();
				System.out.println(model.getValueAt(row,column));
			}
		});

		table.setDefaultRenderer(String.class, new MyTableRenderer());

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

	@Getter
	@AllArgsConstructor
	private static class Person {
		private String name;
		private int age;
		private String address;
	}

	private static class MyTableModel extends AbstractTableModel {
		final java.util.List<String> header = Arrays.asList("이름", "나이", "주소");
		final java.util.List<Person> data = new ArrayList<>();

		MyTableModel() {
			super();
			data.add(new Person("Charlie", 13, "서울"));
			data.add(new Person("Steve", 34, "서울"));
			data.add(new Person("Jane", 23, "서울"));
			data.add(new Person("Jack", 48, "서울"));
		}

		@Override
		public int getRowCount() {
			return data.size();
		}

		@Override
		public int getColumnCount() {
			return header.size();
		}

		@Override
		public Object getValueAt(int row, int column) {
			Person person = data.get(row);
			switch (column) {
				case 0:
					return person.name;
				case 1:
					return person.age;
				case 2:
					return person.address;
			}
			return "";
		}

		@Override
		public String getColumnName(int column) {
			return header.get(column);
		}

		@Override
		public Class<?> getColumnClass(int column) {
			switch (column) {
				case 0:
					return String.class;
				case 1:
					return Integer.class;
				case 2:
					return String.class;
			}
			return Object.class;
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return columnIndex == 2;
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			Person person = data.get(rowIndex);
			switch (columnIndex) {
				case 0:
					person.name = (String) aValue;
					return;
				case 1:
					person.age = (Integer) aValue;
					return;
				case 2:
					person.address = (String) aValue;
					return;
			}
			fireTableCellUpdated(rowIndex,columnIndex);
		}
	}

	private static class MyTableRenderer extends JPanel implements TableCellRenderer {
		private JLabel jLabel;
		MyTableRenderer(){
			super();
			jLabel = new JLabel();
			add(jLabel, BorderLayout.CENTER);
		}
		@Override
		public Component getTableCellRendererComponent(JTable jTable, Object value,
		                                               boolean isSelected, boolean haFocus, int row, int column) {
			//do something
			return this;
		}
	}
	public static void main(String... args) {

		final JFrame jFrame = new JFrame();
		jFrame.setTitle("Example");
		jFrame.setSize(800, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setContentPane(new Table2Example());

		SwingUtilities.invokeLater(() -> {
			jFrame.setVisible(true);
		});

	}


}
