package com.lovenav.controller;

import com.lovenav.entity.Attachment;
import com.lovenav.entity.Config;
import com.lovenav.service.AttachmentService;
import com.lovenav.service.ConfigService;
import org.apache.catalina.core.ApplicationContext;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import java.io.*;
import java.util.HashMap;

import static java.lang.System.out;

@RestController
public class AttachmentController {
    //附件上传
    @Autowired
    AttachmentService attachmentService;
    @Autowired
    ConfigService configService;
    //上传附件
    @RequestMapping("/uploadfile")
    public HashMap<String, String> uploadFile(MultipartFile multipartFile,String cate,Config config) throws IOException {//@RequestPart("photos") MultipartFile multipartFile
        HashMap<String,String> map=new HashMap<>();
        Long id=null;
        if (multipartFile==null){
            map.put("msg","文件不能为空！");
            return map;
        }
        out.println(multipartFile.getOriginalFilename());
        map=attachmentService.upload(multipartFile,cate,id);
        config.setValue(map.get("id"));
        config.setType("image");
        map.put("msg",configService.addConfig(config));
        return map;
    }



    @GetMapping(value = "/getAttachment",produces =MediaType.IMAGE_JPEG_VALUE)//,produces = MediaType.IMAGE_JPEG_VALUE
    @ResponseBody
    public ResponseEntity getAttachment(Config config) throws Exception {
        byte[] bytes1 ;
        Config config1=configService.selectAlreadyExist(config);
        HttpHeaders headers = new HttpHeaders();
        if (config1==null){
            bytes1= "没有该Config!".getBytes();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return new ResponseEntity<byte[]>(bytes1, headers, HttpStatus.NOT_FOUND);
        }
        Attachment attachment =attachmentService.selectAttachment(Long.valueOf(config1.getValue()));
        if (attachment==null){
            bytes1= "没有该Attachment!".getBytes();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return new ResponseEntity<byte[]>(bytes1, headers, HttpStatus.NOT_FOUND);
        }
        String fileUrl=attachment.getPath()+attachment.getFileName()+attachment.getSuffix();
        out.println(fileUrl);
        out.println(attachment.getSuffix().replace(".",""));
        File file = new File(fileUrl);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        //设置ContentType的值 IMAGE_JPEG在浏览器返回图片
        if(attachment.getSuffix().replace(".","").equals("png")){
            headers.setContentType(MediaType.IMAGE_PNG);
        }else if (attachment.getSuffix().replace(".","").equals("jpeg")){
            headers.setContentType(MediaType.IMAGE_JPEG);
        }else{
            bytes= "没有该文件!".getBytes();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.NOT_FOUND);
        }
        // 内容是字节流
        return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);

    }


}
