package com.sz.admin.monitor.pojo.dto.dataModel;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sz.admin.monitor.pojo.po.CanvasCfgDO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

    /**
     * 用户id
     */
    private String userId;

    /**
     * 宽度
     */
    private Integer width;

    /**
     * 高度
     */
    private Integer height;

    /**
     * 比例
     */
    private Double scale;

    /**
     * 颜色
     */
    private String color;

    /**
     * 图片
     */
    private String img;

    /**
     * 指南
     */
    private Boolean guide;

    /**
     * 吸附
     */
    private Boolean adsorp;

    /**
     * 吸附差分
     */
    @JsonProperty("adsorp_diff")
    private Double adsorpDiff;

    /**
     * 原点（json格式）
     */
    @JsonProperty("transform_origin")
    private CoordinatePoint transformOrigin;

    /**
     * 拖拽（json格式）
     */
    @JsonProperty("drag_offset")
    private CoordinatePoint dragOffset;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CoordinatePoint {
        private Integer x;
        private Integer y;

        public void setX(Integer x) {
            this.x = x;
        }

        public void setY(Integer y) {
            this.y = y;  // 修正这里的错误赋值
        }
    }

    /**
     * 转换为数据库对象
     *
     * @param dto
     * @return
     */
    public static CanvasCfgDO dtoConvertDO(CanvasCfgDTO dto) {
        CanvasCfgDO canvasCfgDO = BeanUtil.copyProperties(dto, CanvasCfgDO.class);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String transformOrigin = objectMapper.writeValueAsString(dto.getTransformOrigin());
            String dragOffset = objectMapper.writeValueAsString(dto.getDragOffset());
            canvasCfgDO.setTransformOrigin(transformOrigin);
            canvasCfgDO.setDragOffset(dragOffset);
            return canvasCfgDO;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
