import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;

public class RegistrationFrame extends JFrame implements ActionListener {

    private StudentManager manager;

    private JTextField txtCSID;
    private JButton btnAddCS;

    private JTextField txtCompetitionID;
    private JButton btnRegister;
    private JButton btnRemove;

    private JTextArea txtComputerScienceStudents;
    private JTextArea txtCompetitionStudents;

    private JLabel lblMessage;

    public RegistrationFrame() {

        manager = new StudentManager();

        setTitle("Coding Competition Registration");
        setSize(700, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel(new GridLayout(1,1));
        //--------------------------
        // Competition Panel
        //--------------------------

        JPanel compPanel = new JPanel();

        compPanel.setBorder(
                BorderFactory.createTitledBorder(
                        "Coding Competition")
        );

        txtCompetitionID = new JTextField(10);

        btnRegister = new JButton("Register");
        btnRegister.addActionListener(this);

        btnRemove = new JButton("Remove");
        btnRemove.addActionListener(this);

        compPanel.add(new JLabel("Student ID:"));
        compPanel.add(txtCompetitionID);
        compPanel.add(btnRegister);
        compPanel.add(btnRemove);

        topPanel.add(compPanel);

        add(topPanel, BorderLayout.NORTH);

        //--------------------------
        // Text Areas
        //--------------------------

        JPanel centerPanel = new JPanel(
                new GridLayout(1,2));

        txtComputerScienceStudents =
                new JTextArea();

        txtCompetitionStudents =
                new JTextArea();

        txtComputerScienceStudents.setEditable(false);
        txtCompetitionStudents.setEditable(false);

        centerPanel.add(
                new JScrollPane(
                        txtComputerScienceStudents));

        centerPanel.add(
                new JScrollPane(
                        txtCompetitionStudents));

        add(centerPanel, BorderLayout.CENTER);

        //--------------------------
        // Status Label
        //--------------------------

        lblMessage = new JLabel(" ");

        add(lblMessage, BorderLayout.SOUTH);

        refreshLists();

        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String id;
        //---------------------------------
        // Register Competition
        //---------------------------------

        if(e.getSource() == btnRegister) {

            id = txtCompetitionID.getText()
                    .trim();

            if(id.isEmpty()) {
                return;
            }

            String result  = manager.registerCompetitionStudent(id);

            lblMessage.setText(result);

            txtCompetitionID.setText("");
        }

        //---------------------------------
        // Remove Competition Student
        //---------------------------------

        if(e.getSource() == btnRemove) {

            id = txtCompetitionID.getText().trim();

            String result = manager.removeCompetitionStudent(id);

            lblMessage.setText(result);

            txtCompetitionID.setText("");

        }

        refreshLists();

    }

    private void refreshLists() {

        txtComputerScienceStudents.setText(
                "Computer Science Students\n\n");

        for(String id : manager.getComputerScienceStudents()) {

            txtComputerScienceStudents.append(
                    id + "\n");

        }

        txtCompetitionStudents.setText(
                "Competition Students\n\n");

        for(String id :
                manager.getCompetitionStudents()) {

            txtCompetitionStudents.append(
                    id + "\n");

        }

    }

}