package cn.lihongjie.coal.spring.config;

import com.obs.services.ObsClient;

import lombok.extern.slf4j.Slf4j;

import org.springframework.context.annotation.Bean;

//@Component
@Slf4j
public class HwCloudConfig {

//    @Autowired 
    HwCloudProperty hwCloudProperty;

    @Bean
    public ObsClient obsClient() {
        ObsClient client =
                new ObsClient(
                        hwCloudProperty.getObs().getAk(),
                        hwCloudProperty.getObs().getSk(),
                        hwCloudProperty.getObs().getZones().stream()
                                .filter(x -> x.getCode().equals(hwCloudProperty.getObs().getZone()))
                                .findFirst()
                                .orElseThrow(() -> new RuntimeException("zone not found"))
                                .getEndpoint());

//        if (!client.headBucket(hwCloudProperty.getObs().getBucketName())) {
//            CreateBucketRequest request = new CreateBucketRequest();
//            request.setBucketName(hwCloudProperty.getObs().getBucketName());
//            request.setBucketType(BucketTypeEnum.OBJECT);
//            request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
//            ObsBucket bucket = client.createBucket(request);
//            log.info("bucket created: {}", bucket.getBucketName());
//        }

        return client;
    }
}
