package jnet.client.gui;

import jnet.client.gui.dialog.LoginDialog;
import jnet.client.network.NettyClient;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import jnet.lib.LogFile;

class MenuPanel extends JPanel {

    public MenuPanel() {

        setPreferredSize(new Dimension(50, 50));
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // tlacitko pripojit / odpojit
        try {
            BufferedImage buttonIcon = ImageIO.read(getClass().getResourceAsStream("/img/connect.png"));
            JButton bLogin = new JButton(new ImageIcon(buttonIcon));
            bLogin.setFocusPainted(false);
            bLogin.setContentAreaFilled(false);
            bLogin.setToolTipText("Připojit / Odpojit");
            bLogin.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (NettyClient.isConnected()) {
                        NettyClient.disconnect();
                    } else {
                        new LoginDialog().setVisible(true);
                    }
                }
            });
            add(bLogin);
        } catch (IOException ex) {
            LogFile.printErr("File not found: image connect.png");
        }

        // tlacitko nastaveni
        try {
            BufferedImage buttonIcon = ImageIO.read(getClass().getResourceAsStream("/img/settings.png"));
            JButton bSettings = new JButton(new ImageIcon(buttonIcon));
            bSettings.setFocusPainted(false);
            bSettings.setContentAreaFilled(false);
            bSettings.setToolTipText("Nastavení programu");
            bSettings.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Window.showSettings();
                }
            });
            add(bSettings);
        } catch (IOException ex) {
            LogFile.printErr("File not found: image settings.png");
        }

        // tlacitko log
        try {
            BufferedImage buttonIcon = ImageIO.read(getClass().getResourceAsStream("/img/log.png"));
            JButton bLog = new JButton(new ImageIcon(buttonIcon));
            bLog.setFocusPainted(false);
            bLog.setContentAreaFilled(false);
            bLog.setToolTipText("Log událostí");
            bLog.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Window.showEvents();
                }
            });
            add(bLog);
        } catch (IOException ex) {
            LogFile.printErr("File not found: image log.png");
        }

        // tlacitko server settings
        try {
            BufferedImage buttonIcon = ImageIO.read(getClass().getResourceAsStream("/img/server_settings.png"));
            JButton bServerSettings = new JButton(new ImageIcon(buttonIcon));
            bServerSettings.setFocusPainted(false);
            bServerSettings.setContentAreaFilled(false);
            bServerSettings.setToolTipText("Nastavení serveru");
            bServerSettings.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Window.showServerSettings(ServerSettingsPanel.TAB_DEFAULT);
                }
            });
            add(bServerSettings);
        } catch (IOException ex) {
            LogFile.printErr("File not found: image server_settings.png");
        }

    }

}
