package cn.reghao.tnb.file.app.controller;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.web.ServletUtil;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.file.app.model.dto.UploadFile;
import cn.reghao.tnb.file.app.model.po.LocalFile;
import cn.reghao.tnb.file.app.model.vo.LocalFileUrl;
import cn.reghao.tnb.file.app.service.FileService;
import cn.reghao.tnb.file.app.util.JarFileResources;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author reghao
 * @date 2025-09-26 20:40:27
 */
@Tag(name = "file 管理接口")
@RestController
public class FileController {
    private final JarFileResources jarFileResources;
    private final FileService fileService;

    public FileController(JarFileResources jarFileResources, FileService fileService) {
        this.jarFileResources = jarFileResources;
        this.fileService = fileService;
    }

    @AuthUser
    @Operation(summary = "文件上传接口", description = "N")
    @PostMapping(value = "/api/file/upload", produces = MediaType.APPLICATION_JSON_VALUE)
    public String uploadFile(UploadFile uploadFile) throws Exception {
        MultipartFile file = uploadFile.getFile();
        LocalFile localFile = fileService.putLocalFile(file);
        String url = String.format("/%s", localFile.getObjectName());
        Map<String, String> map = new HashMap<>();
        map.put("name", file.getOriginalFilename());
        map.put("url", url);
        return WebResult.success(map);
    }

    @AuthUser
    @Operation(summary = "图片列表页面", description = "N")
    @GetMapping("/api/file/image")
    public String imageList(@RequestParam("pn") int pageNumber) {
        PageList<LocalFileUrl> pageList = fileService.getImageFiles(pageNumber);
        return WebResult.success(pageList);
    }

    @Operation(summary = "文件访问接口", description = "N")
    @GetMapping("/file/**")
    public void getFile() throws IOException {
        HttpServletRequest servletRequest = ServletUtil.getRequest();
        String uri = servletRequest.getRequestURI();
        String uri1 = URLDecoder.decode(uri, StandardCharsets.UTF_8);
        String objectName =  uri1.replaceFirst("/", "");
        fileService.getFile(objectName);
    }

    @Operation(summary = "默认头像", description = "N")
    @GetMapping(value = "/avatar.jpg", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<byte[]> getAvatar() throws IOException {
        String avatarResource = "static/dist/images/avatar.jpg";
        byte[] imageBytes = jarFileResources.getFileFromResourceAsStream(avatarResource).readAllBytes();
        // 返回包含图片字节数组的 ResponseEntity
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_JPEG)
                .contentLength(imageBytes.length)
                .body(imageBytes);
    }
}
