package com.fastbee.web.controller.common;

import com.fastbee.common.config.RuoYiConfig;
import com.fastbee.common.constant.Constants;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.common.utils.file.FileUploadUtils;
import com.fastbee.common.utils.file.FileUtils;
import com.fastbee.common.utils.file.QRCodeUtils;
import com.fastbee.framework.config.ServerConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@Api(tags = "通用请求处理")
@RestController
@RequestMapping("/common")
public class CommonController
{
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ServerConfig serverConfig;

    private static final String FILE_DELIMETER = ",";

    /**
     * 通用下载请求
     *
     * @param fileName 文件名称
     * @param delete 是否删除
     */
    @ApiOperation("文件下载")
    @GetMapping("/download")
    public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
    {
        try
        {
            if (!FileUtils.checkAllowDownload(fileName))
            {
                throw new Exception(StringUtils.format(MessageUtils.message("download.filename.not.valid"), fileName));
            }
            String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
            String filePath = RuoYiConfig.getDownloadPath() + fileName;

            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, realFileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (delete)
            {
                FileUtils.deleteFile(filePath);
            }
        }
        catch (Exception e)
        {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 通用上传请求（单个）
     */
    @ApiOperation("单个文件上传")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 通用上传请求（多个）
     */
    @ApiOperation("多个文件上传")
    @PostMapping("/uploads")
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files)
            {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                newFileNames.add(FileUtils.getName(fileName));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 本地资源通用下载
     */
    @GetMapping("/download/resource")
    @ApiOperation("本地资源通用下载")
    public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        try
        {
            if (!FileUtils.checkAllowDownload(resource))
            {
                throw new Exception(StringUtils.format(MessageUtils.message("download.resource.not.valid"), resource));
            }
            // 本地资源路径
            String localPath = RuoYiConfig.getProfile();
            // 数据库资源地址
            String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
            // 下载名称
            String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            FileUtils.writeBytes(downloadPath, response.getOutputStream());
        }
        catch (Exception e)
        {
            log.error("下载文件失败", e);
        }
    }

    /**
     *  生成二维码
     * @param url url

     * */
    @GetMapping(value = "/createQrCode")
    @ApiOperation("根据url生成二维码")
    public void getCode(String url, HttpServletResponse response) throws IOException {
        // 设置响应流信息
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        OutputStream stream = response.getOutputStream();

        //获取一个二维码图片
        BitMatrix bitMatrix = QRCodeUtils.createCode(url);
        //以流的形式输出到前端
        MatrixToImageWriter.writeToStream(bitMatrix , "jpg" , stream);
    }


    @ApiOperation("音频文件上传")
    @PostMapping("/upload/audio")
    public AjaxResult uploadAudioFile(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            log.info("音频文件上传开始");

            // 1. 校验文件是否为空
            if (file.isEmpty()) {
                return AjaxResult.error("上传文件不能为空");
            }

            // 2. 校验文件名
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null) {
                return AjaxResult.error("无法获取文件名");
            }

            // 3. 创建存储目录（确保路径无重复）
            String audioDir = RuoYiConfig.getProfile() + "/audios"; // 实际存储路径：/data/upload/audios/
            File dir = new File(audioDir);
            if (!dir.exists() && !dir.mkdirs()) {
                log.error("目录创建失败：{}", audioDir);
                return AjaxResult.error("服务器目录创建失败");
            }

            // 4. 生成唯一文件名
            String extension = StringUtils.substringAfterLast(originalFilename, ".");
            String uuidFileName = UUID.randomUUID() + "." + extension;
            File dest = new File(dir, uuidFileName);

            // 5. 保存文件
            try {
                file.transferTo(dest);
            } catch (IOException e) {
                log.error("文件保存失败：{}", dest.getAbsolutePath());
                return AjaxResult.error("文件保存失败：" + e.getMessage());
            }

            // 6. 构造返回数据（兼容下载接口）
            String relativePath = Constants.RESOURCE_PREFIX + "/audios/" + uuidFileName; // 格式：/profile/audios/xxx.mp3
            String url = serverConfig.getUrl() + relativePath;

            AjaxResult ajax = AjaxResult.success("音频上传成功");
            ajax.put("url", url);                      // 完整访问URL（如 http://domain/profile/audios/xxx.mp3）
            ajax.put("resourcePath", relativePath);     // 下载接口参数（如 /profile/audios/xxx.mp3）
            ajax.put("fileName", uuidFileName);        // 新文件名（带扩展名）
            ajax.put("originalFilename", originalFilename); // 原始文件名
            ajax.put("fileSize", file.getSize());      // 文件大小（字节）
            ajax.put("fileType", file.getContentType()); // MIME类型（如 audio/mpeg）

            log.info("音频文件上传成功，存储路径：{}", dest.getAbsolutePath());
            return ajax;
        } catch (Exception e) {
            log.error("音频文件上传失败", e);
            return AjaxResult.error("音频上传失败: " + e.getMessage());
        }
    }

}
