package com.sz.admin.monitor.pojo.po;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mybatisflex.annotation.Table;
import com.sz.admin.monitor.pojo.dto.dataModel.CanvasCfgDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 画布配置数据对象
 * @description: 存储画布的基本配置信息
 * @author xq
 * @date 2026/1/29 11:32
 */
@Table("model_content")
@Schema(description = "画布配置数据对象")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CanvasCfgDO {
    /**
     * 面板id
     */
    @Schema(description = "面板id")
    private String id;

    /**
     * 用户id
     */
    @Schema(description = "用户id")
    private String userId;

    /**
     * 宽度
     */
    @Schema(description = "宽度")
    private Integer width;

    /**
     * 高度
     */
    @Schema(description = "高度")
    private Integer height;

    /**
     * 比例
     */
    @Schema(description = "比例")
    private Double scale;

    /**
     * 颜色
     */
    @Schema(description = "颜色")
    private String color;

    /**
     * 图片
     */
    @Schema(description = "图片")
    private String img;

    /**
     * 指南
     */
    @Schema(description = "指南")
    private Boolean guide;

    /**
     * 吸附
     */
    @Schema(description = "吸附")
    private Boolean adsorp;

    /**
     * 吸附差分
     */
    @Schema(description = "吸附差分")
    private Double adsorpDiff;

    /**
     * 原点（json格式）
     */
    @Schema(description = "原点（json格式）")
    private String transformOrigin;

    /**
     * 拖拽（json格式）
     */
    @Schema(description = "拖拽（json格式）")
    private String dragOffset;

    /**
     * 转换为DTO对象
     * @param canvasCfgDO
     * @return
     */
    @Schema(description = "模型id")
    public static CanvasCfgDTO doConvertDTO(CanvasCfgDO canvasCfgDO){
        // 创建新的CanvasCfgDTO实例，先复制其他属性
        CanvasCfgDTO canvasCfgDTO = new CanvasCfgDTO();

        // 手动复制除transformOrigin和dragOffset之外的所有基本类型属性
        canvasCfgDTO.setId(canvasCfgDO.getId());
        canvasCfgDTO.setUserId(canvasCfgDO.getUserId());
        canvasCfgDTO.setWidth(canvasCfgDO.getWidth());
        canvasCfgDTO.setHeight(canvasCfgDO.getHeight());
        canvasCfgDTO.setScale(canvasCfgDO.getScale());
        canvasCfgDTO.setColor(canvasCfgDO.getColor());
        canvasCfgDTO.setImg(canvasCfgDO.getImg());
        canvasCfgDTO.setGuide(canvasCfgDO.getGuide());
        canvasCfgDTO.setAdsorp(canvasCfgDO.getAdsorp());
        canvasCfgDTO.setAdsorpDiff(canvasCfgDO.getAdsorpDiff());

        ObjectMapper objectMapper = new ObjectMapper();
        String transformOrigin = canvasCfgDO.getTransformOrigin();
        String dragOffset = canvasCfgDO.getDragOffset();

        try {
            // 单独处理JSON转换 - 添加空值检查
            if (transformOrigin != null && !transformOrigin.trim().isEmpty() && !transformOrigin.equals("null")) {
                CanvasCfgDTO.CoordinatePoint originPoint = objectMapper.readValue(transformOrigin, CanvasCfgDTO.CoordinatePoint.class);
                canvasCfgDTO.setTransformOrigin(originPoint);
            }

            if (dragOffset != null && !dragOffset.trim().isEmpty() && !dragOffset.equals("null")) {
                CanvasCfgDTO.CoordinatePoint dragPoint = objectMapper.readValue(dragOffset, CanvasCfgDTO.CoordinatePoint.class);
                canvasCfgDTO.setDragOffset(dragPoint);
            }

            return canvasCfgDTO;
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON parsing error: " + e.getMessage() + ", JSON: " + transformOrigin, e);
        }
    }



    public static final String ID="id";
    public static final String USER_ID="user_id";
    public static final String WIDTH = "width";
    public static final String HEIGHT = "height";
    public static final String SCALE = "scale";
    public static final String COLOR = "color";
    public static final String IMG = "img";
    public static final String GUIDE = "guide";
    public static final String ADSORP = "adsorp";
    public static final String ADSORP_DIFF = "adsorp_diff";
    public static final String TRANSFORM_ORIGIN = "transform_origin";
    public static final String DRAG_OFFSET = "drag_offset";
}
