package com.younike.admin.controller.system;

import com.younike.common.annotation.RestAuth;
import com.younike.common.bean.APIResult;
import com.younike.common.bean.VideoUploadResult;
import com.younike.common.constant.ResultPacketCode;
import com.younike.common.utils.oss.service.IStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenpengfei
 */
@RestController
@Log4j2
@RequestMapping("oss")
@Api(tags = "文件上传接口")
public class FileUploadController {

    private final IStorageService storageService;

    @Value("${oss.filehost}")
    String fileHost;

    @Autowired
    public FileUploadController(IStorageService storageService) {
        log.info("storageService--------------" + storageService);
        this.storageService = storageService;
    }

    /**
     * 获取文件服务器host(绑定的域名)
     *
     * @return
     */
    @GetMapping("/filehost")
    @RestAuth(isPublic = true)
    @ApiOperation("获取文件服务器host")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "150", description = "请求参数不正确"),
            @ApiResponse(responseCode = "404", description = "请求路径没有或页面跳转路径不对")
    })
    public APIResult<String> filehost() {
        APIResult<String> result = new APIResult<String>();
        result.setCode(ResultPacketCode.APIResultCode.Success.getCode());
        result.setResult(fileHost);
        return result;
    }

    /**
     * 单文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/fileupload")
    @RestAuth(isPublic = true)
    @ApiOperation("单文件上传")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "150", description = "请求参数不正确"),
            @ApiResponse(responseCode = "404", description = "请求路径没有或页面跳转路径不对")
    })
    public APIResult<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
        APIResult<String> result = new APIResult<String>();
        // 保存文件
        String path = storageService.store(file);

        result.setCode(path == null ? ResultPacketCode.APIResultCode.Fail.getCode() : ResultPacketCode.APIResultCode.Success.getCode());
        result.setResult(fileHost + path);
        return result;
    }

    /**
     * 多文件上传
     *
     * @param files
     * @return
     */
    @PostMapping("/filesupload")
    @RestAuth(isPublic = true)
    @ApiOperation("多文件上传")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "150", description = "请求参数不正确"),
            @ApiResponse(responseCode = "404", description = "请求路径没有或页面跳转路径不对")
    })
    public APIResult<List<String>> handleFileUpload(@RequestParam("file") List<MultipartFile> files) {
        APIResult<List<String>> result = new APIResult<>();
        List<String> paths = new ArrayList<>();
        for (MultipartFile file : files) {
            // 保存文件
            String path = storageService.store(file);
            if (path == null) {
                result.setCode(ResultPacketCode.APIResultCode.Fail.getCode());
                result.setResult(paths);
                return result;
            }
            paths.add(fileHost + path);
        }

        result.setCode(ResultPacketCode.APIResultCode.Success.getCode());
        result.setResult(paths);
        return result;

    }

    @PostMapping("/filesuploadMore")
    public APIResult<List<List<String>>> handleFileUploadMore(@RequestParam("file") List<MultipartFile> files, @RequestParam("file1") List<MultipartFile> files1) {
        APIResult<List<List<String>>> result = new APIResult<>();
        List<String> paths = new ArrayList<>();
        List<String> paths1 = new ArrayList<>();
        List<List<String>> resultPath = new ArrayList<>();
        for (MultipartFile file : files) {
            // 保存文件
            String path = storageService.store(file);
            if (path == null) {
                result.setCode(ResultPacketCode.APIResultCode.Fail.getCode());
                result.setResult(resultPath);
                return result;
            }
            paths.add(path);
        }

        for (MultipartFile file : files1) {
            // 保存文件
            String path = storageService.store(file);
            if (path == null) {
                result.setCode(ResultPacketCode.APIResultCode.Fail.getCode());
                result.setResult(resultPath);
                return result;
            }
            paths1.add(path);
        }
        resultPath.add(paths);
        resultPath.add(paths1);
        result.setCode(ResultPacketCode.APIResultCode.Success.getCode());
        result.setResult(resultPath);
        return result;

    }

    /**
     * 单个视频上传
     *
     * @param file
     * @return
     */
    @PostMapping("/videoUpload")
    @RestAuth(isPublic = true)
    @ApiOperation("单视频上传")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "150", description = "请求参数不正确"),
            @ApiResponse(responseCode = "404", description = "请求路径没有或页面跳转路径不对")
    })
    public APIResult<VideoUploadResult> videoUpload(@RequestParam("file") MultipartFile file) {
        APIResult<VideoUploadResult> result = new APIResult<VideoUploadResult>();
        // 保存文件
        VideoUploadResult video = storageService.storeVideo(file);
        result.setCode(video == null ? ResultPacketCode.APIResultCode.Fail.getCode() : ResultPacketCode.APIResultCode.Success.getCode());
        result.setResult(video);
        return result;
    }


}
