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

package com.elex_project.examples.components.list;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ListExample extends JPanel {
	@AllArgsConstructor
	@Getter
	@ToString
	public static class Person {
		private String firstName, lastName;
		private int age;
		private String address;
	}

	public static class PersonRenderer extends JPanel implements ListCellRenderer<Person> {
		private final JLabel lblName;
		private final JLabel lblAddress;

		PersonRenderer() {
			super();
			setLayout(new BorderLayout());
			lblName = new JLabel();
			lblAddress = new JLabel();

			add(lblName, BorderLayout.NORTH);
			add(lblAddress, BorderLayout.SOUTH);

			lblName.setFont(lblName.getFont().deriveFont(Font.BOLD));
			setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
		}

		@Override
		public Component getListCellRendererComponent(JList<? extends Person> jList, Person item, int index,
		                                              boolean isSelected, boolean hasFocus) {
			// 선택된 항목의 색상을 변경해주고,
			if (isSelected) {
				setBackground(jList.getSelectionBackground());
				setForeground(jList.getSelectionForeground());
			} else {
				setBackground(jList.getBackground());
				setForeground(jList.getForeground());
			}
			// 선택된 항목의 값을 할당해 준다.
			lblAddress.setText(item.getAddress());
			lblName.setText(item.getFirstName() + " " + item.getLastName());
			return this;
		}
	}

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

		final DefaultListModel<Person> listModel = new DefaultListModel<>();
		listModel.addElement(new Person("Charlie", "Brown", 13, "Snoopy house"));
		listModel.addElement(new Person("Steve", "McQueen", 24, "Garage"));


		final JList<Person> jList = new JList<>(listModel);
		jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jList.setLayoutOrientation(JList.VERTICAL);
		jList.setVisibleRowCount(-1);
		jList.setCellRenderer(new PersonRenderer());
		jList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				if (!listSelectionEvent.getValueIsAdjusting()) {
					System.out.println(jList.getSelectedValue());
				}
			}
		});


		final JButton addButton = new JButton("Add");
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				int index = jList.getSelectedIndex();
				if (index == -1) index = 0;
				else index++;
				listModel.insertElementAt(
						new Person("Soohanmoo", "Kim", 543, "Unknown"), index);

				jList.setSelectedIndex(index);
				jList.ensureIndexIsVisible(index);
			}
		});
		this.add(new JScrollPane(jList), BorderLayout.CENTER);
		this.add(addButton, BorderLayout.NORTH);


	}

	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 ListExample());

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


	}
}
