package com.ycwl.basic.puzzle.test;

import com.ycwl.basic.puzzle.entity.PuzzleElementEntity;
import com.ycwl.basic.puzzle.entity.PuzzleTemplateEntity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 测试数据构建器
 * 用于创建测试用的模板和元素数据
 *
 * @author Claude
 * @since 2025-01-17
 */
public class PuzzleTestDataBuilder {

    /**
     * 创建基础模板
     */
    public static PuzzleTemplateEntity createBasicTemplate() {
        PuzzleTemplateEntity template = new PuzzleTemplateEntity();
        template.setId(1L);
        template.setName("测试模板");
        template.setCode("test_template");
        template.setCanvasWidth(750);
        template.setCanvasHeight(1334);
        template.setBackgroundType(0);
        template.setBackgroundColor("#FFFFFF");
        template.setDescription("用于测试的模板");
        template.setCategory("test");
        template.setStatus(1);
        template.setScenicId(null);
        template.setCreateTime(new Date());
        template.setUpdateTime(new Date());
        template.setDeleted(0);
        return template;
    }

    /**
     * 创建自定义尺寸的模板
     */
    public static PuzzleTemplateEntity createTemplate(String code, int width, int height, String bgColor) {
        PuzzleTemplateEntity template = createBasicTemplate();
        template.setCode(code);
        template.setCanvasWidth(width);
        template.setCanvasHeight(height);
        template.setBackgroundColor(bgColor);
        return template;
    }

    /**
     * 创建图片元素
     */
    public static PuzzleElementEntity createImageElement(Long templateId, String elementKey,
                                                         int x, int y, int width, int height,
                                                         int zIndex, String imageUrl) {
        PuzzleElementEntity element = new PuzzleElementEntity();
        element.setId(System.currentTimeMillis());
        element.setTemplateId(templateId);
        element.setElementType("IMAGE"); // 修改为字符串类型
        element.setElementKey(elementKey);
        element.setElementName(elementKey);
        element.setXPosition(x);
        element.setYPosition(y);
        element.setWidth(width);
        element.setHeight(height);
        element.setZIndex(zIndex);
        element.setRotation(0);
        element.setOpacity(100);
        
        // 使用JSON配置
        String config = String.format("{\"defaultImageUrl\":\"%s\",\"imageFitMode\":\"FILL\",\"borderRadius\":0}", imageUrl);
        element.setConfig(config);
        
        element.setCreateTime(new Date());
        element.setUpdateTime(new Date());
        element.setDeleted(0);
        return element;
    }

    /**
     * 创建圆角图片元素
     */
    public static PuzzleElementEntity createRoundedImageElement(Long templateId, String elementKey,
                                                                int x, int y, int width, int height,
                                                                int zIndex, String imageUrl, int borderRadius) {
        PuzzleElementEntity element = new PuzzleElementEntity();
        element.setId(System.currentTimeMillis());
        element.setTemplateId(templateId);
        element.setElementType("IMAGE");
        element.setElementKey(elementKey);
        element.setElementName(elementKey);
        element.setXPosition(x);
        element.setYPosition(y);
        element.setWidth(width);
        element.setHeight(height);
        element.setZIndex(zIndex);
        element.setRotation(0);
        element.setOpacity(100);
        
        // 使用JSON配置（包含圆角）
        String config = String.format("{\"defaultImageUrl\":\"%s\",\"imageFitMode\":\"COVER\",\"borderRadius\":%d}", 
            imageUrl, borderRadius);
        element.setConfig(config);
        
        element.setCreateTime(new Date());
        element.setUpdateTime(new Date());
        element.setDeleted(0);
        return element;
    }

    /**
     * 创建文字元素
     */
    public static PuzzleElementEntity createTextElement(Long templateId, String elementKey,
                                                        int x, int y, int width, int height,
                                                        int zIndex, String defaultText,
                                                        int fontSize, String fontColor) {
        PuzzleElementEntity element = new PuzzleElementEntity();
        element.setId(System.currentTimeMillis());
        element.setTemplateId(templateId);
        element.setElementType("TEXT"); // 修改为字符串类型
        element.setElementKey(elementKey);
        element.setElementName(elementKey);
        element.setXPosition(x);
        element.setYPosition(y);
        element.setWidth(width);
        element.setHeight(height);
        element.setZIndex(zIndex);
        element.setRotation(0);
        element.setOpacity(100);
        
        // 使用JSON配置
        String config = String.format(
            "{\"defaultText\":\"%s\",\"fontFamily\":\"微软雅黑\",\"fontSize\":%d,\"fontColor\":\"%s\"," +
            "\"fontWeight\":\"NORMAL\",\"fontStyle\":\"NORMAL\",\"textAlign\":\"LEFT\"," +
            "\"lineHeight\":1.5,\"textDecoration\":\"NONE\"}",
            defaultText, fontSize, fontColor);
        element.setConfig(config);
        
        element.setCreateTime(new Date());
        element.setUpdateTime(new Date());
        element.setDeleted(0);
        return element;
    }

