package com.ruoyi.common.event;

import java.util.Map;

/**
 * 播放方案变化事件
 *
 * @author ruoyi
 */
public class PlayPlanChangedEvent extends ScreenEvent {

    /** 播放方案数据 */
    private final Map<String, Object> planData;

    /** 是否启用 */
    private final boolean enabled;

    /** 方案ID */
    private final Long planId;

    /**
     * 构造函数
     *
     * @param source   事件源
     * @param planData 播放方案数据
     */
    public PlayPlanChangedEvent(Object source, Map<String, Object> planData) {
        super(source);
        this.planData = planData;
        this.enabled = planData != null && !"false".equals(String.valueOf(planData.get("enabled")));
        this.planId = planData != null ? parsePlanId(planData.get("planId")) : null;
    }

    public Map<String, Object> getPlanData() {
        return planData;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public Long getPlanId() {
        return planId;
    }

    private Long parsePlanId(Object planId) {
        if (planId == null) {
            return null;
        }
        if (planId instanceof Long) {
            return (Long) planId;
        }
        if (planId instanceof Integer) {
            return ((Integer) planId).longValue();
        }
        try {
            return Long.parseLong(String.valueOf(planId));
        } catch (NumberFormatException e) {
            return null;
        }
    }
}
