package easydo.technology.controller;

import com.alibaba.fastjson.JSONObject;
import easydo.technology.components.JdbcClient;
import easydo.technology.exception.BizException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.MultipartHttpServletRequest;
import zmo.technology.lite.core.ZeroException;
import zmo.technology.lite.core.ZeroFolders;
import zmo.technology.lite.core.ZeroRequestGuider;
import zmo.technology.lite.processors.ZeroFolderProcessor;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.Part;
import javax.sql.DataSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Collection;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    DataSource dataSource;
    @Resource
    JdbcClient jdbcClient;

    @PostMapping("/upload")
    public Object handleFileUpload(MultipartHttpServletRequest request) throws Exception {
        Connection connection = null;
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            connection = dataSource.getConnection();
            ZeroRequestGuider guider = ZeroRequestGuider.build();
            guider.setConnection(connection);

            Collection<Part> parts = request.getParts();

            JSONObject res = new JSONObject();
            JSONObject expands = new JSONObject();
            if (parts.isEmpty()) {
                throw new ZeroException("it is empty");
            }

            ZeroFolderProcessor processor = ZeroFolderProcessor.build(guider);
            for (Part part : parts) {
                inputStream = part.getInputStream();
                outputStream = new ByteArrayOutputStream();

                int len = -1;
                byte[] buffer = new byte[1024];
                while ((len = inputStream.read(buffer)) != -1)
                    outputStream.write(buffer, 0, len);

                ZeroFolders.ZeroFolderEntity entity = new ZeroFolders.ZeroFolderEntity();
                entity.getFeatures().put("size", part.getSize());
                entity.setName(part.getSubmittedFileName());
                entity.setMineType(part.getContentType());
                entity.setDatas(outputStream.toByteArray());
                processor.addFolder(entity);
                expands.put(part.getName(), entity.uuIDString());
            }
            res.put("code", 200);
            res.put("expands", expands);
            res.put("message", "success");
            return new ResponseEntity<>(res, HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
            outputStream.close();
            inputStream.close();
        }
    }


}
