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.ModelContentDO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 模型数据对象
 * @description: 存储模型的基本信息和配置
 * @author xq
 * @version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ModelContentDTO {
    /**
     * 模型id
     */
    private String id;

    /**
     * 模型名称
     */
    private String title;

    /**
     * 模型类型
     */
    private String type;

    /**
     * 位置（json格式）
     */
    private Place binfo;

    /**
     * 调整大小
     */
    private Boolean resize;

    /**
     * 旋转
     */
    private Boolean rotate;

    /**
     * 锁定
     */
    private Boolean lock;

    /**
     * 动作
     */
    private Boolean active;

    /**
     * 隐藏
     */
    private Boolean hide;

    /**
     * 属性（json格式）
     */
    private Map<String,String> props;

    /**
     * vue组件名称
     */
    private String tag;

    /**
     * 动画（json格式）
     */
    @JsonProperty("common_animations")
    private Animation commonAnimations;

    /**
     * 事件
     */
    private List<String> events;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Place{
        private BigDecimal left;
        private BigDecimal top;
        private BigDecimal width;
        private BigDecimal height;
        private BigDecimal angle;

    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Animation{
        private String val;
        private String delay;
        private String speed;
        private String repeat;

    }

    /**
     * 转换为数据库对象
     * @param dto
     * @return
     */
    public static ModelContentDO dtoConvertDO(ModelContentDTO dto){
        ModelContentDO modelContentDO = BeanUtil.copyProperties(dto, ModelContentDO.class);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String binfo = objectMapper.writeValueAsString(dto.getBinfo());
            String commonAnimations = objectMapper.writeValueAsString(dto.getCommonAnimations());
            String props = objectMapper.writeValueAsString(dto.getProps());

            modelContentDO.setBinfo(binfo);
            modelContentDO.setCommonAnimations(commonAnimations);
            modelContentDO.setProps(props);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return modelContentDO;
    }

}
