package com.zhiqiyun.open.mvc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiqiyun.open.annotation.Permission;
import com.zhiqiyun.open.core.models.UploadResult;
import com.zhiqiyun.open.core.models.statistics.UploadFileInfo;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.core.service.UploadFileInfoService;
import com.zhiqiyun.open.exception.UploadException;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.params.UploaderBase64Param;
import com.zhiqiyun.open.mvc.params.statistics.QueryUploadFileInfoParams;
import com.zhiqiyun.open.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicMatch;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/uploader")
public class UploaderController {

    @Autowired
    private UploadFileInfoService uploadFileInfoService;

    @Permission(value = "upload.file.find", tags = "查询多媒体信息")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryUploadFileInfoParams params) {

        QueryWrapper<UploadFileInfo> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(params.getOriginalFileName())) {
            wrapper.like("original_file_name", params.getOriginalFileName());
        }
        if (params.getCreatedTime() != null) {
            wrapper.between("created_time", params.getCreatedTime().getStatDate(), params.getCreatedTime().getEndDate());
        }

        Page<UploadFileInfo> page = params.getPage();
        page.addOrder(OrderItem.desc("id"));

        Page<UploadFileInfo> resultData = this.uploadFileInfoService.page(page, wrapper);

        return Result.instance(Result.Code.SUCCESS).setData(resultData);
    }

    @Permission(value = "upload.file.delete", tags = "删除多媒体信息")
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids) {
        this.uploadFileInfoService.removeByIds(ids);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(tags = "上传图片", writeLog = false)
    @PostMapping(value = "/handle/base64File")
    public Object handleBase64File(@RequestBody UploaderBase64Param param) throws IOException {

        if (StringUtils.isBlank(param.getBase64File())) {
            log.warn("没有添加上传文件");
            return Result.instance("没有添加上传文件");
        }
        byte[] fileBuff = Base64.decodeBase64(param.getBase64File());

        String originalFilename = param.getFilename();
        if (StringUtils.isBlank(originalFilename)) {
            originalFilename = String.format("%s.%s", DigestUtils.md5Hex(fileBuff), "png");
        }

        UploadResult uploadResult = this.uploadFileInfoService.handle(originalFilename, fileBuff);
        if (uploadResult.isSuccess()) {
            return Result.instance(Result.Code.SUCCESS).setData(uploadResult);
        } else {
            return Result.instance(Result.Code.MESSAGE_ERROR, uploadResult.getMessage());
        }
    }

}
