package cn.iocoder.yudao.module.smartfactory.service.hik;

import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.smartfactory.constant.ArtemisApiConstant;
import cn.iocoder.yudao.module.smartfactory.controller.admin.hik.vo.CameraOperateDTO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.hik.vo.CameraReplayDTO;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.date.DateUtils.IS0_8601;

/**
 * Hik Service 接口
 *
 * @author 艾楷
 */
@Service
@Validated
@Slf4j
public class HikServiceImpl implements HikService {

    @Override
    public String getPreviewUrlsApi(String cameraCode, Integer streamType) {
        final String previewUrlsApi = ArtemisApiConstant.previewUrlsApi;
        Map<String, String> path = new HashMap<String, String>(1) {
            {
                put("https://", previewUrlsApi);
            }
        };
        JSONObject body = new JSONObject();
        body.set("cameraIndexCode", cameraCode);
        body.set("streamType", 0);
        body.set("protocol", "ws");
        body.set("transmode", 1);
        body.set("expand", "streamform=ps");
        try {
            String result = ArtemisHttpUtil
                    .doPostStringArtemis(path, JSONUtil.toJsonStr(body), null, null, "application/json");
            log.info("{}监控查看预览地址返回：{}", JSONUtil.toJsonStr(body), result);
            if (StringUtils.isEmpty(result)) {
//                throw new ServiceException("监控查看预览地址返回为空");
                System.out.println("监控查看预览地址返回为空");
            }
            JSONObject json = JSONUtil.parseObj(result);
            String url = "";
            if (json.getJSONObject("data") != null) {
                url = json.getJSONObject("data").getStr("url");
            }
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public JSONObject getReplayUrl(CameraReplayDTO replayDTO) {
        final String replayUrlApi = ArtemisApiConstant.replayUrlApi;
        Map<String, String> path = new HashMap<String, String>(1) {
            {
                put("https://", replayUrlApi);
            }
        };
        JSONObject body = new JSONObject();
        body.set("cameraIndexCode", replayDTO.getCameraIndexCode());
        body.set("recordLocation", replayDTO.getRecordLocation());
        body.set("protocol", "ws");
        body.set("transmode", 1);
        body.set("beginTime", DateUtil.format(DateUtil.parse(replayDTO.getBeginTime()), IS0_8601));
        body.set("endTime", DateUtil.format(DateUtil.parse(replayDTO.getEndTime()), IS0_8601));
        try {
            String result = ArtemisHttpUtil.doPostStringArtemis(path, JSONUtil.toJsonStr(body), null, null, "application/json");
            JSONObject json = JSONUtil.parseObj(result);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String operateCamera(CameraOperateDTO dto) {
        final String operateUrlApi = ArtemisApiConstant.operateUrlApi;
        Map<String, String> path = new HashMap<String, String>(1) {
            {
                put("https://", operateUrlApi);
            }
        };
//        String speedStr = configService.selectConfigByKey(CONFIG_SPEED);
        String speedStr = "";

        JSONObject body = new JSONObject();
        body.set("cameraIndexCode", dto.getCameraIndexCode());
        body.set("action", dto.getAction());
        body.set("command", dto.getCommand());
        body.set("speed", StringUtils.isNotBlank(speedStr) ? Integer.parseInt(speedStr) : 30);
        body.set("presetIndex", 50);
        try {
            String result = ArtemisHttpUtil.doPostStringArtemis(path, JSONUtil.toJsonStr(body), null, null, "application/json");
            log.info("云台操作：{}，返回结果：{}", JSONUtil.toJsonStr(dto), result);
            JSONObject json = JSONUtil.parseObj(result);
            String msg = json.getStr("msg");
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}