package com.huimv.admin.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.huimv.admin.entity.BaseSpecificVideo;
import com.huimv.admin.entity.CameraBase;
import com.huimv.admin.entity.CameraBrand;
import com.huimv.admin.entity.hkwsdto.HkvsPreviewDto;
import com.huimv.admin.entity.vo.CameraVo;
import com.huimv.admin.mapper.BaseSpecificVideoMapper;
import com.huimv.admin.mapper.CameraAreaMapper;
import com.huimv.admin.mapper.CameraBaseMapper;
import com.huimv.admin.mapper.CameraBrandMapper;
import com.huimv.admin.service.IBaseSpecificVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-12-20
 */
@Service
public class BaseSpecificVideoServiceImpl extends ServiceImpl<BaseSpecificVideoMapper, BaseSpecificVideo> implements IBaseSpecificVideoService {
    @Autowired
    private CameraBaseMapper cameraMapper;

    @Autowired
    private CameraAreaMapper cameraAreaMapper;


    @Autowired
    private CameraBrandMapper cameraBrandMapper;

    @Override
    public List listSpecif(Integer farmId, Integer type) throws Exception {
        List<BaseSpecificVideo> list = this.list(new QueryWrapper<BaseSpecificVideo>().eq("farm_id", farmId).eq("type", type));

        List list1 = new ArrayList();
        for (BaseSpecificVideo baseSpecificVideo : list) {

            CameraBase camera = cameraMapper.selectById(baseSpecificVideo.getVideoId());

            list1.add(getCameraVo(camera));
        }

        return list1;
    }

    public CameraVo getCameraVo(CameraBase camera) throws Exception {


        String cameraName = camera.getCameraName();
        String areaId = camera.getRegionUuid();

        CameraBrand cameraBrand = cameraBrandMapper.selectById(camera.getBrandId());
        Integer type = cameraBrand.getStreamType();



        CameraVo cameraVo = new CameraVo();
        cameraVo.setAreaId(areaId);

        Integer id = camera.getId();
        String cameraUuid = camera.getCameraUuid();
        CameraBase cameraBase = cameraMapper.selectById(id);
        String callPostApiGetPreviewURLs = callPostApiGetPreviewURLs(cameraUuid);
        HkvsPreviewDto hkvsPreviewDto = JSONUtil.toBean(callPostApiGetPreviewURLs, HkvsPreviewDto.class);

        String url = hkvsPreviewDto.getData().getUrl();
        cameraBase.setUrl(url);
        cameraMapper.updateById(cameraBase);


        cameraVo.setWsUrl(url);
//        if (camera.getId() == 767){
//            cameraVo.setWsUrl("ws://" + url + "/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword + "%40" + camerIp);
//            cameraVo.setRtspUrl("rtsp://" + cameraAccount + ":" + cameraPassword + "@" + camerIp + "/cam/realmonitor?channel=2&subtype=0");
//        }else {
//            cameraVo.setWsUrl("ws://" + url + "/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword + "%40" + camerIp);
//            cameraVo.setRtspUrl("rtsp://" + cameraAccount + ":" + cameraPassword + "@" + camerIp + rtsp);
//        }

        cameraVo.setCameraName(cameraName);
        return cameraVo;
    }

    private static final String PREVIEW_PATH = "/api/video/v2/cameras/previewURLs";
    /**
     * API网关的后端服务上下文为：/artemis
     */
    private static final String ARTEMIS_PATH = "/artemis";

    private static final String SUBREGIONS_PATH = "/api/resource/v2/regions/subRegions";

    private static final String CAMERAS_PATH = "/api/resource/v2/camera/search";

    private static final String NEW_APPKEY = "27546900";

    private static final String NEW_SECRET = "S5qgrKdBd75M0NZRzGAh";

//    private static final String OPENAPI_IP = "61.153.248.109";
    private static final String OPENAPI_IP = "www.htzj.fun";

    private static final String ZK_REGION = "500d8b6a-2554-43f3-bda7-e686577eb741";

    private static final String PJ_REGION = "70405f14-1cb5-4413-8bef-38e4a365cbee";

    private static final String XSD_REGION= "5c23fa9b-ee7e-4201-851d-340b0f6f320e";

    private static final String PW_REGION = "66096289-bd19-4390-a5d5-71a31e970b0b";

    private static final String DY_REGION = "a09ba2d8-8236-4de6-8e53-9008912d7b72";

    private static final String TT_REGION = "d64e2029-75bb-4214-8cc4-e93deace9ea4";
    public static String callPostApiGetPreviewURLs(String cameraIndexCode) throws Exception {
        /**
         * https://ip:port/artemis/api/video/v2/cameras/previewURLs
         * 根据指定的区域编号获取该区域下的监控点列表信息，返回结果分页展示。
         * 注：返回的监控点不包括下级区域的。
         */
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(OPENAPI_IP); // 代理API网关nginx服务器ip端口
        config.setAppKey(NEW_APPKEY);  // 秘钥appkey
        config.setAppSecret(NEW_SECRET);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + PREVIEW_PATH;
        Map<String, Object> paramMap = new HashMap<String, Object>();// post请求Form表单参数
        paramMap.put("cameraIndexCode",cameraIndexCode);
        paramMap.put("streamType",0);
        paramMap.put("protocol","ws");
        paramMap.put("transmode",1);
        paramMap.put("expand","streamform=rtp");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String s = ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
        System.out.println(s);

        return s;
    }

}
