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.ModelContentDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 模型数据对象
 * @description: 存储模型的基本信息和配置
 * @author xq
 * @version 1.0
 */
@Table("model_content")
@Schema(description = "存储模型的基本信息和配置")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ModelContentDO {
    /**
     * 模型id
     */
    @Schema(description = "模型id")
    private String id;

    /**
     * 模型名称
     */
    @Schema(description = "模型名称")
    private String title;

    /**
     * 模型类型
     */
    @Schema(description = "模型类型")
    private String type;

    /**
     * 位置（json格式）
     */
    @Schema(description = "位置（json格式）")
    private String binfo;

    /**
     * 调整大小
     */
    @Schema(description = "调整大小")
    private Boolean resize;

    /**
     * 旋转
     */
    @Schema(description = "旋转")
    private Boolean rotate;

    /**
     * 锁定
     */
    @Schema(description = "锁定")
    private Boolean lock;

    /**
     * 动作
     */
    @Schema(description = "动作")
    private Boolean active;

    /**
     * 隐藏
     */
    @Schema(description = "隐藏")
    private Boolean hide;

    /**
     * 属性（json格式）
     */
    @Schema(description = "属性（json格式）")
    private String props;

    /**
     * vue组件名称
     */
    @Schema(description = "vue组件名称")
    private String tag;

    /**
     * 动画（json格式）
     */
    @Schema(description = "动画（json格式）")
    private String commonAnimations;

    /**
     * 事件
     */
    @Schema(description = "事件")
    private String events;

    public static final String ID = "id";
    public static final String TITLE = "title";
    public static final String TYPE = "type";
    public static final String BINFO = "binfo";
    public static final String RESIZE = "resize";
    public static final String ROTATE = "rotate";
    public static final String LOCK = "lock";
    public static final String ACTIVE = "active";
    public static final String HIDE = "hide";
    public static final String PROPS = "props";
    public static final String TAG = "tag";
    public static final String COMMON_ANIMATIONS = "common_animations";
    public static final String EVENTS = "events";

    public static ModelContentDTO doConvertDTO(ModelContentDO modelContentDO) {
        if (modelContentDO == null) {
            return null;
        }

        ModelContentDTO modelContentDTO = new ModelContentDTO();

        // 基础字段映射
        modelContentDTO.setId(modelContentDO.getId());
        modelContentDTO.setTitle(modelContentDO.getTitle());
        modelContentDTO.setType(modelContentDO.getType());
        modelContentDTO.setResize(modelContentDO.getResize());
        modelContentDTO.setRotate(modelContentDO.getRotate());
        modelContentDTO.setLock(modelContentDO.getLock());
        modelContentDTO.setActive(modelContentDO.getActive());
        modelContentDTO.setHide(modelContentDO.getHide());
        modelContentDTO.setTag(modelContentDO.getTag());
        modelContentDTO.setEvents(new ArrayList<>());

        ObjectMapper objectMapper = new ObjectMapper();

        try {
            // JSON 字段转换 - 位置信息
            if (modelContentDO.getBinfo() != null && !modelContentDO.getBinfo().trim().isEmpty() && !"null".equals(modelContentDO.getBinfo())) {
                ModelContentDTO.Place binfoObj = objectMapper.readValue(modelContentDO.getBinfo(), ModelContentDTO.Place.class);
                modelContentDTO.setBinfo(binfoObj);
            }

            // JSON 字段转换 - 属性
            if (modelContentDO.getProps() != null && !modelContentDO.getProps().trim().isEmpty() && !"null".equals(modelContentDO.getProps())) {
                HashMap propsObj = objectMapper.readValue(modelContentDO.getProps(), HashMap.class);
                modelContentDTO.setProps(propsObj);
            }

            // JSON 字段转换 - 动画
            if (modelContentDO.getCommonAnimations() != null && !modelContentDO.getCommonAnimations().trim().isEmpty() && !"null".equals(modelContentDO.getCommonAnimations())) {
                ModelContentDTO.Animation animationsObj = objectMapper.readValue(modelContentDO.getCommonAnimations(), ModelContentDTO.Animation.class);
                modelContentDTO.setCommonAnimations(animationsObj);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON parsing error in ModelContentDO conversion: " + e.getMessage(), e);
        }

        return modelContentDTO;
    }


}
