package com.om.controller.admin;

import com.om.utils.HuaweiObsUtil;
import com.om.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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 javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(tags = "文件上传控制类")
public class UtilController {
    //人脸信息储存位置
    @Value("${files.upload.path}")
    private String fileUploadPath;
    //临时照片存储位置
    @Value("${photo.tmp}")
    private String tmpPhoto;

    private final HuaweiObsUtil huaweiObsUtil;

    public UtilController(HuaweiObsUtil huaweiObsUtil) {
        this.huaweiObsUtil = huaweiObsUtil;
    }

    @ApiOperation("文件下载接口")
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(fileUploadPath + name);
            //输出流，通过输出流将文件写回浏览器，在浏览器展示图片
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/png");
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("图片上传接口")
    @PostMapping("/photo")
    public Result photo(@RequestParam("file") MultipartFile file,
                        HttpServletRequest request,
                        @RequestParam("id") Integer id) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile headerImage = multipartRequest.getFile("file");
        String fileName = headerImage.getOriginalFilename();

        //根据当前时间生成文件名
        fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + "." + fileName;
        // 确定文件存放的路径
        File dest = new File(fileUploadPath + fileName);
        //判断当前目录是否存在
        if (!dest.exists()) {
            //目录不存在，需要创建
            dest.mkdir();
        }
        try {
            // 存储文件
            headerImage.transferTo(dest);
        } catch (IOException e) {
            throw new RuntimeException("上传文件失败,服务器发生异常!", e);
        }

        System.out.println("文件名：" + fileName);

        return Result.succ(fileName);
    }

    @ApiOperation("视频下载接口")
    @GetMapping("/downloadv")
    public void downloadv(String name, HttpServletResponse response) {
        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(fileUploadPath + name);
            //输出流，通过输出流将文件写回浏览器，在浏览器展示图片
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("video/mp4");
            int len;
            byte[] bytes = new byte[4096];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("视频上传接口")
    @PostMapping(value = "/video",headers="content-type=multipart/form-data")
    public Result video(@RequestParam("file") MultipartFile file,
                        HttpServletRequest request,
                        @RequestParam("prefix") String prefix) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile headerImage = multipartRequest.getFile("file");
        if (headerImage != null) {
            String fileUrl = huaweiObsUtil.upload(headerImage, prefix);
            if (fileUrl == null) return Result.fail("上传失败!");
            else return Result.succ(fileUrl);
        } else {
            return Result.fail("文件不正确!");
        }
//      以下是上传到本服务器的代码：
//        String fileName = headerImage.getOriginalFilename();
//
//        //根据当前时间生成文件名
//        fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + "." + fileName;
//        // 确定文件存放的路径
//        File dest = new File(fileUploadPath + fileName);
//        //判断当前目录是否存在
//        if (!dest.exists()) {
//            //目录不存在，需要创建
//            dest.mkdir();
//        }
//        try {
//            // 存储文件
//            headerImage.transferTo(dest);
//        } catch (IOException e) {
//            throw new RuntimeException("上传文件失败,服务器发生异常!", e);
//        }
//
//        System.out.println("文件名：" + fileName);
//
//        return Result.succ(fileName);
    }
}
