package com.unis.equipmentOnlineRateAssessment.modular.hk.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.unis.equipmentOnlineRateAssessment.modular.hk.constant.ApiHkConstant;
import com.unis.equipmentOnlineRateAssessment.modular.hk.dto.req.*;
import com.unis.equipmentOnlineRateAssessment.modular.hk.dto.res.*;
import com.unis.equipmentOnlineRateAssessment.modular.hk.service.HkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ybw
 * @version V1.0
 * @className HkServiceImpl
 * @date 2024/6/18
 **/
@Service
@Slf4j
public class HkServiceImpl implements HkService {

    @Resource
    private ArtemisConfig artemisConfig;

    @Override
    public PageResDTO<RegionResDTO> getRegions(PageReqDTO pageReqDTO) {
        try {
            Map<String, String> path = getPath(ApiHkConstant.API_HK_REGIONS);
            String result = ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, JSON.toJSONString(pageReqDTO), null, null, "application/json");
            ResultResDTO<PageResDTO<RegionResDTO>> resultResDTO = JSONObject.parseObject(
                    result,
                    new TypeReference<ResultResDTO<PageResDTO<RegionResDTO>>>() {
                    }
            );
            log.info("{}", JSON.toJSONString(resultResDTO));
            if (ApiHkConstant.Code.SUCCESS.equals(resultResDTO.getCode())) {
                return resultResDTO.getData();
            }
        } catch (Exception e) {
            log.error("getRegions error:", e);
        }
        return null;
    }

    /**
     * 获取接口地址
     *
     * @param api
     * @methodName: getPath
     * @return: java.util.Map<java.lang.String, java.lang.String>
     * @author: ybw
     * @date: 2024/6/19
     **/
    private Map<String, String> getPath(String api) {
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", api);
            }
        };
        return path;
    }

    @Override
    public CameraResDTO getCamera(CameraReqDTO cameraReqDTO) {
        try {
            // 接口地址
            Map<String, String> path = getPath(ApiHkConstant.API_HK_CAMERA);
            String result = ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, JSON.toJSONString(cameraReqDTO), null, null, "application/json");
            ResultResDTO<CameraResDTO> resultResDTO = JSONObject.parseObject(
                    result,
                    new TypeReference<ResultResDTO<CameraResDTO>>() {
                    }
            );
            log.info("{}", JSON.toJSONString(resultResDTO));
            if (ApiHkConstant.Code.SUCCESS.equals(resultResDTO.getCode())) {
                return resultResDTO.getData();
            }
        } catch (Exception e) {
            log.error("getCamera error:", e);
        }
        return null;
    }

    @Override
    public PreviewResDTO previewURLs(PreviewReqDTO previewReqDTO) {
        try {
            // 接口地址
            Map<String, String> path = getPath(ApiHkConstant.API_HK_PREVIEW_URL);
            String result = ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, JSON.toJSONString(previewReqDTO), null, null, "application/json");
            ResultResDTO<PreviewResDTO> resultResDTO = JSONObject.parseObject(
                    result,
                    new TypeReference<ResultResDTO<PreviewResDTO>>() {
                    }
            );
            log.info("{}", JSON.toJSONString(resultResDTO));
            if (ApiHkConstant.Code.SUCCESS.equals(resultResDTO.getCode())) {
                return resultResDTO.getData();
            }
        } catch (Exception e) {
            log.error("previewURLs error:", e);
        }
        return null;
    }

    @Override
    public PlaybackResDTO playbackURLs(PlaybackReqDTO playbackReqDTO) {
        try {
            // 接口地址
            Map<String, String> path = getPath(ApiHkConstant.API_HK_PLAYBACK_URL);
            String result = ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, JSON.toJSONString(playbackReqDTO), null, null, "application/json");
            ResultResDTO<PlaybackResDTO> resultResDTO = JSONObject.parseObject(
                    result,
                    new TypeReference<ResultResDTO<PlaybackResDTO>>() {
                    }
            );
            log.info("{}", JSON.toJSONString(resultResDTO));
            if (ApiHkConstant.Code.SUCCESS.equals(resultResDTO.getCode())) {
                return resultResDTO.getData();
            }
        } catch (Exception e) {
            log.error("previewURLs error:", e);
        }
        return null;
    }

    @Override
    public RecordStartResDTO recordStart(RecordStartReqDTO recordStartReqDTO) {
        try {
            // 接口地址
            Map<String, String> path = getPath(ApiHkConstant.API_HK_RECORD_START);
            String result = ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, JSON.toJSONString(recordStartReqDTO), null, null, "application/json");
            ResultResDTO<RecordStartResDTO> resultResDTO = JSONObject.parseObject(
                    result,
                    new TypeReference<ResultResDTO<RecordStartResDTO>>() {
                    }
            );
            log.info("{}", JSON.toJSONString(resultResDTO));
            if (ApiHkConstant.Code.SUCCESS.equals(resultResDTO.getCode())) {
                return resultResDTO.getData();
            }
        } catch (Exception e) {
            log.error("previewURLs error:", e);
        }
        return null;
    }

    @Override
    public void recordStop(RecordStopReqDTO recordStopReqDTO) {
        try {
            // 接口地址
            Map<String, String> path = getPath(ApiHkConstant.API_HK_RECORD_STOP);
            String result = ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, JSON.toJSONString(recordStopReqDTO), null, null, "application/json");
            ResultResDTO<RecordStartResDTO> resultResDTO = JSONObject.parseObject(
                    result,
                    new TypeReference<ResultResDTO<RecordStartResDTO>>() {
                    }
            );
            log.info("{}", JSON.toJSONString(resultResDTO));
        } catch (Exception e) {
            log.error("previewURLs error:", e);
        }
    }
}
