package com.community.huiju.controller;

import com.community.commom.mode.ResponseBean;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.service.ImageServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import sun.misc.BASE64Decoder;

import javax.sql.rowset.serial.SerialBlob;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weichaochao
 * @Title: ImageController
 * @Description: 图片上传以及获取url
 * @date 2018/10/31
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "图片操作API", description = "图片操作API")
public class ImageController extends BaseController {

    @Autowired
    private ImageServiceI imageService;

    @ApiOperation(value = "图片上传以及获取url", notes = "图片上传以及获取url")
    @PostMapping(value = "/uploadimage", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public ResponseBean uploadImgAndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) MultipartFile[] uploadFiles) throws Exception {
        ResponseBean responseBean = new ResponseBean();
        List<String> urls = new ArrayList<String>();
        for (MultipartFile uploadFile : uploadFiles){
            String url = imageService.getImageUrl(uploadFile);
            urls.add(url);
        }
        responseBean.addSuccess(urls);
        return responseBean;
    }

    @ApiOperation(value = "图片上传以及获取url", notes = "图片上传以及获取url")
    @PostMapping(value = "/uploadimageBase64")
    public ResponseBean uploadImgBase64AndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) String uploadFiles) throws Exception {
        ResponseBean responseBean = new ResponseBean();
        List<String> urls = new ArrayList<String>();
        // int index = uploadFiles.indexOf("data:image/jpeg;base64,");
        //uploadFiles = uploadFiles.substring(23);
        String[] split = uploadFiles.split("base64,");
        SerialBlob serialBlob = null;
        if (split.length > 1) {
            serialBlob = decodeToImage(split[1]);
        } else {
            serialBlob = decodeToImage(split[0]);
        }

        String url = imageService.getImageUrl(serialBlob.getBinaryStream());
        urls.add(url);
        responseBean.addSuccess(urls);
        return responseBean;
    }

    public static SerialBlob decodeToImage(String imageString) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] imageByte = decoder.decodeBuffer(imageString);
        return new SerialBlob(imageByte);
    }


}
