package com.zcloud.modules.hk.controller;

import com.zcloud.common.utils.R;
import com.zcloud.util.HKUtil;
import com.zcloud.util.ReturnMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("/map/hk")
public class hkController {


    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/platformList")
    public R platformList(@RequestBody Map<String, Object> params) throws Exception{
        if(StringUtils.isEmpty(params.get("pageNo").toString())){
            return R.error("缺少必填参数pageNo");
        }else if(StringUtils.isEmpty(params.get("pageSize").toString())){
            return R.error("缺少必填参数pageSize");
        }
        Map<String,Object> map = HKUtil.cameraSearch(params);
        if(ObjectUtils.isEmpty(map)){
            return R.error("海康平台摄像头调用失败");
        }
        return R.ok(map);
    }

    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/getHlsPath")
    public R getHlsPath(@RequestBody Map<String, Object> params) throws Exception{
        if(StringUtils.isEmpty(params.get("INDEXCODE").toString())){
            return R.error("缺少必填参数INDEXCODE");
        }
        Map<String,Object> map = HKUtil.camerasPreviewURLs(params.get("INDEXCODE").toString(),"hls");
        if(ObjectUtils.isEmpty(map)){
            return R.error("海康平台摄像头调用失败");
        }
        return R.ok(map);
    }

    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/getRtspPath")
    public R getRtspPath(@RequestBody Map<String, Object> params) throws Exception{
        if(StringUtils.isEmpty(params.get("INDEXCODE").toString())){
            return R.error("缺少必填参数INDEXCODE");
        }
        Map<String,Object> map = HKUtil.camerasPreviewURLs(params.get("INDEXCODE").toString(),"rtsp");
        if(ObjectUtils.isEmpty(map)){
            return R.error("海康平台摄像头调用失败");
        }
        return R.ok(map);
    }


    @RequestMapping(value="/goAllVideo")
    public Object goAllVideo(@RequestBody Map<String, Object> params) throws Exception{
        if(StringUtils.isEmpty(params.get("DATA_IDS").toString())){
            return R.error("缺少必填参数DATA_IDS,参数格式为1,2,3,4用逗号分隔的INDEXCODE");
        }
        String DATA_IDS = params.get("DATA_IDS").toString();
        Map<String,Object> map = new HashMap<>();
        for (String INDEXCODE : DATA_IDS.split(",")) {
            Map<String,Object> returnMap = HKUtil.camerasPreviewURLs(INDEXCODE,"hls");
            map.put(INDEXCODE,returnMap);
        }
        return R.ok(map);
    }
}
