package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.springcloud.service.IApplicationStartedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class DemoStartedService implements IApplicationStartedService {


    @Override
    public void start() {
        log.info("开始服务..");

        String path = System.getProperty("user.dir");

        log.info("当前路径:{}", path);


/*
        boolean headless = GraphicsEnvironment.isHeadless();
        log.info("headless:{}",headless);*/

/*        Demo demo = new Demo();

        demo.test();*/

       // new MySystemTray();

        //加载so
        //String strPath = System.getProperty("user.dir") + "\\lib\\tmz\\libSDTReader.so";

       /* boolean sdkInstance = TMZMain.createSDKInstance();

        log.info("skdInstance:" + sdkInstance);
        int closeDev = TMZMain.tmzSDK._SDT_CloseDev();

        log.info("close:0x{}", Integer.toHexString(closeDev));
        int openret = TMZMain.tmzSDK._SDT_OpenUsbByFD(0);
        log.info("打卡usb:{}", Integer.toHexString(openret));
        if (Integer.toHexString(openret).equals("90")) {
            log.info("打卡设备成功！");
            //寻卡
            byte[] pucManaInfo = new byte[4];
            int ret = TMZMain.tmzSDK._SDT_StartFindIDCard(pucManaInfo);
            log.info("寻卡:0x{},pucManaInfo:{}", Integer.toHexString(ret), pucManaInfo);
*//*            if (!Integer.toHexString(ret).equals("9f")) {
                log.info("寻卡失败");
                return;
            }*//*
            //选卡
            byte[] pucManaInfo1 = new byte[4];
            int selectret = TMZMain.tmzSDK._SDT_SelectIDCard(pucManaInfo);
            log.info("选卡:0x{}", Integer.toHexString(selectret));
            //读卡
            byte[] pucCHMsg = new byte[512];
            byte[] pucPHMsg = new byte[2048];
            byte[] pucFPMsg = new byte[2048];
           // int puiCHMsgLen = 0, puiPHMsgLen = 0, puiFHMsgLen = 0;

            IntByReference puiCHMsgLen = new IntByReference();
            IntByReference puiPHMsgLen = new IntByReference();
            IntByReference puiFHMsgLen = new IntByReference();
            log.info("读卡开始");
           // int[] ints = new int[8];
            //int readret = TMZMain._SDT_ReadBaseMsg(pucCHMsg, ints, pucPHMsg, ints);
          //  int readret = JniTest._SDT_ReadBaseFPMsg(pucCHMsg, 0, pucPHMsg, 0,pucFPMsg,0);
            int readret = TMZMain.tmzSDK._SDT_ReadBaseFPMsg(pucCHMsg, puiCHMsgLen, pucPHMsg, puiPHMsgLen,pucFPMsg,puiFHMsgLen);
            //int readret = TMZMain.tmzParseSDK._SDT_ReadBaseMsg(pucCHMsg, puiCHMsgLen, pucPHMsg, puiPHMsgLen);
            log.info("读卡:0x{}，pucCHMsg：{}",  Integer.toHexString(readret),pucCHMsg);

            IntByReference iType = new IntByReference();
            byte[] pSFZInfo = new byte[260];
            int parseret = TMZMain.tmzSDK._SDT_ParseIDCardTextInfo(pucCHMsg, puiCHMsgLen.getValue(), iType, pSFZInfo);
            String sfzStr = new String(pSFZInfo, StandardCharsets.UTF_8);
            String[] split = sfzStr.split("|");
            log.info("parseret:{},pSFZInfo:{}", parseret, sfzStr);

           // byte[] pBmpFile = new byte[255];

            byte[] pBmpFile ="/tmp/sfz.jpg".getBytes(StandardCharsets.UTF_8);
            int photoRet = TMZMain.tmzSDK._SDT_ParsePhotoInfo(pucPHMsg, puiPHMsgLen.getValue(), 4, pBmpFile);
            log.info("photoRet:0x{},pucCHMsg lens:{}", Integer.toHexString(photoRet),pucCHMsg.length);
        }
*/
/*
        String[] base64Text = CameraOpration.getInstance().initWindow();

        log.info("base64Text:{}", base64Text);*/


       /* Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String resp = HttpUtil.get("http://localhost:8092/app/account/imageCode");
                        if(!ObjectUtils.isEmpty(resp)){
                            log.info("http web is ok");
                        }
                        HttpClient http = null;
                        CookieStore httpCookieStore = new BasicCookieStore();
                        http = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore).build();

                        *//* do stuff *//*
                        HttpGet httpRequest = new HttpGet("http://localhost:8092/app/account/imageCode");
                        HttpResponse httpResponse = null;
                        try {
                            httpResponse = http.execute(httpRequest);
                            //String content = EntityUtils.toString(entity, charset);
                            //httpResponse.getEntity()
                            //httpResponse.toString()
                        } catch (Exception e) {
                            log.error("异常",e);
                        }
                        *//* check cookies *//*
                        String cookieStr = Arrays.asList(httpCookieStore.getCookies()).stream().map(item -> JSON.toJSONString(item)).collect(Collectors.joining("|"));
                        log.info("cookies:{}", cookieStr);
                        TimeUnit.MINUTES.sleep(2);
                    } catch (Exception e) {
                        log.error("异常", e);
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();*/

    }

    @Override
    public void stop() {
        log.info("停止服务..");
    }

    @Override
    public int getOrder() {
        return 10;
    }

}
