package com.mortals.xhx.daemon.applicationservice;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.common.Rest;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.framework.util.ThreadPool;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.hik.event.model.req.EventSubReq;
import com.mortals.xhx.module.hik.event.model.rsp.EventInfo;
import com.mortals.xhx.module.hik.event.service.IHikEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@Slf4j
public class SubEventStartedService implements IApplicationStartedService {

    @Value("${hik.callback:''}")
    private String callback;

    @Autowired
    private IHikEventService hikEventService;

    @Override
    public void start() {
        log.info("开始服务..[事件订阅服务]");
        ThreadPool.getInstance().init(20);
        String eventtypes = GlobalSysInfo.getParamValue(Constant.PARAM_EVENTTYPES, "1644175361");
        List<Integer> eventTypes = StrUtil.split(eventtypes, ",".charAt(0), -1, false, Integer::parseInt);

        EventSubReq eventSubReq = new EventSubReq();
        eventSubReq.setEventTypes(eventTypes);
        eventSubReq.setEventDest(callback);
        Rest<String> rest = hikEventService.subEvent(eventSubReq);
        log.info("sub events resp==>{}", JSON.toJSONString(rest));

        Rest<List<EventInfo>> events = hikEventService.getEvents();
        log.info("get events resp==>{}", JSON.toJSONString(events));

    }

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

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


}
