package com.zhangmeng.file.controller;


import com.zhangmeng.api.service.file.UploadControllerApi;
import com.zhangmeng.file.config.FileConfig;
import com.zhangmeng.file.feign.TokenFeign;
import com.zhangmeng.file.feign.UserFeignService;
import com.zhangmeng.file.service.FileInfoService;
import com.zhangmeng.file.service.UploadService;
import com.zhangmeng.file.utils.Base64DecodeMultipartFile;
import com.zhangmeng.file.utils.FastDfsClient;
import com.zhangmeng.model.base.baseController.BaseController;
import com.zhangmeng.model.entity.FileInfo;
import com.zhangmeng.model.vo.Result;
import com.zhangmeng.model.vo.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController implements UploadControllerApi {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    private FileConfig fileConfig;

    @Override
    @PostMapping("/file")
    public Result upload(MultipartFile file, FileInfo.UploadType type ) {
        return this.uploadService.uploadFile(type, file);
    }

    @Override
    @PostMapping("/base64")
    public Result upload(FileInfo.UploadType type,String base64) {
        MultipartFile file = null;
        if (base64 != null && !"".equals(base64)) {
            file = Base64DecodeMultipartFile.base64Convert(base64);
        }
        if (file == null) {
            return new Result(false, StatusCode.OK, "请选择文件在进行上传");
        }
        return this.uploadService.uploadFile(type, file);
    }

    @Override
    @GetMapping("/download")
    public Result download(String groupName, String remoteFileNam, HttpServletResponse response) {
        if (groupName == null ){
           return this.failure("组名不能为空");
        }
        if (remoteFileNam == null){
            return this.failure("远程文件不能为空");
        }
        org.csource.fastdfs.FileInfo file = FastDfsClient.getFile(groupName, remoteFileNam);
        InputStream in = FastDfsClient.downFile(groupName, remoteFileNam);

        BufferedInputStream bis = null;
        try {
            if (file != null) {
                response.setContentType("application/octet-stream");//
                response.setHeader("content-type", "application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;fileName=" + remoteFileNam);// 设置文件名
                byte[] buffer = new byte[1024];
                bis = new BufferedInputStream(in);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
            }else {
                return new Result(false, StatusCode.ERROR,"该资源不存在,请稍后再试");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    @PostMapping("/md")
    public Map<String, Object> md(@RequestParam(value = "editormd-image-file", required = false) MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        FileInfo.UploadType type = FileInfo.UploadType.images;
        FileInfo fileInfo = (FileInfo) this.uploadService.uploadFile(type, file).getData();
        String path = fileInfo.getPath();
        map.put("url", path);
        map.put("message", "上传成功");
        map.put("success", 1);
        return map;
    }

    @Autowired
    private TokenFeign tokenFeign;

    @GetMapping("/decoToken")
    public Result decoToken(String token){
        return new Result(true,StatusCode.OK,"success",this.tokenFeign.parseToken(token));
    }
}
