package jguard.client.gui.dialog;

import cucky.jGuard.lib.LogFile;
import cucky.jGuard.lib.Message;
import cucky.jGuard.lib.object.ObjectType;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Base64;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import jguard.client.Client;
import jguard.client.gui.ServerSettingsPanel;
import jguard.client.gui.Window;
import jguard.client.network.NetworkClient;
import org.apache.commons.io.FilenameUtils;

public class AddDeviceType extends JDialog {
    
    private JLabel info = new JLabel("PNG obrazek o šířce 32px");
    private JTextField nazevText = new JTextField();
    private String img_str = "";

    public AddDeviceType() {
        setTitle("Přidat typ zařízení");
        setSize(300, 180);
        setLocationRelativeTo(null);
        setAlwaysOnTop(false);
        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);

    }

    private void placeComponents(JPanel panel) {

        panel.setLayout(null);

        JLabel serverLabel = new JLabel("Název:");
        serverLabel.setBounds(20, 10, 80, 25);
        panel.add(serverLabel);

        nazevText.setBounds(110, 10, 160, 25);
        panel.add(nazevText);

        JLabel portLabel = new JLabel("Obrázek");
        portLabel.setBounds(20, 40, 80, 25);
        panel.add(portLabel);

        JButton btnBrowse = new JButton("Browse");
        btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                browseButtonActionPerformed(e);
            }
        });
        btnBrowse.setBounds(110, 40, 160, 25);
        panel.add(btnBrowse);
        
        info.setBounds(20, 70, 200, 25);
        panel.add(info);

        JButton addButton = new JButton("Přidat");
        addButton.setBounds(20, 100, 90, 25);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (nazevText.getText().length() > 3) {
                    ObjectType ot = new ObjectType(nazevText.getText(), img_str);
                    Client.client.send(Message.ADD_OBJECT_TYPE, ot);
                    // refresh tabulky
                    //TODO
                    Window.showServerSettings();
                    System.out.println(".actionPerformed()");
                    // zavreni dialogu
                    dispose();
                } else {
                    info.setForeground(Color.RED);
                    info.setText("Název je moc krátký");
                }
                
            }
        });
        panel.add(addButton);

        JButton closeButton = new JButton("Storno");
        closeButton.setBounds(185, 100, 80, 25);
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        panel.add(closeButton);

    }

    private void browseButtonActionPerformed(ActionEvent evt) {
        JFileChooser fc = new JFileChooser(".");
        fc.addChoosableFileFilter(new FileNameExtensionFilter("Image Files PNG", "png"));

        int res = fc.showOpenDialog(null);
        try {
            if (res == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String ext = "png";
                // TODO doresit overeni správného typu souboru
                //String ext = FilenameUtils.getExtension("C:\\Users\\Michal\\Documents\\NetBeansProjects\\jGuardClient\\img\\app.png"); // returns "exe"
                if (ext.equals("png")) {
                    Image image = ImageIO.read(file);
                            BufferedImage bimg = ImageIO.read(file);
                    if (bimg.getWidth() == 32) {
                        info.setText("");
                        img_str = encodeFileToBase64Binary(file);
                    } else {
                        info.setForeground(Color.RED);
                        info.setText("Chybný rozměr obrázku");
                    }
                } else {
                    info.setForeground(Color.RED);
                    info.setText("Chybný formát obrázku");
                }
                
            }
        } catch (Exception e) {
            LogFile.printErr("File choose exception: " + e.getMessage());
        }

    }

    private static String encodeFileToBase64Binary(File file) throws Exception {
        FileInputStream fileInputStreamReader = new FileInputStream(file);
        byte[] bytes = new byte[(int) file.length()];
        fileInputStreamReader.read(bytes);
        return new String(Base64.getEncoder().encode(bytes), "UTF-8");
    }

}
