/*
 * 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.FontSize;
import br.com.ctecinf.jetty.JettyException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;

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

    public static void main(String[] args) {

        /**
         * Decoração
         */
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        FontSize.change(22);

        JMenuItem item;
        JMenuBar menuBar = new JMenuBar();

        JFrame frame = new JFrame("Race Director - hobbyslotcar.com.br");
        frame.setLayout(new BorderLayout(10, 10));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(menuBar, BorderLayout.NORTH);
        frame.add(new JLabel(Img.get("logo")), BorderLayout.CENTER);
        frame.setMinimumSize(new Dimension(800, 600));
        frame.setLocationRelativeTo(null);

        JMenu menuStart = new JMenu("Início");
        menuStart.setIcon(Img.get("home"));

        item = new JMenuItem("Conexão USB", Img.get("wired"));
        item.addActionListener((ActionEvent e) -> {
            try {
                ConfigSerialPort.choice();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        menuStart.add(item);

        item = new JMenuItem("Pilotos", Img.get("users"));
        item.addActionListener((ActionEvent e) -> {
            try {
                new ConfigDrivers().setVisible(true);
            } catch (JettyException ex) {
                JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        menuStart.add(item);

        item = new JMenuItem("Fendas", Img.get("lanes"));
        item.addActionListener((ActionEvent e) -> {
            try {
                new ConfigLanes().setVisible(true);
            } catch (JettyException ex) {
                JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        menuStart.add(item);

        item = new JMenuItem("Relé");
        item.addActionListener((ActionEvent e) -> {
            try {
                String txt = JOptionPane.showInputDialog("Valor");
                com.fazecast.jSerialComm.SerialPort s = ConfigSerialPort.get();
                try (FileOutputStream outputStream = new FileOutputStream(s.getSystemPortPath())) {
                    outputStream.write(txt.getBytes());
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        menuStart.add(item);

        menuStart.add(new JSeparator());

        item = new JMenuItem("Sair", Img.get("exit"));
        item.addActionListener((ActionEvent e) -> {
            frame.dispose();
            System.exit(0);
        });
        menuStart.add(item);

        menuBar.add(menuStart);

        frame.setVisible(true);

    }
}
