package com.mortals.xhx.swing;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

@Slf4j
public class MyTray {

    private static MyTray instance = null;
    String filePath = "/root/mid.prop";
    String siteUrl = "http://192.168.0.98:8090";

    public MyTray() {
    }

    public static MyTray getInstance() {
        if (null == instance) {
            synchronized (MyTray.class) {
                if (null == instance) {
                    instance = new MyTray();
                }
            }
        }

        return instance;
    }

    //初始化菜单
    public void SystemTrayInitial(String path,String url) {
        filePath = path;
        siteUrl = url;

        if (!SystemTray.isSupported()) {//判断系统是否支持托盘
            log.info("系统不支持托盘");
            return;
        }
        try {
            String title = "tts语音服务";//系统栏通知标题
            String company = "";//系统通知栏内容
            SystemTray systemTray = SystemTray.getSystemTray();//获取系统默认托盘

            ClassPathResource classPathResource = new ClassPathResource("img/icon.png");
            Image image = ImageIO.read(classPathResource.getInputStream());
//            Image image = Toolkit.getDefaultToolkit().getImage("img/icon.png");


            // Image image = Toolkit.getDefaultToolkit().getImage(absolutePath);//系统栏图标
            TrayIcon trayIcon = new TrayIcon(image, title + "\n" + company, createMenu());//添加图标,标题,内容,菜单
            trayIcon.setImageAutoSize(true);//设置图像自适应
//            trayIcon.addMouseListener(new MouseAdapter() {
//                @Override
//                public void mouseClicked(MouseEvent e) {
//                    super.mouseClicked(e);
//                    if(e.getClickCount() == 2){  //双击操作
//                    }
//                }
//            });
            systemTray.add(trayIcon);//添加托盘
            trayIcon.displayMessage(title, company, TrayIcon.MessageType.INFO);//弹出一个info级别消息框
        } catch (Exception e) {
            log.error("系统托盘图标加载失败", e);
        }
    }

    //托盘中的菜单
    private  PopupMenu createMenu() {
        PopupMenu menu = new PopupMenu();//创建弹出式菜单
        MenuItem exitItem = new MenuItem("exit");//创建菜单项
        exitItem.addActionListener(new ActionListener() {//给菜单项添加事件监听器，单击时退出系统
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        MenuItem settingItem = new MenuItem("setting");//创建菜单项
        settingItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                SetWindow.getInstance().initWindow(filePath,siteUrl);
            }
        });

        menu.add(settingItem);
        menu.add(exitItem);//添加退出系统菜单
        return menu;
    }


    public static void main(String[] args) {
        new MyTray().SystemTrayInitial(getInstance().filePath, getInstance().siteUrl);
    }
}