package cn.newfeifan.mall.module.infra.controller.app.file;

import cn.hutool.core.io.IoUtil;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.module.infra.controller.app.file.vo.AppFileUploadReqVO;
import cn.newfeifan.mall.module.infra.service.file.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.module.infra.enums.AppFileConstants.DOMAIN_LIST;
import static cn.newfeifan.mall.module.infra.enums.ErrorCodeConstants.URL_DOMAIN_NOT_EXISTS;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

@Tag(name = "用户 App - 文件存储")
@RestController
@RequestMapping("/infra/file")
@Validated
@Slf4j
public class AppFileController {

    @Resource
    private FileService fileService;


    @Resource
    private RestTemplate restTemplate;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public CommonResult<String> uploadFile(AppFileUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        String path = uploadReqVO.getPath();
        return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
    }

    @GetMapping("/downloadByUrl")
    @Operation(summary = "通过链接下载文件流")
    public ResponseEntity<ByteArrayResource> downloadByUrl(@RequestParam("fileUrl") String fileUrl) throws Exception {

        if(!checkPrefix(fileUrl)){
            throw exception(URL_DOMAIN_NOT_EXISTS);
        }

        try {
            URL url = new URL(fileUrl);
            URLConnection connection = url.openConnection();
            byte[] data = StreamUtils.copyToByteArray(connection.getInputStream());
            ByteArrayResource resource = new ByteArrayResource(data);

            String[] path = fileUrl.split("/?\\?", 2)[0].split("/");  // Ignore query parameters
            String filename = path[path.length - 1];

            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");

            MediaType contentType = MediaType.APPLICATION_OCTET_STREAM; // Default content type
            try {
                contentType = MediaType.parseMediaType(connection.getContentType());
            } catch (IllegalArgumentException ex) {
                // Log warning and fall back to default content type
            }

            return ResponseEntity.ok()
                    .headers(headers)
                    .contentLength(connection.getContentLengthLong())
                    .contentType(contentType)
                    .body(resource);
        } catch (IOException e) {
            return null;
        }
    }

    private boolean checkPrefix(String fileUrl) {
        // 遍历DOMAIN_LIST中的每个域名
        for (String domain : DOMAIN_LIST) {
            // 检查传入的字符串是否以当前域名为前缀
            if (fileUrl.startsWith(domain)) {
                return true; // 如果找到匹配的前缀，返回true
            }
        }
        return false; // 如果没有找到匹配的前缀，返回false
    }



}
