package edge_service

import (
	"fmt"
	"io/ioutil"
	"iot_manager_service/config"
	"net/http"
	"net/url"
)

// ForRewindService  回放记录
type ForRewindService struct {
}

// getRewindApi 公用请求接口
func (f *ForRewindService) getRewindApi(url string) (string, error) {
	//fmt.Printf("url = %v", url)
	method := "GET"

	client := &http.Client{}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return "", nil
	}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return "", nil
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return "", nil
	}
	fmt.Println(string(body))
	return string(body), err
}

// GetRewindText 请求回话加密数据
func (f *ForRewindService) GetRewindText(streamId string, date string) (string, error) {
	cfg := config.Instance()
	api := cfg.Foreign.SecurityRewindUrl + "/list/%v/%v"
	url2 := fmt.Sprintf(api, (streamId), url.QueryEscape(date))
	return f.getRewindApi(url2)
}
