package com.sunwin.metro.bean;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * @author kane
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Event {
    private String eventId;
    private long eventTimestamp;
    private String type;
    private JSONObject event;
    private List<SimpleRule> simpleRules;

    public Event(JSONObject event) {
        this.eventId = UUID.randomUUID().toString();
        DateTime dateTime = DateUtil.parse(event.getString("time"), "yyyyMMddHHmmss");
        this.eventTimestamp = dateTime.getTime();
        this.type = event.getString("name").trim();
        this.event = event;
        this.simpleRules = new ArrayList<>();
    }

    public Object getValue(String field) {
        return (Object) event.get(field);
    }

    public void setValue(String key, Object value) {
        event.put(key, value);
    }

    public void setSimpleRules(SimpleRule simpleRule) {
        this.simpleRules.add(simpleRule);
    }

    @Override
    public String toString() {
        String ruleStr = "";
        if (simpleRules != null) {
            ruleStr = simpleRules.toString();
        }

        return "{" +
                "id='" + eventId + "'" +
                ", eventTime='" + eventTimestamp + "'" +
                ", type='" + type + "'" +
                ", event='" + event.toString() + "'" +
                ", rule='" + ruleStr + "'" +
                "}";
    }
}
