package com.huimv.beeboxs.dahuaVideo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.dahuaVideo.business.impl.UserManagerImpl;
import com.huimv.beeboxs.dahuaVideo.util.HttpSend;
import org.springframework.web.bind.annotation.*;

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


/**
 * TODO
 *
 * @author CodeYang.
 * @date 2022/4/22 13:58
 */




@CrossOrigin
@RestController
@RequestMapping(value = "/dahua")
public class CameraListController {



    //展示账号下面所有的设备
    @PostMapping("/listDevice")
    public  Result page( ){

        HttpSend httpSend = new HttpSend();
        Map<String, Object>   parmap   = new HashMap<>();

        parmap.put("token",getToken());
        parmap.put("bindId","-1");
        parmap.put("limit",128);
        parmap.put("type","bindAndShare");
        parmap.put("needApInfo","false");
        JSONObject deviceBaseList = httpSend.execute(parmap, "deviceBaseList");
        //{"result":{"msg":"操作成功。","code":"0","data":{"count":1,"deviceList":[{"channels":[{"channelName":"IPDome-FF35","channelId":"0"}],"deviceId":
        // "8B06F91PAJ1FF35","bindId":1,"aplist":[]}]}},"id":"23"}
        System.out.println(deviceBaseList);
        JSONObject jsonResult = deviceBaseList.getJSONObject("result");
        JSONObject jsonData = jsonResult.getJSONObject("data");
        return new Result(ResultCode.SUCCESS, jsonData );

    }


    /**
     * 拿到视频流  返回数据
     *kitToken  ：拿视屏的token
     * 视频流 url长这样 --前端自己拼拼:   'imou://open.lechange.com/8B06F91PAJ1FF35/0/1?streamId=1'
      param map
     * @return
     */
    @PostMapping("/getVideoStream")
    public Result getVideo(@RequestBody Map map){


        HttpSend httpSend = new HttpSend();
        Map<String, Object>   parmap   = new HashMap<>();
        parmap.put("token",getToken());
        parmap.put("channelId",map.get("channelId"));
        parmap.put("enable",true);
        parmap.put("deviceId",map.get("deviceId"));
        JSONObject deviceBaseList = httpSend.execute(parmap, "getKitToken");

         String  VideoUrl = "imou://open.lechange.com/"+map.get("deviceId")+"/"+map.get("channelId")+"/1?streamId=1";
        JSONObject jsonResult = deviceBaseList.getJSONObject("result");
        JSONObject jsonData = jsonResult.getJSONObject("data");
        String token = jsonData.getString("kitToken");
         String  kitToken =token;

        HashMap<String, String> jectHashMap = new HashMap<>();
        jectHashMap.put("VideoUrl",VideoUrl);
        jectHashMap.put("kitToken",kitToken);

        System.out.println(deviceBaseList);
        return new Result(ResultCode.SUCCESS, jectHashMap  );

    }


    /**
     * 拿到视频流  返回数据
     *kitToken  ：拿视屏的token
     * 视频流 url长这样 --前端自己拼拼:   'imou://open.lechange.com/8B06F91PAJ1FF35/0/1?streamId=1'
     param map
     * @return
     */
    @PostMapping("/changeDirection")
    public Result changeDirection(@RequestBody Map map){
        HttpSend httpSend = new HttpSend();
        Map<String, Object>   parmap   = new HashMap<>();
        parmap.put("token",getToken());
        parmap.put("deviceId",map.get("deviceId"));
        parmap.put("channelId",map.get("channelId"));
        parmap.put("operation",map.get("operation"));
        parmap.put("duration",map.get("duration"));
        JSONObject deviceBaseList = httpSend.execute(parmap, "controlMovePTZ");
        System.out.println(deviceBaseList);
        return new Result(ResultCode.SUCCESS);

    }






















    public  String   getToken(){
        UserManagerImpl userManager = new UserManagerImpl();
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        JSONObject json = userManager.accessToken(paramsMap);

        JSONObject jsonResult = json.getJSONObject("result");
        JSONObject jsonData = jsonResult.getJSONObject("data");
        String token = jsonData.getString("accessToken");

        return token;

    }















}
