package com.fdkankan.rubbersheeting;

import com.tencentcloudapi.as.v20180419.AsClient;
import com.tencentcloudapi.as.v20180419.models.ScaleOutInstancesRequest;
import com.tencentcloudapi.as.v20180419.models.ScaleOutInstancesResponse;
import com.tencentcloudapi.as.v20180419.models.StopAutoScalingInstancesRequest;
import com.tencentcloudapi.as.v20180419.models.StopAutoScalingInstancesResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@ConditionalOnProperty(name = "scaling.type",havingValue = "cos")
public class TencentScalingService implements ScalingService{

    @Value("${accessKey.id:xxx}")
    private static String accessKeyId;
    @Value("${accessKey.secret:xxx}")
    private static String accessKeySecret;
    @Value("${scaling.region:ap-guangzhou}")
    private String region;
    @Value("${scaling.group.id:asg-hv68oe36}")
    private String groupId;

    private AsClient getAsClient(){
        Credential cred = new Credential(accessKeyId, accessKeySecret);
        AsClient client = new AsClient(cred, region);
        return client;
    }


    @Override
    public String createEcs(){
        AsClient asClient = this.getAsClient();
        ScaleOutInstancesRequest req = new ScaleOutInstancesRequest();
        req.setAutoScalingGroupId(groupId);
        req.setScaleOutNumber(1L);
        ScaleOutInstancesResponse resp = null;
        try {
            resp = asClient.ScaleOutInstances(req);
        } catch (TencentCloudSDKException e) {
            log.error("启动弹性伸缩示例失败", e);
            return null;
        }
        return ScaleOutInstancesResponse.toJsonString(resp);
    }

    @Override
    public String createEcs(String ruleAri) {
        return null;
    }

    @Override
    public String createEcsByConfig(Map<String, String> configs) {
        return null;
    }



    @Override
    public String createEcsByConfig(String ruleAri) {
        return null;
    }


    @Override
    public  String deleteEcs(String id){
        AsClient asClient = this.getAsClient();
        StopAutoScalingInstancesRequest req2 = new StopAutoScalingInstancesRequest();
        req2.setAutoScalingGroupId(groupId);
        req2.setInstanceIds(new String[]{id});
        req2.setStoppedMode("STOP_CHARGING");
        StopAutoScalingInstancesResponse response = null;
        try {
            response = asClient.StopAutoScalingInstances(req2);
        } catch (TencentCloudSDKException e) {
            log.error("停止弹性伸缩示例失败,实例id:{}",id);
            return null;
        }
        return StopAutoScalingInstancesResponse.toJsonString(response);
    }

    @Override
    public String deleteEcs(String groupId, String instanceId) {
        return null;
    }
}
