package cn.superdesk.uniorder.openapicall.superdeskapp.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.security.superdeskapp.sign.OpenDataApiRequestEntity;
import cn.superdesk.uniorder.common.security.superdeskapp.sign.OpenDataApiSignUtils;
import cn.superdesk.uniorder.openapicall.superdeskapp.dto.RespSuperdeskOpenData;
import cn.superdesk.uniorder.openapicall.superdeskapp.restapi.SuperdeskCloudOpenDataCallApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import retrofit2.Call;

import java.io.IOException;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/10/28 下午12:41
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/28 下午12:41
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class SuperdeskCloudOpenDataCallService {

	@Autowired
	private SuperdeskCloudOpenDataCallApi superdeskCloudOpenDataCallApi;

	@Value("${test.superdesk.cloud.opendata.accessToken}")
	private String accessToken;
	@Value("${test.superdesk.cloud.opendata.accessKeyId}")
	private String accessKeyId;
	@Value("${test.superdesk.cloud.opendata.accessKeySecret}")
	private String accessKeySecret;


	public Long dashboardTvGetUserCount(String token) {

		Long count;

		RespSuperdeskOpenData<Long> response = null;


		String appKey    = accessKeyId;
		String appSecret = accessKeySecret;

		String timestamp = DateUtil.now();

		String v      = "1.0";
		String format = "json";
		String locale = "cn";

//		String sign = SHA1.getSuperdeskCloudOpenDataCallOpiSign(appKey, timestamp, v, format, locale, appSecret);


		OpenDataApiRequestEntity extRequestEntity = new OpenDataApiRequestEntity();
		extRequestEntity.setAppKey(appKey);
//        extRequestEntity.setContent(content);
		extRequestEntity.setFormat(format);
		extRequestEntity.setLocale(locale);
		extRequestEntity.setTimestamp(timestamp);
		extRequestEntity.setV(v);


		String sign = OpenDataApiSignUtils.getSign(extRequestEntity, appSecret);

		log.info("dashboardTvGetUserCount \n{}\n{}", JSONUtil.toJsonStr(extRequestEntity), sign);


		try {

			Call<RespSuperdeskOpenData<Long>> callResponse = superdeskCloudOpenDataCallApi.dashboardTvGetUserCount(
//				ReqSuperdeskOpenData.builder()
//						.appKey(appKey)
//						.timestamp(timestamp)
//						.sign(sign)
//						.v(v)
//						.format(format)
////						.local(local)
//						.build()

					appKey,
					timestamp,
					sign,
					v,
					format,
					locale

			);

//		{"code":503,"msg":"非法签名"}

			response = callResponse.execute().body();
			count = response.getData();

			if (ObjectUtil.isNull(count)) {
				count = 0L;
			}

			if (response.getCode().compareTo(200) != 0) {
				count = 0L;
			}

			log.info("dashboardTvGetUserCount response\n{}", JSONUtil.toJsonStr(response));
		} catch (com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException retrofitException) {

			count = 0L;

		} catch (IOException ioe) {
			ioe.printStackTrace();
			count = 0L;
		}

		return count;
	}

}
