package jguard.client.gui;

import cucky.jGuard.lib.LogFile;
import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
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 jguard.client.Client;
import jguard.client.network.NetworkClient;

public class Tray {

    private SystemTray tray = SystemTray.getSystemTray();
    private static TrayIcon trayIcon;
    private static PopupMenu menu = new PopupMenu();

    public void createAndShowTray() {
        try {
            
            //Check the SystemTray is supported
            if (!SystemTray.isSupported()) {
                LogFile.printErr("SystemTray is not supported");
                return;
            }
            
            // tray menu
            MenuItem exitItem = new MenuItem("Ukončit");
            exitItem.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        LogFile.printInfo("Exit appliaction from tray icon");
                        System.exit(0);
                    }
            });
            menu.add(exitItem);

            trayIcon = new TrayIcon(ImageIO.read(new File("img/flag_gray.png")), Client.APP_NAME, menu);
            trayIcon.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed( ActionEvent e ) {
                    //Double click - obnovit/minimalizovat
                    if (Client.okno.isVisible()) {
                        Client.okno.setVisible(false);
                    } else {
                        Client.okno.setVisible(true);
                    }
                }}
            );
            
            tray = SystemTray.getSystemTray();
            tray.add(trayIcon);

        } catch (IOException ex) {
            LogFile.printErr("TrayIcon error: " + ex.getMessage());
        } catch (AWTException ex) {
            LogFile.printErr("TrayIcon error: " + ex.getMessage());
        }
    }

    public static void setIcon(String icon) {
        try {
            BufferedImage image = ImageIO.read(new File("img/"+icon));
            trayIcon.setImage(image);
        } catch (IOException ex) {
            LogFile.printErr("TrayIcon error: " + ex.getMessage());
        }

    }
    
    
    public static void setStatusOffline() {
        setIcon("flag_red.png");
    }
    
    public static void setStatusOnline() {
        setIcon("flag_green.png");
    }
    
    public static void setStatusWarning() {
        setIcon("flag_orange.png");
    }
    
    public static void setStatusDisconnected() {
        setIcon("flag_gray.png");
    }
    
    public static void setState() {
        if (Client.offline > 0) {
            setStatusOffline();
        } else {
            setStatusOnline();
        }
    }

}
