package com.fs.course.service.impl;

import com.fs.common.core.domain.R;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.course.param.HuaweiCloudStatisticsParam;
import com.fs.course.service.IHuaweiCloudStatisticsService;
import com.huaweicloud.sdk.cdn.v2.region.CdnRegion;
import org.springframework.stereotype.Service;

import com.huaweicloud.sdk.cdn.v2.CdnClient;
import com.huaweicloud.sdk.cdn.v2.model.ShowDomainLocationStatsRequest;
import com.huaweicloud.sdk.cdn.v2.model.ShowDomainLocationStatsResponse;
import com.huaweicloud.sdk.cdn.v2.model.ShowDomainStatsRequest;
import com.huaweicloud.sdk.cdn.v2.model.ShowDomainStatsResponse;
import com.huaweicloud.sdk.cdn.v2.model.ShowTopUrlRequest;
import com.huaweicloud.sdk.cdn.v2.model.ShowTopUrlResponse;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.utils.JsonUtils;

import java.util.Arrays;


@Service
public class HuaweiCloudStatisticsServiceImpl implements IHuaweiCloudStatisticsService {
    // 区域
    public static final String REGION_NORTH4 = "cn-north-4";
    public static final String REGION_NORTH1 = "cn-north-1";
    public static final String REGION_EAST2 = "cn-east-2";
    private static final String END_POINT = "cdn.myhuaweicloud.com";

    // ak/sk
    private static final String AK = "K2UTJGIN7UTZJR2XMXYG";
    private static final String SK = "sbyeNJLbcYmH6copxeFP9pAoksM4NIT9Zw4x0SRX";

    @Override
    public R DomainStatistics(HuaweiCloudStatisticsParam param) {
        // 认证用的ak和sk直接写到代码中有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
        // 本示例以ak和sk保存在环境变量中来实现身份验证为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。

        ICredential auth = new GlobalCredentials()
                .withAk(AK)
                .withSk(SK);
        CdnClient client = CdnClient.newBuilder()
                .withCredential(auth)
                .withRegion(CdnRegion.CN_NORTH_1)
                .build();
        // 查询域名统计数据
        R r1 = showDomainStats(client,param);
        R r2 = showDomainDetailStats(client,param);
        if (r1.get("code").equals(200) && r2.get("code").equals(200)){
            return R.ok().put("summary",r1.get("data")).put("detail",r2.get("data"));
        }
        return R.error("获取统计异常");
    }


    public R showDomainLocationStats(CdnClient client,HuaweiCloudStatisticsParam param) {
        ShowDomainLocationStatsRequest request = new ShowDomainLocationStatsRequest();
        BeanUtils.copyProperties(param,request);
        try {
            ShowDomainLocationStatsResponse response = client.showDomainLocationStats(request);
            int resultCode = response.getHttpStatusCode();
            if (resultCode == 200) {
                System.out.println("show domain location stats success");
                return R.ok().put("data",response);
            }
            System.out.println(JsonUtils.toJSON(response));
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
            return R.error(e.getMessage());
        } catch (ServiceResponseException e) {
            printExceptionMsg(e);
            return R.error(e.getMessage());
        }
        return R.error("获取华为云统计数据异常");
    }

    public R showDomainStats(CdnClient client,HuaweiCloudStatisticsParam param) {
        ShowDomainStatsRequest request = new ShowDomainStatsRequest();
        BeanUtils.copyProperties(param,request);
        request.setAction("summary");
        try {
            ShowDomainStatsResponse response = client.showDomainStats(request);
            int resultCode = response.getHttpStatusCode();
            if (resultCode == 200) {
                System.out.println("show domain stats success");
                return R.ok().put("data",response);
            }
            System.out.println(JsonUtils.toJSON(response));
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
            return R.error(e.getMessage());
        } catch (ServiceResponseException e) {
            printExceptionMsg(e);
            return R.error(e.getMessage());
        }
        return R.error("获取华为云统计数据异常");
    }

    public R showDomainDetailStats(CdnClient client,HuaweiCloudStatisticsParam param) {
        ShowDomainStatsRequest request = new ShowDomainStatsRequest();
        BeanUtils.copyProperties(param,request);
        request.setAction("detail");
        try {
            ShowDomainStatsResponse response = client.showDomainStats(request);
            int resultCode = response.getHttpStatusCode();
            if (resultCode == 200) {
                System.out.println("show domain detail stats success");
                return R.ok().put("data",response);
            }
            System.out.println(JsonUtils.toJSON(response));
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
            return R.error(e.getMessage());
        } catch (ServiceResponseException e) {
            printExceptionMsg(e);
            return R.error(e.getMessage());
        }
        return R.error("获取华为云统计数据异常");
    }

    public R showTopUrl(CdnClient client,HuaweiCloudStatisticsParam param) {

        ShowTopUrlRequest request = new ShowTopUrlRequest();
        request.setDomainName(param.getDomainName());
        request.setStatType(param.getStatType());
        request.setStartTime(param.getStartTime());
        request.setEndTime(param.getEndTime());
        request.setServiceArea(param.getServiceArea());
        request.setEnterpriseProjectId(param.getEnterpriseProjectId());

        try {
            ShowTopUrlResponse response = client.showTopUrl(request);
            int resultCode = response.getHttpStatusCode();
            if (resultCode == 200) {
                System.out.println("show top url success");
                return R.ok().put("data",response);
            }
            System.out.println(JsonUtils.toJSON(response));
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
            return R.error(e.getMessage());
        } catch (ServiceResponseException e) {
            printExceptionMsg(e);
            return R.error(e.getMessage());
        }
        return R.error("获取华为云统计数据异常");
    }

    private static void printExceptionMsg(ServiceResponseException e) {
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getRequestId());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }

}
