package com.YuyeTech.TPlat.game.imageInfo.controller;

import com.YuyeTech.TPlat.VO.ResultVO;
import com.YuyeTech.TPlat.game.userinfo.repository.UserInfoRepository;
import com.YuyeTech.TPlat.game.userinfo.service.UserInfoServiceImpl;
import com.YuyeTech.TPlat.utils.ImageUtils;
import com.YuyeTech.TPlat.utils.ResultVOUtil;
import com.YuyeTech.TPlat.config.AliyunConfig;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author:slambb
 * @date:2019/12/16
 */
@Controller
@RequestMapping("/oss")
@Slf4j
public class UploadController {
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    private AliyunConfig aliyunConfig;

    @Autowired
    private UserInfoServiceImpl userInfoService;

    @Autowired
    private UserInfoRepository userInfoRepository;

    private ImageUtils imageUtils;

    @GetMapping("/toUploadBlog")
    public String toUploadBlog() {
        return "oss/upload";
    }

    @PostMapping("/toUploadBlog")
    @ResponseBody
    public ResultVO toUploadBlogPost(MultipartFile file, @RequestParam(value = "fileType", required = false) String fileType) {
        log.info("=========>文件上传::", fileType);

        //存的项目的中模版图片
        String contextPath = "";
        File uploadFile = null;
        try {
            String uploadUrl = "";
            if (null != file) {
                String filename = file.getOriginalFilename();
                if (!"".equals(filename.trim())) {
                    if (fileType.equals("avatar") ) {
                        contextPath = imageUtils.getCompressPath(file, 200);
                        uploadFile = new File(contextPath);
                    } else {
                        uploadFile= new File(filename);
                        FileOutputStream os = new FileOutputStream(uploadFile);
                        os.write(file.getBytes());
                        os.close();
                        file.transferTo(uploadFile);
                    }
                    //上传到OSS
                    uploadUrl = aliyunOSSUtil.upload(uploadFile, fileType);
                    uploadUrl = aliyunOSSUtil.addDomainName(uploadUrl);
                    log.info("domain uploadUrl=" + uploadUrl);
                }
            }
            return ResultVOUtil.success(uploadUrl);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (!contextPath.equals("")) {
                imageUtils.deleteImage(contextPath);
            }
            uploadFile.delete();
        }
        return ResultVOUtil.success();
    }

    @GetMapping("/getObjectList")
    @ResponseBody
    public List<String> getObjectList() {
        String bucketName = aliyunConfig.getBucketName();
        System.out.println(bucketName);
        List<String> objectList = aliyunOSSUtil.getObjectList(bucketName);
        return objectList;
    }

    @GetMapping("/delete")
    @ResponseBody
    public String deleteBlog(@RequestParam("key") String key) {
        aliyunOSSUtil.deleteBlog(key);
        return "删除成功";
    }
}