/*
 * Copyright (C) 2025 ctecinf.com.br
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.com.hobbyslotcar.racedirector;

import br.com.ctecinf.jetty.Config;
import br.com.ctecinf.jetty.JettyException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

/**
 *
 * @author Cássio Conceição
 * @since 07/03/2025
 * @version 2503
 * @see http://hobbyslotcar.com.br
 */
public class ConfigDrivers extends JDialog {

    protected static Config CONFIG = new Config() {
        @Override
        public String getFileName() {
            return "drivers";
        }
    };

    public ConfigDrivers() throws JettyException {

        setTitle("Configuração de pilotos");

        List drivers = new List(CONFIG.keySet());

        JLabel label = new JLabel("Pilotos", Img.get("avatar"), SwingConstants.CENTER);

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        JButton button = new JButton("Adicionar", Img.get("list-add"));
        button.addActionListener((ActionEvent e) -> {
            try {
                String name = JOptionPane.showInputDialog(null, "Nome do piloto", "Adicionar Piloto", JOptionPane.INFORMATION_MESSAGE);
                if (name != null) {
                    CONFIG.add(name, "");
                    drivers.addElement(name);
                }
            } catch (JettyException ex) {
                JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        panel.add(button);

        button = new JButton("Remover", Img.get("list-remove"));
        button.addActionListener((ActionEvent e) -> {
            try {
                if (drivers.getSelectedIndex() == -1) {
                    JOptionPane.showMessageDialog(null, "Nenhum piloto selecionado!", "Aviso", JOptionPane.WARNING_MESSAGE);
                } else {
                    String name = drivers.getSelectedValue().toString();
                    CONFIG.remove(name);
                    drivers.removeElement(name);
                }
            } catch (JettyException ex) {
                JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        panel.add(button);

        button = new JButton("Fechar", Img.get("window-close"));
        button.addActionListener((ActionEvent e) -> {
            dispose();
        });
        panel.add(button);

        setLayout(new BorderLayout(10, 10));
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        add(label, BorderLayout.NORTH);
        add(new JScrollPane(drivers), BorderLayout.CENTER);
        add(panel, BorderLayout.SOUTH);
        setMinimumSize(new Dimension(600, 600));
        setLocationRelativeTo(null);
    }

}
