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.face.model.req.face.FaceListReq;
import com.mortals.xhx.module.hik.face.model.req.face.FaceReq;
import com.mortals.xhx.module.hik.face.model.req.face.ResRecognReq;
import com.mortals.xhx.module.hik.face.model.req.group.FaceGroupReq;
import com.mortals.xhx.module.hik.face.model.rsp.face.FaceDataInfo;
import com.mortals.xhx.module.hik.face.model.rsp.face.ResRecognDataInfo;
import com.mortals.xhx.module.hik.face.model.rsp.group.FaceGroupDataInfo;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

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

    @Override
    public Rest<List<FaceDataInfo>> findFaceList(FaceListReq faceListReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/face";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(faceListReq), null, null, "application/json");
            Rest<List<FaceDataInfo>> rest = JSON.parseObject(respJson, new TypeReference<Rest<List<FaceDataInfo>>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok(rest.getData());
            } else {
                log.info("face single error resp=>", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("face add error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<FaceDataInfo> faceSingleAdd(FaceReq faceReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/face/single/addition";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(faceReq), null, null, "application/json");
            Rest<FaceDataInfo> rest = JSON.parseObject(respJson, new TypeReference<Rest<FaceDataInfo>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok(rest.getData());
            } else {
                log.info("face single error resp=>", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("face add error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

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

    @Override
    public Rest<Boolean> faceSingleDel(FaceReq faceReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/face/single/delete";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(faceReq), null, null, "application/json");
            Rest<Boolean> rest = JSON.parseObject(respJson, new TypeReference<Rest<Boolean>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok(rest.getData());
            } else {
                log.info("face single error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("face add error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<List<FaceGroupDataInfo>> findFaceGroupList(FaceGroupReq faceGroupReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/group";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(faceGroupReq), null, null, "application/json");
            Rest<List<FaceGroupDataInfo>> rest = JSON.parseObject(respJson, new TypeReference<Rest<List<FaceGroupDataInfo>>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok(rest.getData());
            } else {
                log.info("face group list error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("face group list error异常", e);
            return Rest.fail(e.getMessage());
        }
    }


    @Override
    public Rest<FaceGroupDataInfo> faceGroupSingleAdd(FaceGroupReq faceGroupReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/face/single/addition";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(faceGroupReq), null, null, "application/json");
            Rest<FaceGroupDataInfo> rest = JSON.parseObject(respJson, new TypeReference<Rest<FaceGroupDataInfo>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok(rest.getData());
            } else {
                log.info("face single error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("face add error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

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

    /**
     * 识别计划需要人脸资源
     * @param recognReq
     * @return
     */
    @Override
    public Rest<List<ResRecognDataInfo>> findResRecognList(ResRecognReq recognReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/resource/recognition";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(recognReq), null, null, "application/json");
            Rest<List<ResRecognDataInfo>> rest = JSON.parseObject(respJson, new TypeReference<Rest<List<ResRecognDataInfo>>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok(rest.getData());
            } else {
                log.info("face res list error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("face res list error异常", e);
            return Rest.fail(e.getMessage());
        }
    }
}
