package com.mortals.xhx.module.hik.face.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.module.hik.AbstractHikService;
import com.mortals.xhx.module.hik.HikApiRest;
import com.mortals.xhx.module.hik.face.model.req.plan.PlanRecognListReq;
import com.mortals.xhx.module.hik.face.model.req.plan.PlanRecognReq;
import com.mortals.xhx.module.hik.face.model.rsp.plan.PlanRecognInfo;
import com.mortals.xhx.module.hik.face.service.IHikPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 海康人员接口类
 *
 * @author: zxfei
 * @date: 2023/4/12 17:51
 */
@Service("hikPlanService")
@Slf4j
public class HikPlanServiceImpl extends AbstractHikService implements IHikPlanService {

    @Override
    public Rest<List<PlanRecognInfo>> findPlanRecognBlackList(PlanRecognListReq planRecognBlackListReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/black";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognBlackListReq), null, null, "application/json");
            HikApiRest<List<PlanRecognInfo>> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<List<PlanRecognInfo>>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("plan recognize balck list error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize balck list error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    /**
     * a)该接口可以新增一条重点人员识别计划，以实现将若干抓拍点抓拍到的人脸，
     * 通过若干识别资源同若干分组内的人脸比较，并将命中结果以事件形式上报。
     * b)配置一条识别计划需要人脸分组集合（通过”按条件查询人脸分组”接口获取）
     * ，抓拍点集合（通过LDAP服务获取监控点，并通过型号确定抓拍点），
     * 识别资源集合（通过”按条件查询识别资源”接口获取）。
     * c)一条重点人员识别计划只能配置一种识别资源类别：深眸或脸谱或超脑。
     * d)一条重点人员识别计划，可以配置多个超脑作为识别资源或一个脸谱作为识别资源。
     * e)一条重点人员识别计划如果配置深眸作为识别资源，则recognitionResourceIndexCodes可以传空
     * 。因为深眸的抓拍点就是识别资源本身，人脸监控应用服务会根据抓拍点自动匹配正确的识别资源。
     * f)人脸监控应用服务会校验抓拍点的有效性。校验规则如下：超脑作为识别资源时，
     * 抓拍点必须是超脑的通道；脸谱作为识别资源时，脸谱作为识别资源时，抓拍点一般为深眸或其他人脸抓拍机通道；
     * 深眸作为识别资源时，抓拍点必须是深眸的通道；一个通道是否是深眸/超脑/抓拍机的通道，取决于通道所属设备的型号，
     * 详见详见附录I 人脸监控应用服务支持设备型号列表。
     * g)重点人员识别计划配置有以下限制，这些限制会在添加时作为特殊错误返回：超脑的一个通道只能配置一次重点人员识别计划；
     * 同一个抓拍点不能同时配置在多个脸谱上；抓拍点或识别资源没有同步到IAC前，无法配置识别计划。
     * h)识别计划的配置为异步过程，调用接口成功后，人脸监控应用服务会在后台自动下发人脸分组并执行布控操作，
     * 可以通过接口”单个查询重点人员识别计划详情”查询该识别计划的下发结果。 i) 识别计划支持配置时间计划模板，
     * 当时间计划参数不传时，默认全天候。当传入此参数时，时间计划规则如下：按周计划进行配置，每一天的时间段上限为8，每一个时间段的开始时间必须小于结束时间。
     * j)同一个深眸支持配置多个识别计划。
     * k)若设备上人脸分组达到上限，则有可能导致下发失败。
     *
     * @param planRecognBlackReq
     * @return
     */
    @Override
    public Rest<String> planRecognBlackAdd(PlanRecognReq planRecognBlackReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/black/addition";
        path.put(protocol, getCamsApi);
        try {
            log.info("plan recognize balck req=>{}", JSON.toJSONString(planRecognBlackReq));
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognBlackReq), null, null, "application/json");
            log.info("plan recognize balck  resp=>{}", respJson);
            HikApiRest<String> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<String>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("plan recognize balck error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize balck add error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<Boolean> planRecognBlackDel(PlanRecognReq planRecognBlackReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/black/deletion";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognBlackReq), null, null, "application/json");
            HikApiRest<Boolean> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<Boolean>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("plan recognize balck error del resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize balck del error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<List<PlanRecognInfo>> findPlanRecognWhiteList(PlanRecognListReq planRecognWhiteListReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/white";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognWhiteListReq), null, null, "application/json");
            HikApiRest<List<PlanRecognInfo>> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<List<PlanRecognInfo>>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("plan recognize white list error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize white list error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<String> planRecognWhiteAdd(PlanRecognReq planRecognWhiteReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/white/addition";
        path.put(protocol, getCamsApi);
        try {
            log.info("plan recognize white req=>{}", JSON.toJSONString(planRecognWhiteReq));
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognWhiteReq), null, null, "application/json");
            log.info("plan recognize white  resp=>{}", respJson);
            HikApiRest<String> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<String>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getMsg(),rest.getData());
            } else {
                log.info("plan recognize white add error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize white add error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<Boolean> planRecognWhiteUpdate(PlanRecognReq planRecognWhiteReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/white/update";
        path.put(protocol, getCamsApi);
        try {
            log.info("plan recognize white req=>{}", JSON.toJSONString(planRecognWhiteReq));
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognWhiteReq), null, null, "application/json");
            log.info("plan recognize white  resp=>{}", respJson);
            HikApiRest<Boolean> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<Boolean>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("plan recognize white add error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize white add error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<Boolean> planRecognWhiteDel(PlanRecognReq planRecognWhiteReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/white/deletion";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognWhiteReq), null, null, "application/json");
            HikApiRest<Boolean> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<Boolean>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("plan recognize white error del resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize white del error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<Boolean> planRecognWhiteReStart(PlanRecognReq planRecognReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/plan/recognition/white/restart";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(planRecognReq), null, null, "application/json");
            HikApiRest<Boolean> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<Boolean>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("plan recognize white error restart resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("plan recognize white erstart error异常", e);
            return Rest.fail(e.getMessage());
        }
    }
}
