package com.mortals.xhx.module.hik.event.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.event.model.req.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.rsp.plan.PlanRecognBlackInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

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


    @Override
    public Rest<String> subEvent(EventSubReq eventSubReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/eventService/v1/eventSubscriptionByEventTypes";
        path.put(protocol, getCamsApi);
        try {
            log.info("event sub req=>{}", JSON.toJSONString(eventSubReq));
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(eventSubReq), null, null, "application/json");
            log.info("event sub resp=>{}", respJson);
            Rest<String> rest = JSON.parseObject(respJson, new TypeReference<Rest<String>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok();
            } else {
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("event sub error异常", e);
            return Rest.fail(e.getMessage());
        }
    }

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

    @Override
    public Rest<String> cancelEvent(EventSubReq eventSubReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/eventService/v1/eventUnSubscriptionByEventTypes";
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(eventSubReq), null, null, "application/json");
            Rest<String> rest = JSON.parseObject(respJson, new TypeReference<Rest<String>>() {
            });
            if (rest.getCode() == 0) {
                return Rest.ok();
            } else {
                log.info("event cancel error resp=>", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("event cancel error异常", e);
            return Rest.fail(e.getMessage());
        }
    }
}
