package com.template.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.template.api.HikvisionAPI;
import com.template.common.utils.GetCameraPreviewURL;
import com.template.model.pojo.SmartFaceDiscern;
import com.template.model.pojo.SmartWarning;
import com.template.services.SmartFaceDiscernService;
import com.template.services.SmartWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;

@RestController
public class HikvisionController implements HikvisionAPI {

    @Autowired
    SmartWarningService smartWarningService;

    @Autowired
    SmartFaceDiscernService smartFaceDiscernService;


    /**
     * 事件订阅
     * @param request
     */
    @Override
    public void subscription(HttpServletRequest request) {
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
//            System.out.println("jsonObject = " + jsonObject);
            JSONObject params = jsonObject.getJSONObject("params");

            JSONArray events = params.getJSONArray("events");

            JSONObject jsonObject1 = events.getJSONObject(0);
//            类型
            String eventType = jsonObject1.getString("eventType");

//            时间
            String happenTime = jsonObject1.getString("happenTime");

            SmartWarning smartWarning = new SmartWarning();
            smartWarning.setDateTime(happenTime);
            smartWarning.setStatu(0);
            JSONObject data = jsonObject1.getJSONObject("data");

//                地点
            String srcName = jsonObject1.getString("srcName");
            smartWarning.setLocation(srcName);

            if ("131588".equals(eventType)) {

                smartWarning.setType("区域入侵");

                JSONArray fielddetection = data.getJSONArray("fielddetection");

                JSONObject jsonObject2 = fielddetection.getJSONObject(0);
//                图片
                String imageUrl = jsonObject2.getString("imageUrl");
                smartWarning.setImage(imageUrl);

            }else if ("131586".equals(eventType)){
                smartWarning.setType("进入区域");

                JSONArray regionEntrance = data.getJSONArray("regionEntrance");

                JSONObject jsonObject2 = regionEntrance.getJSONObject(0);
//                图片
                String imageUrl = jsonObject2.getString("imageUrl");
                smartWarning.setImage(imageUrl);

            }else if ("131587".equals(eventType)){
                smartWarning.setType("离开区域");

                JSONArray regionExiting = data.getJSONArray("regionExiting");

                JSONObject jsonObject2 = regionExiting.getJSONObject(0);
//                图片
                String imageUrl = jsonObject2.getString("imageUrl");
                smartWarning.setImage(imageUrl);

            }else if ("131605".equals(eventType)){
                smartWarning.setType("倒地");

                JSONArray fallDown = data.getJSONArray("fallDown");

                JSONObject jsonObject2 = fallDown.getJSONObject(0);
//                图片
                String imageUrl = jsonObject2.getString("imageUrl");
                smartWarning.setImage(imageUrl);

            }

            smartWarningService.save(smartWarning);




        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 人脸比对
     * @param request
     */
    @Override
    public void faceComparison(HttpServletRequest request) {
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());

//            System.out.println("jsonObject = " + jsonObject);

            JSONObject params = jsonObject.getJSONObject("params");

            JSONArray events = params.getJSONArray("events");

            JSONObject jsonObject1 = events.getJSONObject(0);
//            类型
            String eventType = jsonObject1.getString("eventType");

//            时间
            String happenTime = jsonObject1.getString("happenTime");

            SmartFaceDiscern smartFaceDiscern = new SmartFaceDiscern();
            smartFaceDiscern.setDateTime(happenTime);

            JSONObject data = jsonObject1.getJSONObject("data");

//            地点
            String channelName = data.getString("channelName");
            smartFaceDiscern.setLocation(channelName);


            if ("131659".equals(eventType)) {
                smartFaceDiscern.setType("人脸比对");

                JSONArray alarmResult = data.getJSONArray("alarmResult");
                JSONObject jsonObject2 = alarmResult.getJSONObject(0);

                JSONObject targetAttrs = jsonObject2.getJSONObject("targetAttrs");
                String bkgUrl = targetAttrs.getString("bkgUrl");
                smartFaceDiscern.setImage(bkgUrl);

//                获取人脸信息
                JSONArray faces = jsonObject2.getJSONArray("faces");
                JSONObject jsonObject3 = faces.getJSONObject(0);
//                对比成功的图片
                JSONArray identify = jsonObject3.getJSONArray("identify");
                if (ObjectUtils.isNotEmpty(identify)&&identify.size()>0) {
//                    取对比成功的第一张
                    JSONObject jsonObject4 = identify.getJSONObject(0);
                    JSONArray candidate = jsonObject4.getJSONArray("candidate");
                    JSONObject jsonObject5 = candidate.getJSONObject(0);
                    JSONObject reserve_field = jsonObject5.getJSONObject("reserve_field");
//                    证件号
                    String certificateNumber = reserve_field.getString("certificateNumber");
//                    名字
                    String name = reserve_field.getString("name");
                    smartFaceDiscern.setName(name);

//                  todo  通过身份证查询是谁

                    smartFaceDiscernService.save(smartFaceDiscern);

                }


            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 重点人员
     * @param request
     */
    @Override
    public void emphasisFace(HttpServletRequest request) {
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());

            System.out.println("jsonObject = " + jsonObject);

            JSONObject params = jsonObject.getJSONObject("params");

            JSONArray events = params.getJSONArray("events");

            JSONObject jsonObject1 = events.getJSONObject(0);
//            类型
            String eventType = jsonObject1.getString("eventType");

//            时间
            String happenTime = jsonObject1.getString("happenTime");

            SmartWarning smartWarning = new SmartWarning();
            smartWarning.setDateTime(happenTime);
            smartWarning.setStatu(0);
            JSONObject data = jsonObject1.getJSONObject("data");

//            获取地点
            JSONArray resInfo = data.getJSONArray("resInfo");
            JSONObject jsonObject3 = resInfo.getJSONObject(0);
            String cn = jsonObject3.getString("cn");
            smartWarning.setLocation(cn);


            if ("1644175361".equals(eventType)) {
                smartWarning.setType("重点人员识别");

                JSONObject faceRecognitionResult = data.getJSONObject("faceRecognitionResult");

//                获取图片
                JSONObject snap = faceRecognitionResult.getJSONObject("snap");
                String bkgUrl = snap.getString("bkgUrl");
                smartWarning.setImage(bkgUrl);

//                获取重点人员身份
                JSONArray faceMatch = faceRecognitionResult.getJSONArray("faceMatch");
                JSONObject jsonObject2 = faceMatch.getJSONObject(0);
//                名字
                String faceInfoName = jsonObject2.getString("faceInfoName");
                smartWarning.setWarningName(faceInfoName);
//                证件号码
                String certificate = jsonObject2.getString("certificate");


                smartWarningService.save(smartWarning);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public JSONObject playback() {
        String url="/api/video/v2/cameras/playbackURLs";
        JSONObject jsonBoxdy = new JSONObject();
        jsonBoxdy.put("cameraIndexCode","ff2a2978a71c4ba0ac43a1a6be97a1a1");
        jsonBoxdy.put("streamType",0);
        jsonBoxdy.put("protocol","rtsp");
        jsonBoxdy.put("transmode",1);
        String s =GetCameraPreviewURL.CameraPreviewURL(url, jsonBoxdy);

        JSONObject jsonObject1 = JSONObject.parseObject(s);
        return jsonObject1;

    }

    @Override
    public JSONObject general(JSONObject jsonObject) {
        String url = jsonObject.getString("url");
        JSONObject jsonBoxdy = jsonObject.getJSONObject("jsonBoxdy");
        String s = GetCameraPreviewURL.CameraPreviewURL(url, jsonBoxdy);
        JSONObject jsonObject1 = JSONObject.parseObject(s);
        return jsonObject1;
    }
}
