package com.fastbee.deviceData.api.yingshiyun.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class YingshiyunService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //获取鉴权
    public  String getAuth(){
        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("yingshiyun:user:accessToken"))){
            return stringRedisTemplate.opsForValue().get("yingshiyun:user:accessToken");
        }
        // 鉴权请求
        String AUTH_URL = "https://open.ys7.com/api/lapp/token/get";
        String appKey = "b21f910dc7044d668e7625a3c0392e62";
        String appSecret  = "b4beff5f8f6694dd6993c8c5b618417b";
        // 构建请求体参数
        String body = StrUtil.format("appKey={}&appSecret={}", appKey, appSecret);
        HttpResponse response = HttpRequest.post(AUTH_URL)
                .body(body).execute();

        JSONObject jsonObject = JSONUtil.parseObj(response.body());
//        System.err.println(jsonObject);
        if( !jsonObject.get("code").toString().equals("200")){
            throw new RuntimeException("获取萤石云token失败！");
        }
        JSONObject data = JSONUtil.parseObj(jsonObject.get("data"));
        String accessToken = data.get("accessToken").toString();

        stringRedisTemplate.opsForValue().set("yingshiyun:user:accessToken",accessToken, 60*60*24*6, TimeUnit.MILLISECONDS);
        return accessToken;
    }

    //获取视频播放地址
    public Map<String,Object> getVideoPlayUrl(String deviceSerial){
        String accessToken = getAuth();
//        System.err.println(accessToken);
        String VIDEO_PLAY_URL = "https://open.ys7.com/api/lapp/v2/live/address/get";
        String body = StrUtil.format("accessToken={}&deviceSerial={}&protocol={}",accessToken ,deviceSerial,"2");

        HttpResponse response = HttpRequest.post(VIDEO_PLAY_URL)
                .body(body).execute();
        JSONObject jsonObject = JSONUtil.parseObj(response.body());
        Map<String,Object> map = new HashMap<>();
        if(!jsonObject.get("code").toString().equals("200")){
           return map;
        }
//        System.err.println(jsonObject);
        JSONObject data = JSONUtil.parseObj(jsonObject.get("data"));
//        System.err.println(data);
        String playUrl = data.get("url").toString();


        map.put("playUrl",playUrl);
        map.put("accessToken",accessToken);

        return map;
    }





    public static void main(String[] args) {
        // 鉴权请求
        String AUTH_URL = "https://open.ys7.com/api/lapp/token/get";
        String appKey = "b21f910dc7044d668e7625a3c0392e62";
        String appSecret  = "b4beff5f8f6694dd6993c8c5b618417b";
        // 构建请求体参数
        String body = StrUtil.format("appKey={}&appSecret={}", appKey, appSecret);
        HttpResponse response = HttpRequest.post(AUTH_URL)
                .body(body).execute();
        JSONObject jsonObject = JSONUtil.parseObj(response.body());
        System.err.println(jsonObject);
        if( !jsonObject.get("code").toString().equals("200")){
            throw new RuntimeException("获取萤石云token失败！");
        }
        JSONObject data = JSONUtil.parseObj(jsonObject.get("data"));
        String accessToken = data.get("accessToken").toString();
        System.err.println(accessToken);
    }
}
