/*
 * 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.io.IOException;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

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

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

    public ConfigSerialPort() {
        
        setTitle("Configuração do cabo USB");
    }

    
    
    public static com.fazecast.jSerialComm.SerialPort choice() throws IOException {
        com.fazecast.jSerialComm.SerialPort[] commPorts = com.fazecast.jSerialComm.SerialPort.getCommPorts();
        if (commPorts.length > 0) {
            if (commPorts.length == 1) {
                JOptionPane.showMessageDialog(null, "Cabo USB da telemetria conectado com sucesso na porta " + commPorts[0].getSystemPortName() + "!", "Porta Serial", JOptionPane.INFORMATION_MESSAGE);
                try {
                    CONFIG.clear();
                    CONFIG.add(commPorts[0].getSystemPortName(), commPorts[0].getSystemPortPath());
                } catch (JettyException ex) {
                    throw new IOException(ex.getMessage());
                }
                return commPorts[0];
            } else {
                com.fazecast.jSerialComm.SerialPort commPort = (com.fazecast.jSerialComm.SerialPort) JOptionPane.showInputDialog(null, "Selecione a porta Serial do cabo USB da Telemetria", "Porta Serial", JOptionPane.INFORMATION_MESSAGE, null, commPorts, null);
                if (commPort != null) {
                    try {
                        CONFIG.clear();
                        CONFIG.add(commPort.getSystemPortName(), commPorts[0].getSystemPortPath());
                    } catch (JettyException ex) {
                        throw new IOException(ex.getMessage());
                    }
                }
                return commPort;
            }
        } else {
            throw new IOException("Cabo USB da telemetria não está conectado ao computador.");
        }
    }

    public static com.fazecast.jSerialComm.SerialPort get() throws IOException {
        if (CONFIG.isEmpty()) {
            ConfigSerialPort.choice();
        }
        com.fazecast.jSerialComm.SerialPort serialPort = com.fazecast.jSerialComm.SerialPort.getCommPort((String) CONFIG.values().iterator().next());
        serialPort.setComPortParameters(serialPort.getBaudRate(), serialPort.getNumDataBits(), com.fazecast.jSerialComm.SerialPort.ONE_STOP_BIT, com.fazecast.jSerialComm.SerialPort.NO_PARITY);
        serialPort.setComPortTimeouts(com.fazecast.jSerialComm.SerialPort.TIMEOUT_WRITE_BLOCKING, com.fazecast.jSerialComm.SerialPort.LISTENING_EVENT_TIMED_OUT, com.fazecast.jSerialComm.SerialPort.LISTENING_EVENT_TIMED_OUT);
        return serialPort;
    }
}
