package com.fufan.mtsaas.modules.base.captcha;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.fufan.mtsaas.modules.base.captcha.bo.response.CaptchaResponse;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.List;

/**
 * @ClassName: CaptchaController
 * @description: 动态数字 控制器
 * @author: Liby
 * @date: 2025-01-20
 * @version: V1.0
 **/
@Slf4j
@RestController
@RequestMapping("/home")
@Api(tags = "公共-获取动态数字")
@RequiredArgsConstructor
public class CaptchaController {

    @RequestMapping("/getCaptcha")
    @ApiOperation(value = "获取动态数字", notes = "获取动态数字", httpMethod = "GET")
    public @ResponseBody
    Result<CaptchaResponse> getCaptcha(HttpServletRequest request) {
        Result result = new Result();
        Page page = new Page<>();
        CaptchaResponse response = new CaptchaResponse();
        int codeLength = 4;
        int width = 100;
        int height = 40;
        String filePath = "c://temp//captcha.png";

        // 创建一个图片缓冲区
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bufferedImage.createGraphics();

        // 设置背景色
        g2d.setColor(Color.PINK);
        g2d.fillRect(0, 0, width, height);

        // 生成随机数
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < codeLength; i++) {
            char c = (char) (random.nextInt(10) + '0'); // 生成0-9的数字
            code.append(c);
            g2d.setFont(new Font("Georgia", 1, 20));
            g2d.setColor(Color.WHITE);
            // 绘制字符到图片上，稍微改变位置以避免重叠
            g2d.drawString(String.valueOf(c), i * width / codeLength, height / 2);
        }

//        try {
//            ImageIO.write(bufferedImage, "png", new File(filePath));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        log.info("验证码已生成: " + code); // 输出验证码

        // 保存图片到文件BASE64
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", baos);
            response.setData(Base64.getEncoder().encodeToString(baos.toByteArray()));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 释放资源
        g2d.dispose();

        response.setCode(String.valueOf(code));
        result.setResData(page.setRecords(Arrays.asList(response)));
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return result;
    }
}