    /**
     * 创建粗体文字元素
     */
    public static PuzzleElementEntity createBoldTextElement(Long templateId, String elementKey,
                                                            int x, int y, int width, int height,
                                                            int zIndex, String defaultText,
                                                            int fontSize, String fontColor, String textAlign) {
        PuzzleElementEntity element = new PuzzleElementEntity();
        element.setId(System.currentTimeMillis());
        element.setTemplateId(templateId);
        element.setElementType("TEXT");
        element.setElementKey(elementKey);
        element.setElementName(elementKey);
        element.setXPosition(x);
        element.setYPosition(y);
        element.setWidth(width);
        element.setHeight(height);
        element.setZIndex(zIndex);
        element.setRotation(0);
        element.setOpacity(100);
        
        // 使用JSON配置（粗体）
        String config = String.format(
            "{\"defaultText\":\"%s\",\"fontFamily\":\"微软雅黑\",\"fontSize\":%d,\"fontColor\":\"%s\"," +
            "\"fontWeight\":\"BOLD\",\"fontStyle\":\"NORMAL\",\"textAlign\":\"%s\"," +
            "\"lineHeight\":1.5,\"textDecoration\":\"NONE\"}",
            defaultText, fontSize, fontColor, textAlign);
        element.setConfig(config);
        
        element.setCreateTime(new Date());
        element.setUpdateTime(new Date());
        element.setDeleted(0);
        return element;
    }

    /**
     * 创建完整的测试模板（带元素）
     */
    public static List<PuzzleElementEntity> createFullTestElements(Long templateId) {
        List<PuzzleElementEntity> elements = new ArrayList<>();

        // 背景图片
        elements.add(createImageElement(templateId, "bgImage", 0, 0, 750, 400, 0,
                "https://example.com/bg.jpg"));

        // 用户头像（圆形）
        elements.add(createRoundedImageElement(templateId, "userAvatar", 50, 100, 80, 80, 1,
                "https://example.com/avatar.jpg", 40));

        // 用户名
        elements.add(createBoldTextElement(templateId, "userName", 150, 110, 200, 60, 1,
                "用户昵称", 24, "#333333", "LEFT"));

        // 订单号标题
        elements.add(createTextElement(templateId, "orderTitle", 50, 200, 650, 40, 1,
                "订单号：", 16, "#666666"));

        // 订单号内容
        elements.add(createBoldTextElement(templateId, "orderNumber", 50, 240, 650, 40, 1,
                "ORDER123456", 18, "#333333", "LEFT"));

        return elements;
    }

    /**
     * 创建1020x1520的现实场景元素
     * 上方3张图片，每张470高度（共1410），底下110像素左侧二维码右侧文字
     */
    public static List<PuzzleElementEntity> createRealScenarioElements(Long templateId) {
        List<PuzzleElementEntity> elements = new ArrayList<>();

        // 画布总高度1520，每张图片470高度
        int imageHeight = 470;
        int bottomHeight = 110; // 剩余高度：1520 - 470*3 = 110

        // 上方3张图片，每张470高度
        elements.add(createImageElement(templateId, "image1", 0, 0, 1020, imageHeight, 1,
                "https://example.com/image1.jpg"));

        elements.add(createImageElement(templateId, "image2", 0, imageHeight, 1020, imageHeight, 1,
                "https://example.com/image2.jpg"));

        elements.add(createImageElement(templateId, "image3", 0, imageHeight * 2, 1020, imageHeight, 1,
                "https://example.com/image3.jpg"));

        // 底部区域起始Y坐标
        int bottomY = imageHeight * 3; // 1410

        // 底部二维码（左侧，占满底部高度）
        PuzzleElementEntity qrElement = new PuzzleElementEntity();
        qrElement.setId(System.currentTimeMillis());
        qrElement.setTemplateId(templateId);
        qrElement.setElementType("IMAGE");
        qrElement.setElementKey("qrCode");
        qrElement.setElementName("qrCode");
        qrElement.setXPosition(20);
        qrElement.setYPosition(bottomY + 5);
        qrElement.setWidth(100);
        qrElement.setHeight(100);
        qrElement.setZIndex(2);
        qrElement.setRotation(0);
        qrElement.setOpacity(100);
        qrElement.setConfig("{\"defaultImageUrl\":\"https://example.com/qrcode.png\",\"imageFitMode\":\"CONTAIN\",\"borderRadius\":0}");
        qrElement.setCreateTime(new Date());
        qrElement.setUpdateTime(new Date());
        qrElement.setDeleted(0);
        elements.add(qrElement);

        // 底部文字（右侧，占满剩余宽度和高度）
        int textX = 140; // 二维码右侧留20像素间距
        int textWidth = 1020 - textX - 20; // 右侧留20像素边距
        
        PuzzleElementEntity textElement = new PuzzleElementEntity();
        textElement.setId(System.currentTimeMillis() + 1);
        textElement.setTemplateId(templateId);
        textElement.setElementType("TEXT");
        textElement.setElementKey("bottomText");
        textElement.setElementName("bottomText");
        textElement.setXPosition(textX);
        textElement.setYPosition(bottomY + 10);
        textElement.setWidth(textWidth);
        textElement.setHeight(bottomHeight - 10);
        textElement.setZIndex(2);
        textElement.setRotation(0);
        textElement.setOpacity(100);
        textElement.setConfig("{\"defaultText\":\"扫码查看详情\",\"fontFamily\":\"微软雅黑\",\"fontSize\":18,\"fontColor\":\"#333333\",\"fontWeight\":\"BOLD\",\"textAlign\":\"RIGHT\",\"lineHeight\":1.5,\"textDecoration\":\"NONE\"}");
        textElement.setCreateTime(new Date());
        textElement.setUpdateTime(new Date());
        textElement.setDeleted(0);
        elements.add(textElement);

        return elements;
    }
}
