package com.mortals.xhx.daemon.applicationservice;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.net.NetUtil;
import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.framework.util.ThreadPool;
import com.mortals.xhx.daemon.applicationservice.model.DestInfo;
import lombok.extern.slf4j.Slf4j;
import org.pcap4j.core.PcapAddress;
import org.pcap4j.core.PcapNetworkInterface;
import org.pcap4j.core.Pcaps;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

//@Component
@Slf4j
public class CaptureStartedService implements IApplicationStartedService {

    @Value("${config.hcpurl:http://127.0.0.1:8090/inter/hcpapi/hcpGrabEvaluate}")
    private String hcpUrl;

    @Value("${config.hcpgovurl:https://hcp.sczwfw.gov.cn/appEval/index.html#/evalInstrument}")
    private String hcpGovUrl;


    @Value("${configPath:/tmp/mid.prop}")
    private String confPath;

    @Override
    public void start() {
        log.info("启动抓取网卡数据服务..");
        ThreadPool.getInstance().init(10);

        List<PcapNetworkInterface> allDevs = null;
        try {
            allDevs = Pcaps.findAllDevs();
            String localHost = "127.0.0.1";
            for (final PcapNetworkInterface device : allDevs) {
                List<PcapAddress> addresses = device.getAddresses();
                Boolean bool = false;
                String ip = "";
                boolean ipv4 = false;
                for (PcapAddress pcapAddress : addresses) {
                    // 获取网卡IP地址
                    ip = pcapAddress.getAddress().getHostAddress();
                    //log.info("device"+JSON.toJSONString(device) + " ip:" + ip+" is local ipw:"+ JSON.toJSONString( NetUtil.localIps()));
                    //非本地网卡
                    ipv4 = Validator.isIpv4(ip);
                    log.info("ip:{}, is ipv4:{}", ip, ipv4);
                    if (ip != null && ip.contains(localHost)) {
                        continue;
                    }
                    if (ipv4) {
                        bool = true;
                        break;
                    }
                }
                if ("any".indexOf(device.getName()) != -1) {
                    continue;
                }
                if (!bool) {
                    log.info("excloud device:{},ip:{}", device.getName(),ip);
                    continue;
                }
                //读取窗口id

                log.info("start deviceName:{},ip:{}, is ipv4:{}", device.getName(), ip, ipv4);
                Map<String, Set<String>> filterMap = DestInfo.build();

                //String redirectUrl = "";
                CaptureThread captureThread = new CaptureThread(device, hcpUrl, hcpGovUrl, filterMap,confPath);
                ThreadPool.getInstance().execute(captureThread);
            }
        } catch (Exception e) {
            log.error("启动抓取网卡服务异常：", e);
        }

    }

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

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

}
