package jguard.client.gui;

import cucky.jGuard.lib.LogFile;
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 jguard.client.Client;
import jguard.client.gui.dialog.LoginDialog;

class MenuPanel extends JPanel {

    public MenuPanel() {

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

        // tlacitko pripojit 
        // TODO pokud je pripojeno změnit tlacítko na odpojit 
        try {
            BufferedImage buttonIcon = ImageIO.read(new File("img/connect.png"));
            JButton bLogin = new JButton(new ImageIcon(buttonIcon));
            bLogin.setFocusPainted(false);
            bLogin.setContentAreaFilled(false);
            bLogin.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (Client.client.isConnected()) {
                        int reply = JOptionPane.showConfirmDialog(null, "Odpojit od serveru ?", "Odpojit", JOptionPane.YES_NO_OPTION);
                        if (reply == JOptionPane.YES_OPTION) {
                            // odpojeni od serveru
                            Client.client.disconnect();
                        }
                    } else {
                        new LoginDialog().setVisible(true);
                    }
                }
            });
            add(bLogin);
        } catch (IOException ex) {
            LogFile.printErr("File not found: image connect.png");
        }

        // tlacitko nastaveni serveru
        try {
            BufferedImage buttonIcon = ImageIO.read(new File("img/server.png"));
            JButton bSettings = new JButton(new ImageIcon(buttonIcon));
            bSettings.setFocusPainted(false);
            bSettings.setContentAreaFilled(false);
            bSettings.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (Client.client.isConnected()) {
                        if (Client.user.isSetServer()) { 
                            Window.showServerSettings();
                        } else {
                            JOptionPane.showMessageDialog(null, "Nedostatečné oprávnění");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Odpojeno od serveru");
                    }
                    
                }
            });
            add(bSettings);
        } catch (IOException ex) {
            LogFile.printErr("File not found: image server.png");
        }

        // tlacitko nastaveni
        try {
            BufferedImage buttonIcon = ImageIO.read(new File("img/settings.png"));
            JButton bSettings = new JButton(new ImageIcon(buttonIcon));
            bSettings.setFocusPainted(false);
            bSettings.setContentAreaFilled(false);
            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(new File("img/log.png"));
            JButton bLogin = new JButton(new ImageIcon(buttonIcon));
            bLogin.setFocusPainted(false);
            bLogin.setContentAreaFilled(false);
            bLogin.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });
            add(bLogin);
        } catch (IOException ex) {
            LogFile.printErr("File not found: image log.png");
        }

    }

}
