package org.aohe;

import com.formdev.flatlaf.FlatDarculaLaf;
import io.github.sanyarnd.applocker.AppLocker;
import lombok.extern.slf4j.Slf4j;
import org.aohe.constant.SocketEnum;
import org.aohe.daemon.WatchDog;
import org.aohe.show.TrayFrameUtf8;
import org.aohe.web.SocketUtils;
import org.aohe.webcam.WebcamShowTask;

import javax.swing.*;

import org.aohe.constant.Control;

import java.nio.file.Paths;

@Slf4j
public class Main {

    private static final String JAVA_IO_TMPDIR = System.getProperty("java.io.tmpdir");

    public static void main(String[] args) throws Exception {
        AppLocker locker = AppLocker.create("aoheSaneServiceLock").setPath(Paths.get(JAVA_IO_TMPDIR)).build();
        UIManager.setLookAndFeel(new FlatDarculaLaf());
        try {
            locker.lock();
        }catch (Exception e){
            JOptionPane.showMessageDialog(null,"程序启动失败，已有实例在运行！"
                    ,"AoHe Scan Program",JOptionPane.WARNING_MESSAGE);	//消息对话框
            log.error("程序已被其他应用占用");
            System.exit(0);
        }
        log.info("启动成功");
        SocketUtils.start(SocketEnum.CAM_SOCKET);
        Control.WEBCAM_SHOW_TASK = new WebcamShowTask();
        Control.WEBCAM_SHOW_TASK.start();
        WatchDog watchDog = new WatchDog();
        watchDog.setDaemon(true);
        watchDog.start();
        TrayFrameUtf8.initSystemTrayUTF8();
    }


}