package com.winsun.utils;

import com.winsun.constant.FilePath;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

/**
 * 二维码海报工具类
 * @Author: chancy
 * @Date: 2020/3/12 14:33
 */
@Slf4j
public class PicturesUtil {

    /**
     * 上传图片
     * <p>
     * 前端传来图片必须为jpeg格式
     * @param file 图片base64字符串
     * @param path 图片上一级文件夹名
     * @param pathType 图片上一级文件夹名
     * @param
     * @return
     */
    public static String uploadPictures(String file, String path,String pathType){
        if(StringUtils.isBlank(path)){
            path = "temp";
        }
        Date date = new Date();
        SimpleDateFormat yyMMdd = new SimpleDateFormat("yyMMdd");
        String h = yyMMdd.format(date.getTime());
        String filePath = null;
        String[] multipartFiles = new String[]{file};
        for (int i = 0; i < multipartFiles.length; i++) {
            if (!multipartFiles[i].isEmpty()) {
                String base64Data = multipartFiles[i].split(",")[1];
                String houzhui = multipartFiles[i].split(",")[0].split("/")[1].split(";")[0];
                String  filePath1 = FilePath.PATH.getValue()+ pathType+ "/"  + h + "/" + path;
                File dir = new File(filePath1);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                //enclosure/images/idCard/200525/temp/1590395528186.jpeg
                filePath = FilePath.PATH.getValue()+ pathType+ "/"+ h + "/" + path + "/" + date.getTime() + "." + houzhui;

                //byte[] bytes = Base64.getUrlDecoder().decode(base64Data);
                //byte[] bytes =new BASE64Decoder().decodeBuffer(base64Data);
                //byte[] bytes = Base64.getDecoder().decode(base64Data);
                byte[] bytes = Base64.getMimeDecoder().decode(base64Data.replace(" ","+"));
                        FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(filePath);
                    fos.write(bytes);
                } catch (IOException e) {
                    log.error("图片上传异常",e.getMessage());
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            log.error("图片上传异常",e.getMessage());
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        String enclosure = filePath.replace("enclosure", "manager/ciop");
        return enclosure;
    }


    /**
     * 海报删除图片
     * @param fileName 删除图片地址
     * @return
     */
    public static boolean deletePictures(String fileName){
        File file = new File(fileName.replace("manager/ciop","enclosure"));
        Boolean flag = false;
        //判断文件是否存在
        if (file.exists() == true){
            flag = file.delete();
            if (flag){
                log.info("成功删除海报背景图片"+file.getName());
            }else {
                log.info("海报背景图片删除失败");
            }
        }else {
            log.info("图片不存在，终止操作");
        }
        return flag;
    }
}
