package MVC_Desighn;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

// modell
class Person {
    private String name;
    private int age;

    // Konstruktor
    public Person(String name, int age) throws IllegalArgumentException {
        this.setName(name);
        this.setAge(age);
    }

    // Getter für Name
    public String getName() {
        return name;
    }

    // Setter für Name
    public void setName(String name) {
        this.name = name;
    }

    // Getter für Alter
    public int getAge() {
        return age;
    }

    // Setter für Alter
    public void setAge(int age) throws IllegalArgumentException {
        if (age <= 18) {
            throw new IllegalArgumentException("Das Alter muss größer als 18 sein.");
        }
        this.age = age;
    }
    
    
}

//view
public class Person_GUI {
	private JFrame frame;
	private JTextField nameTextField;
	private JButton updateButton;
	private JLabel nameLabel;
	private JLabel ageLabel;

	public Person_GUI() {
        // Setup des Frames
        frame = new JFrame("Person Details");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setLayout(null);

        // Name Label
        nameLabel = new JLabel("Name:");
        nameLabel.setBounds(10, 20, 80, 25);
        frame.add(nameLabel);

        // Age Label
        ageLabel = new JLabel("Alter:");
        ageLabel.setBounds(10, 50, 80, 25);
        frame.add(ageLabel);

        // Name Textfeld
        nameTextField = new JTextField(20);
        nameTextField.setBounds(100, 20, 165, 25);
        frame.add(nameTextField);

        // Update Button
        updateButton = new JButton("Name ändern");
        updateButton.setBounds(100, 80, 165, 25);
        frame.add(updateButton);

        // Frame sichtbar machen
        frame.setVisible(true);
    }

	public String getNameInput() {
		return nameTextField.getText();
	}

	public void setPersonDetails(String name, int age) {
		nameLabel.setText("Name: " + name);
		ageLabel.setText("Alter: " + age);
	}

	public void addUpdateButtonListener(ActionListener listener) {
		updateButton.addActionListener(listener);
	}
	
	public static void main(String[] args) {
        
        // Erstellen des Modells
        Person model = new Person("Max Mustermann", 20);

        // Erstellen der Ansicht
        Person_GUI view = new MVC_Desighn.Person_GUI();

        // Erstellen des Controllers
        PersonController controller = new PersonController(model, view);

  
}
}

class PersonController {
	private Person model;
	private Person_GUI view;

	public PersonController(Person model, Person_GUI view) {
		this.model = model;
		this.view = view;

		// Initialisieren der View mit den aktuellen Modelldaten
		this.view.setPersonDetails(model.getName(), model.getAge());

		// Hinzufügen eines ActionListeners zum Button
		this.view.addUpdateButtonListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String newName = view.getNameInput();
				setPersonName(newName);
				updateView();
			}
		});
	}

	public void setPersonName(String name) {
		model.setName(name);
	}

	public String getPersonName() {
		return model.getName();
	}

	public void setPersonAge(int age) {
		model.setAge(age);
	}

	public int getPersonAge() {
		return model.getAge();
	}

	public void updateView() {
		view.setPersonDetails(model.getName(), model.getAge());
	}
}
