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

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.*;
import com.zfire.mall.manager.constant.Constant;
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 java.io.ByteArrayInputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;


@Service
@Slf4j
public class OSSUtil {

    @Value("${ali.access.key.id}")
    private String ACCESS_KEY_ID;
    @Value("${ali.access.key.secert}")
    private String ACCESS_KEY_SECERT;
    @Value("${ali.oss.bucket.name}")
    private String OSS_BUCKET_NAME;
    @Value("${ali.oss.endpoint.ww}")
    private String OSS_ENDPOINT_WW;
    @Value("${ali.oss.endpoint}")
    private String OSS_ENDPOINT;
    @Value("${ali.oss.type.pic}")
    private String OSS_TYPE_PIC;

    public  boolean uploadFile(String ossObjectKey, File file){
        OSS client = new OSSClientBuilder().build(OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
        PutObjectResult putObjectResult = client.putObject(new PutObjectRequest(OSS_BUCKET_NAME, ossObjectKey, file));
        log.info("putObjectResult" + putObjectResult);
        return true;
    }

    public  boolean uploadFile(String ossObjectKey, byte[] file){
        OSS client = new OSSClientBuilder().build(OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
        PutObjectResult putObjectResult = client.putObject(new PutObjectRequest(OSS_BUCKET_NAME, ossObjectKey, new ByteArrayInputStream(file)));
        log.info("putObjectResult" + putObjectResult);
        return true;
    }

    public  boolean uploadFile(String ossObjectKey,ByteArrayInputStream byteArrayInputStream){
        OSS client = new OSSClientBuilder().build(OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
        PutObjectResult putObjectResult =  client.putObject(new PutObjectRequest(OSS_BUCKET_NAME, ossObjectKey,byteArrayInputStream));
        log.info("putObjectResult" + putObjectResult);
        return true;
    }


    public  Map<String, String> getConfig() throws UnsupportedEncodingException {
        OSS client = new OSSClientBuilder().build(OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
        String host = "https://" + OSS_BUCKET_NAME + "." + OSS_ENDPOINT_WW; // host的格式为 bucketname.endpoint

//        OSSClient client = new OSSClient(endpoint, accessId, accessKey);
        long expireTime = 300;
        long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
        java.sql.Date expiration = new java.sql.Date(expireEndTime);
        PolicyConditions policyConds = new PolicyConditions();
        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
        policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY,OSS_TYPE_PIC);

        String postPolicy = client.generatePostPolicy(expiration, policyConds);
        byte[] binaryData = postPolicy.getBytes("utf-8");
        String encodedPolicy = BinaryUtil.toBase64String(binaryData);
        String postSignature = client.calculatePostSignature(postPolicy);

        Map<String, String> respMap = new LinkedHashMap<String, String>();
        respMap.put("OSSAccessKeyId", ACCESS_KEY_ID);
        respMap.put("expire", String.valueOf(expireEndTime / 1000));
        respMap.put("policy", encodedPolicy);
        respMap.put("signature", postSignature);
        respMap.put("id",ACCESS_KEY_ID);
        respMap.put("dir", OSS_TYPE_PIC);
        respMap.put("host", host);
        return respMap;
    }


    public  String getUrl(String key){
        OSS client = new OSSClientBuilder().build(OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
        // 设置URL过期时间为1小时
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
        GeneratePresignedUrlRequest generatePresignedUrlRequest ;
        generatePresignedUrlRequest =new GeneratePresignedUrlRequest(OSS_BUCKET_NAME, key);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString();
    }

    public  URL getUrl2(String key){
        OSS client = new OSSClientBuilder().build(OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
        // 设置URL过期时间为1小时
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
        GeneratePresignedUrlRequest generatePresignedUrlRequest ;
        generatePresignedUrlRequest =new GeneratePresignedUrlRequest(OSS_BUCKET_NAME, key);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
        return url;
    }


    public  String getUrlWw(String key){
        OSS clientWw = new OSSClientBuilder().build(OSS_ENDPOINT_WW,ACCESS_KEY_ID,ACCESS_KEY_SECERT);
        // 设置URL过期时间为1小时
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
        GeneratePresignedUrlRequest generatePresignedUrlRequest ;
        generatePresignedUrlRequest =new GeneratePresignedUrlRequest(OSS_BUCKET_NAME, key);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = clientWw.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString();
    }


}
