package com.mortals.xhx.daemon.applicationservice;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hikvision.artemis.sdk.constant.Constants;
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.code.YesNoEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.hik.face.model.FaceGroupEntity;
import com.mortals.xhx.module.hik.face.model.FaceGroupQuery;
import com.mortals.xhx.module.hik.face.service.FaceGroupService;
import com.mortals.xhx.module.hik.face.service.FacePlanService;
import com.mortals.xhx.module.hik.event.model.req.sub.EventSubReq;
import com.mortals.xhx.module.hik.event.model.rsp.EventInfo;
import com.mortals.xhx.module.hik.event.service.IHikEventService;
import com.mortals.xhx.module.hik.face.model.req.group.FaceGroupReq;
import com.mortals.xhx.module.hik.face.model.rsp.group.FaceGroupDataInfo;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.hik.face.service.IHikPlanService;
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 org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

import static com.mortals.xhx.common.key.ParamKey.PARAM_FACE_GROUP;

/**
 * 初始化部分计划数据等
 * @author:
 * @date: 2023/5/9 10:01
 */
@Component
@Slf4j
public class SubEventStartedService implements IApplicationStartedService {

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

    @Autowired
    private IHikEventService hikEventService;
    @Autowired
    private IHikFaceService hikFaceService;
    @Autowired
    private IHikPlanService hikPlanService;
    @Autowired
    private FaceGroupService faceGroupService;
    @Autowired
    private FacePlanService facePlanService;
    @Autowired
    private DeviceService deviceService;


    @Override
    public void start() {
        ThreadPool.getInstance().init(20);

        Constants.DEFAULT_TIMEOUT=12000;

        log.info("开始服务..[事件订阅服务]");
        //String eventtypes = GlobalSysInfo.getParamValue(Constant.PARAM_EVENTTYPES, "131614,131659,1644175361,1644171265");
        String eventtypes = GlobalSysInfo.getParamValue(Constant.PARAM_EVENTTYPES, "131659,1644175361,1644171265");
        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));

        //查询本地指定人脸分组 如果没有则添加 并更新
        String groupStr = GlobalSysInfo.getParamValue(PARAM_FACE_GROUP, "face_group_register,appointment_group");
        List<String> groupList = StrUtil.split(groupStr, ",".charAt(0));
        for (String group : groupList) {
            FaceGroupEntity faceGroupEntity = faceGroupService.selectOne(new FaceGroupQuery().name(group));
            if (ObjectUtils.isEmpty(faceGroupEntity)) {
                log.info("创建人脸分组...");
                FaceGroupReq faceGroupReq = new FaceGroupReq();
                faceGroupReq.setName(group);
                faceGroupReq.setDescription(group);
                Rest<FaceGroupDataInfo> faceGroupRest = hikFaceService.faceGroupSingleAdd(faceGroupReq);
                if (faceGroupRest.getCode() == YesNoEnum.YES.getValue()) {
                    FaceGroupEntity groupEntity = new FaceGroupEntity();
                    groupEntity.initAttrValue();
                    groupEntity.setName(group);
                    groupEntity.setDescription(group);
                    groupEntity.setIndexCode(faceGroupRest.getData().getIndexCode());
                    groupEntity.setCreateTime(new Date());
                    groupEntity.setCreateUserId(1L);
                    groupEntity.setCreateUserName("admin");
                    faceGroupService.save(groupEntity);
                }
            }
        }

        //创建陌生人计划
        facePlanService.createStrangerPlan();
        //创建预约人监控计划
        facePlanService.createAppointmentPlan();

    }

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

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


}
