package com.iformall.controller.sys;


import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.iformall.common.ResultData;
import com.iformall.config.AwsProperty;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.MallResource;
import com.iformall.service.MallResourceService;
import com.iformall.utils.Constant;
import com.iformall.utils.ImgUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.List;

@RestController
@RequestMapping(value = "upload")
@Api(description = "文件上传接口")
public class UploadController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AwsProperty awsProperty;

    @Autowired
    private String fmUploadDir;

    @Autowired
    private MallResourceService mallResourceService;

    private AmazonS3 s3 = null;

    private ResultData awsUpload(InputStream inputStream, ObjectMetadata metadata, String fileName, String tenantId) {
        ResultData data = new ResultData();
        try {
            if(s3 == null) {
                s3 = AmazonS3ClientBuilder.standard()
                        .withRegion(awsProperty.getClientRegion())
                        .withCredentials(new AWSCredentialsProvider() {
                            @Override
                            public AWSCredentials getCredentials() {
                                return new BasicAWSCredentials(awsProperty.getAccess(), awsProperty.getSecret());
                            }

                            @Override
                            public void refresh() {

                            }
                        })
                        .build();
            }

            s3.putObject(
                    new PutObjectRequest(awsProperty.getBucketName(), fileName, inputStream, metadata)
                            .withCannedAcl(CannedAccessControlList.PublicRead));
            URL url = s3.getUrl(awsProperty.getBucketName(), fileName);
            logger.info(url.toString());

            MallResource mr = new MallResource();
            mr.setTenantId(tenantId);
            mr.setBucket(awsProperty.getBucketName());
            mr.setS3Key(fileName);
            mr.setUrl(url.toString());
            mallResourceService.saveOrUpdate(mr);

            data.code = ResultData.SUCCESS;
            Map<String, String> map = new HashMap<>();
            map.put("url", url.toString());
            data.data = map;
        } catch (AmazonServiceException ase) {
            data.code = ResultData.ERROR;
            logger.warn("Caught an AmazonServiceException, which " +
                    "means your request made it " +
                    "to Amazon S3, but was rejected with an error response" +
                    " for some reason.");
            logger.warn(ase.getMessage());
            data.code = ResultData.ERROR;
            data.message = "上传失败";
        } catch (AmazonClientException ace) {
            data.code = ResultData.ERROR;
            logger.warn("Caught an AmazonClientException, which " +
                    "means the client encountered " +
                    "an internal error while trying to " +
                    "communicate with S3, " +
                    "such as not being able to access the network.");
            logger.warn("Error Message: " + ace.getMessage());
            data.code = ResultData.ERROR;
            data.message = "上传失败";
        } catch (Exception ioe) {
            data.code = ResultData.ERROR;
            logger.warn("Caught an IOException: " + ioe.getMessage());
            data.code = ResultData.ERROR;
            data.message = "上传失败";
        }
        return data;
    }

    /**
     * 上传文件
     *
     * @param multiReq
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/awsFileUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation("上传文件")
    public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq) throws Exception {
        logger.info("[" + getIpAddr() + "] UploadController::awsfileUpload");

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(multiReq.getContentType());
        metadata.setContentLength(multiReq.getSize());

        FileOutputStream fos = null;
        BufferedInputStream fs = null;

        String fileName = UUID.randomUUID().toString();
        int dot = multiReq.getOriginalFilename().lastIndexOf('.');
        if (dot >= 0) {
            fileName = getTenantId() + "/" + fileName + multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length());
        } else {
            fileName = getTenantId() + "/" + fileName;
        }

        ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, getTenantId());
        return data;
    }

    /**
     * 图片上传
     *
     * @param multiReq
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/awsImgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation("上传图片")
    public ResultData awsImgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception{
        logger.info("[" + getIpAddr() + "] UploadController::awsImgUpload");

        String fileName = UUID.randomUUID().toString();
        String tempFileName  = fileName;
        String fileFormat = "";

        int dot = multiReq.getOriginalFilename().lastIndexOf('.');
        if (dot >= 0) {
            fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length());
            fileName = getTenantId() + "/" + fileName + fileFormat;
        } else {
            fileName = getTenantId() + "/" + fileName;
        }

        System.out.println(fileName);

        FileOutputStream fos = null;
        String imgFormat = ImgUtil.getImgFormat(fileFormat);
        if(StringUtils.isNotBlank(imgFormat)) {
            String localImgFileName = fmUploadDir + tempFileName + "-1" +  fileFormat;
            String localNewImgFileName = fmUploadDir + tempFileName + fileFormat;
            BufferedInputStream fs = null;
            File localFile = new File(localImgFileName);
            fos = new FileOutputStream(localFile);
            fs = (BufferedInputStream) multiReq.getInputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fs.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            fs.close();

            File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat);
            // 删除本地缓存
            localFile.delete();
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(multiReq.getContentType());
            metadata.setContentLength(newFile.length());
            ResultData data = awsUpload(new FileInputStream(newFile), metadata, fileName, getTenantId());
            // 删除本地缓存
            newFile.delete();
            return data;
        } else {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(multiReq.getContentType());
            metadata.setContentLength(multiReq.getSize());
            ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, getTenantId());
            return data;
        }
    }

    /**
     * 多文件上传
     *
     * @param files
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/awsFilesUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation("多文件上传")
    public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) throws Exception {
        logger.info("[" + getIpAddr() + "] UploadController::awsFilesUpload");

        if(files.length > 0){
            ResultData data = new ResultData();
            List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
            for(MultipartFile multipartFile: files) {
                Map<String, String> map = new HashMap<>();

                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentType(multipartFile.getContentType());
                metadata.setContentLength(multipartFile.getSize());

                FileOutputStream fos = null;
                BufferedInputStream fs = null;

                map.put("key", multipartFile.getOriginalFilename());

                String fileName = UUID.randomUUID().toString();
                int dot = multipartFile.getOriginalFilename().lastIndexOf('.');
                if (dot >= 0) {
                    fileName = getTenantId() + "/" + fileName + multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length());
                } else {
                    fileName = getTenantId() + "/" + fileName;
                }

                ResultData data1 = awsUpload(multipartFile.getInputStream(), metadata, fileName, getTenantId());
                if(data1.code == ResultData.SUCCESS) {
                    Map _data = (Map)data1.data;
                    map.put("url", (String) _data.get("url"));
                    dataList.add(map);
                } else {
                    // 部分成功
                    data.code = ResultData.SUCCESS;
                    data.data = dataList;
                    return data;
                }
            }
            data.code = ResultData.SUCCESS;
            data.data = dataList;
            return data;

        }
        return new ResultData();
    }

    /**
     * C端上传图片文件
     *
     * @param multiReq
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/cimgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation("C端上传图片文件")
    public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception {
        logger.info("[" + getIpAddr() + "] UploadController::cimgUpload");

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(multiReq.getContentType());
        metadata.setContentLength(multiReq.getSize());

        String fileName = multiReq.getOriginalFilename();
        fileName = "cimg/" + fileName;
        ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, "cimg");
        return data;
    }

    /**
     * AWS
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/lists")
    @ApiOperation("aws列举文件")
    public ResultData awsList() throws Exception {
        logger.info("[" + getIpAddr() + "] UploadController::cimgUpload");

        try{
            ObjectListing ol = s3.listObjects(awsProperty.getBucketName());
            List<S3ObjectSummary> objects = ol.getObjectSummaries();
            for (S3ObjectSummary os: objects) {
                logger.info(os.toString());
                break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return new ResultData();
    }

}
