package ld5_34;

import javax.swing.*;

import java.awt.*;
import jade.gui.GuiEvent;

public class GuiLauncherGUI extends JFrame {
    private GuiLauncher myAgent;
    private JTextField nameField, classField;
    private JLabel statusLabel;

    public GuiLauncherGUI(GuiLauncher a) {
        statusLabel = new JLabel("Status: Waiting...", SwingConstants.CENTER);
        add(statusLabel);

        myAgent = a;
        setTitle("Agent controller");
        setSize(600, 200);
        setLayout(new GridLayout(5, 1));

        JPanel p1 = new JPanel();
        p1.add(new JLabel("Name:"));
        nameField = new JTextField("Agent1", 15);
        p1.add(nameField);
        add(p1); 

        JPanel p2 = new JPanel();
        p2.add(new JLabel("Agent class:"));
        classField = new JTextField("Simple", 15);
        p2.add(classField);
        add(p2);

        JButton btnStart = new JButton("Start");
        btnStart.addActionListener(e -> triggerEvent(1));

        JButton btnKill = new JButton("Kill");
        btnKill.addActionListener(e -> triggerEvent(2));

        JButton btnEnd = new JButton("End");
        btnEnd.addActionListener(e -> triggerEvent(3));


        JPanel buttons = new JPanel();
        buttons.add(btnStart); buttons.add(btnKill); buttons.add(btnEnd);
        add(buttons);
        // add(btnStart);
        // add(btnKill);
        // add(btnEnd);
        
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void triggerEvent(int type) {
        GuiEvent ge = new GuiEvent(this, type);
        ge.addParameter(classField.getText());
        ge.addParameter(nameField.getText());
        myAgent.postGuiEvent(ge);
    }

    public void updateStatus(String msg) {
        statusLabel.setText("Status: " + msg);
    }
}