package com.ycwl.basic.image.watermark.edge;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 水印模板构建器基类
 * 提供构建元素的工具方法
 */
public abstract class AbstractWatermarkTemplateBuilder implements IWatermarkTemplateBuilder {

    // 虚拟模板ID（运行时使用，不存储）
    private static final AtomicLong VIRTUAL_TEMPLATE_ID = new AtomicLong(-1);
    private static final AtomicLong VIRTUAL_ELEMENT_ID = new AtomicLong(-1);

    /**
     * 元素类型常量
     */
    protected static final String ELEMENT_TYPE_IMAGE = "IMAGE";
    protected static final String ELEMENT_TYPE_TEXT = "TEXT";

    /**
     * 图片适配模式
     */
    protected static final String FIT_MODE_COVER = "COVER";
    protected static final String FIT_MODE_CONTAIN = "CONTAIN";

    /**
     * 文本对齐方式
     */
    protected static final String TEXT_ALIGN_LEFT = "LEFT";
    protected static final String TEXT_ALIGN_RIGHT = "RIGHT";
    protected static final String TEXT_ALIGN_CENTER = "CENTER";

    /**
     * 创建虚拟模板
     */
    protected PuzzleTemplateEntity createTemplate(String code, int width, int height, String backgroundImage) {
        PuzzleTemplateEntity template = new PuzzleTemplateEntity();
        template.setId(VIRTUAL_TEMPLATE_ID.decrementAndGet());
        template.setCode(code);
        template.setName("水印模板-" + getStyle());
        template.setCanvasWidth(width);
        template.setCanvasHeight(height);
        template.setBackgroundType(1); // 图片背景
        template.setBackgroundImage(backgroundImage);
        template.setStatus(1);
        return template;
    }

    /**
     * 创建纯色背景模板
     */
    protected PuzzleTemplateEntity createTemplateWithColor(String code, int width, int height, String backgroundColor) {
        PuzzleTemplateEntity template = new PuzzleTemplateEntity();
        template.setId(VIRTUAL_TEMPLATE_ID.decrementAndGet());
        template.setCode(code);
        template.setName("水印模板-" + getStyle());
        template.setCanvasWidth(width);
        template.setCanvasHeight(height);
        template.setBackgroundType(0); // 纯色背景
        template.setBackgroundColor(backgroundColor);
        template.setStatus(1);
        return template;
    }

    /**
     * 创建图片元素
     */
    protected PuzzleElementEntity createImageElement(String key, String name, int x, int y, int width, int height, int zIndex,
                                                     String fitMode, Integer borderRadius, Integer opacity) {
        PuzzleElementEntity element = new PuzzleElementEntity();
        element.setId(VIRTUAL_ELEMENT_ID.decrementAndGet());
        element.setElementType(ELEMENT_TYPE_IMAGE);
        element.setElementKey(key);
        element.setElementName(name);
        element.setXPosition(x);
        element.setYPosition(y);
        element.setWidth(width);
        element.setHeight(height);
        element.setZIndex(zIndex);
        element.setOpacity(opacity != null ? opacity : 100);

        // 构建配置JSON
        Map<String, Object> config = new HashMap<>();
        config.put("imageFitMode", fitMode != null ? fitMode : FIT_MODE_COVER);
        if (borderRadius != null && borderRadius > 0) {
            config.put("borderRadius", borderRadius);
        }
        element.setConfig(JacksonUtil.toJson(config));

        return element;
    }

    /**
     * 创建圆形图片元素
     */
    protected PuzzleElementEntity createCircleImageElement(String key, String name, int x, int y, int diameter, int zIndex) {
        // 圆形 = borderRadius 为直径的一半
        return createImageElement(key, name, x, y, diameter, diameter, zIndex, FIT_MODE_COVER, diameter / 2, null);
    }

    /**
     * 创建文字元素
     */
    protected PuzzleElementEntity createTextElement(String key, String name, int x, int y, int width, int height, int zIndex,
                                                    String fontFamily, int fontSize, String fontColor,
                                                    String fontWeight, String textAlign) {
        PuzzleElementEntity element = new PuzzleElementEntity();
        element.setId(VIRTUAL_ELEMENT_ID.decrementAndGet());
        element.setElementType(ELEMENT_TYPE_TEXT);
        element.setElementKey(key);
        element.setElementName(name);
        element.setXPosition(x);
        element.setYPosition(y);
        element.setWidth(width);
        element.setHeight(height);
        element.setZIndex(zIndex);
        element.setOpacity(100);

        // 构建配置JSON
        Map<String, Object> config = new HashMap<>();
        config.put("fontFamily", fontFamily != null ? fontFamily : "PingFang SC");
        config.put("fontSize", fontSize);
        config.put("fontColor", fontColor != null ? fontColor : "#FFFFFF");
        config.put("fontWeight", fontWeight != null ? fontWeight : "NORMAL");
        config.put("textAlign", textAlign != null ? textAlign : TEXT_ALIGN_LEFT);
        element.setConfig(JacksonUtil.toJson(config));

        return element;
    }

    /**
     * 创建构建结果
     */
    protected WatermarkTemplateResult createResult(PuzzleTemplateEntity template,
                                                    List<PuzzleElementEntity> elements,
                                                    Map<String, String> dynamicData) {
        WatermarkTemplateResult result = new WatermarkTemplateResult();
        result.setTemplate(template);
        result.setElements(elements);
        result.setDynamicData(dynamicData);
        return result;
    }

    /**
     * 创建空的元素列表和动态数据
     */
    protected List<PuzzleElementEntity> newElementList() {
        return new ArrayList<>();
    }

    protected Map<String, String> newDynamicData() {
        return new HashMap<>();
    }
}
