package com.gree.mall.manager.utils.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.*;
import com.gree.mall.manager.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;


@Slf4j
public class OSSUtil {


    // 用户相关文件夹
    public static final String ALIOSS_BUCKETNAME= "zfire-train";
    public static final String OSS_TYPE_PIC = "train/pic/";
    public static final String AliOSSEndPoint = "http://oss-cn-guangzhou.aliyuncs.com";
    public static final String AliOSSEndPointNohttp = "oss-cn-guangzhou.aliyuncs.com";
    public static final String accessUrl = "https://zfire-train.oss-cn-guangzhou.aliyuncs.com/";
//    public static final String OSSAccessKeyID = "LTAI4G3XTLh8G8DiTpXHpWgt";
//    public static final String OSSAccessKeySecret = "LYe8uf7Lg9WxJ1xejk45qnPFqxXLzN";

    private static OSS ossClientPri = new OSSClientBuilder().build(AliOSSEndPoint,
            Constant.Ali.ACCESS_KEY_ID, Constant.Ali.ACCESS_KEY_SECERT);

    public static boolean uploadFile(String ossObjectKey, byte[] file) throws IOException {

        PutObjectResult putObjectResult = null;
        putObjectResult = ossClientPri.putObject(new PutObjectRequest(ALIOSS_BUCKETNAME, ossObjectKey, new ByteArrayInputStream(file)));
        log.info("putObjectResult" + putObjectResult);
        if (putObjectResult != null) {
            ResponseMessage responseMessage = putObjectResult.getResponse();
            if (responseMessage != null) {
                log.error(responseMessage.getErrorResponseAsString());
            }
            return true;
        }

        return false;
    }

    public static boolean uploadFile(String ossObjectKey,File file) throws IOException {
        if (file == null) {
            return false;
        }

        PutObjectResult putObjectResult = ossClientPri.putObject(new PutObjectRequest(ALIOSS_BUCKETNAME, ossObjectKey, new FileInputStream(file)));
        log.info("putObjectResult" + putObjectResult);
        if (putObjectResult != null) {
            ResponseMessage responseMessage = putObjectResult.getResponse();
            if (responseMessage != null) {
                log.error(responseMessage.getErrorResponseAsString());
            }
            return true;
        }
        return false;
    }
    public static String getUrlWw(String key){
        OSS clientWw = new OSSClientBuilder().build(AliOSSEndPoint,Constant.Ali.ACCESS_KEY_ID, Constant.Ali.ACCESS_KEY_SECERT);
        // 设置URL过期时间为1小时
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
        GeneratePresignedUrlRequest generatePresignedUrlRequest ;
        generatePresignedUrlRequest =new GeneratePresignedUrlRequest(ALIOSS_BUCKETNAME, key);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = clientWw.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString();
    }


    public static String getUrlWw(String key, MediaType mediaType) {
        if (StringUtils.isEmpty(key)) {
            return "";
        }
        // 设置URL过期时间为1小时
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(ALIOSS_BUCKETNAME, key);
        request.setExpiration(expiration);
        if (mediaType != null) {
            request.setContentType(mediaType.toString());
        }

        URL url = new OSSClientBuilder()
                .build("https://" + AliOSSEndPointNohttp, Constant.Ali.ACCESS_KEY_ID, Constant.Ali.ACCESS_KEY_SECERT)
                .generatePresignedUrl(request);

        String resultUrl = url.toString();

        return resultUrl;

    }


}
