package com.community.huiju.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.community.commom.hk.Digests;
import com.community.commom.hk.HttpClientSSLUtils;
import com.community.commom.mode.ResponseBean;
import com.community.huiju.common.hk.HKConstant;
import com.community.huiju.common.hk.HKOpenApi;
import com.community.huiju.service.IMonitoringService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

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

/**
 * 监控 业务实现
 * @author weixmei
 */
@Service("iMonitoringService")
@Slf4j
public class MonitoringServiceImpl implements IMonitoringService {

    @Override
    public ResponseBean getByCommunityId(Integer communityId,Integer pageNo, Integer pageSize) {

        // TODO 数据是假数据, 后期在更改. 需要根据小区查询 组织中心编号

        ResponseBean response = new ResponseBean();
        Map<String,Object> parMap = Maps.newHashMap();
        parMap.put("pageNo",pageNo);
        parMap.put("pageSize",pageSize);
        parMap.put("opUserUuid",HKConstant.OP_USER_UUID);
        // 组织中心编号
        parMap.put("unitUuids","1048576");

        String result = HKOpenApi.getMonitoryPoint(parMap);
        // String url = "rtsp://vipgz1.idcfengye.com:10004/realplay://#cameraUuid#:SUB:";
        String url = "rtsp://222.190.241.190:556/realplay://#cameraUuid#:SUB:";
        JSONArray cameraList = JSONObject.parseObject(result).getJSONObject("data").getJSONArray("list");
        List<String> urlList = new ArrayList<String>();
        for (Object cameraMap : cameraList){
            JSONObject camera = (JSONObject) cameraMap;
            urlList.add(url.replace("#cameraUuid#",camera.getString("cameraUuid")));
        }
        
        response.addSuccess(urlList);

        return response;
    }

    @Override
    public ResponseBean getMonitoringToken(Integer communityId, Integer pageNo, Integer pageSize) {

        // TODO 数据是假数据, 后期在更改. 需要根据小区查询 组织中心编号

        ResponseBean response = new ResponseBean();
        Map<String,Object> parMap = Maps.newHashMap();
        String url = HKConstant.OPENAPI_IP_PORT_HTTP + HKConstant.ITF_MONITORY_POINT;
        //设置APPKEY
        parMap.put("appkey", HKConstant.APPKEY);
        //设置时间参数
        parMap.put("time", System.currentTimeMillis());
        parMap.put("pageNo",pageNo);
        parMap.put("pageSize",pageSize);
        // 这里可以设置其他操作用户 parMap.put("opUserUuid",map.get("opUserUuid") == null?HKConstant.OP_USER_UUID:map.get("opUserUuid"));
        parMap.put("opUserUuid",HKConstant.OP_USER_UUID);
        // 组织中心编号
        parMap.put("unitUuids","1048576");
        String params =  JSON.toJSONString(parMap);
        String token = Digests.buildToken(url + "?" + params, params, HKConstant.SECRET);

        Map<String, Object> map = Maps.newHashMap();
        map.put("token",token);
        response.addSuccess(map);

        return response;
    }
}
