package com.usky.rule.vo.log;

import com.usky.rule.util.JsonUtil;
import com.usky.rule.vo.Condition;
import com.usky.rule.vo.Expression;
import com.usky.rule.vo.TimeRange;
import com.usky.rule.vo.log.Content;
import com.usky.rule.vo.log.DeviceTriggerLog;
import java.util.List;
import java.util.Map;
import org.springframework.util.CollectionUtils;

public class TriggerDeviceSerialization implements RuleEngineLogSerialization<DeviceTriggerLog> {
    public TriggerDeviceSerialization() {
    }

    public DeviceTriggerLog serialize(String detail) {
        return (DeviceTriggerLog)JsonUtil.toObject(detail, DeviceTriggerLog.class);
    }

    public Content toContent(Object obj) {
        if (obj instanceof Map) {
            DeviceTriggerLog deviceTriggerLog = (DeviceTriggerLog)JsonUtil.toObject((Map)obj, DeviceTriggerLog.class);
            return getContent(deviceTriggerLog);
        } else if (obj instanceof DeviceTriggerLog) {
            return getContent((DeviceTriggerLog)obj);
        } else {
            throw new ClassCastException("TriggerDeviceSerialization不支持的该类型的转换");
        }
    }

    static Content getContent(DeviceTriggerLog deviceTriggerLog) {
        Content content = new Content();
        StringBuilder stringBuilder = (new StringBuilder(deviceTriggerLog.getName())).append(":");
        List<Condition> conditions = deviceTriggerLog.getConditions();
        String time = deviceTriggerLog.getTime();
        if (!CollectionUtils.isEmpty(conditions)) {
            for(int i = 0; i < conditions.size(); ++i) {
                Condition condition = (Condition)conditions.get(i);
                TimeRange timeRange = condition.getTimeRange();
                if (timeRange == null) {
                    String identifierName = condition.getName();
                    if (i > 0) {
                        stringBuilder.append("，");
                    }

                    stringBuilder.append(identifierName).append("值为：");
                    stringBuilder.append(condition.getValue());
                    String comparingType = condition.getCondition();
                    Expression expression = condition.getExpression();
                    String x = expression.getX();
                    String y = expression.getY();
                    String m = expression.getM();
                    stringBuilder.append("(").append("数值");
                    switch (comparingType) {
                        case "above_minute":
                            stringBuilder.append("大于").append(x).append("超过").append(m).append("分钟");
                            break;
                        case "below_minute":
                            stringBuilder.append("小于").append(x).append("超过").append(m).append("分钟");
                            break;
                        case "equal_minute":
                            stringBuilder.append("等于").append(x).append("超过").append(m).append("分钟");
                            break;
                        case "not_equal_minute":
                            stringBuilder.append("不等于").append(x).append("超过").append(m).append("分钟");
                            break;
                        case "between_minute":
                            stringBuilder.append("大于").append(x).append("且数值小于").append(y).append("超过").append(m).append("分钟");
                            break;
                        case "above":
                            stringBuilder.append("大于").append(x);
                            break;
                        case "below":
                            stringBuilder.append("小于").append(x);
                            break;
                        case "equal":
                            stringBuilder.append("等于").append(x);
                            break;
                        case "not_equal":
                            stringBuilder.append("不等于").append(x);
                            break;
                        case "between":
                            stringBuilder.append("大于").append(x).append("且数值小于").append(y).append(")");
                    }

                    stringBuilder.append(")");
                    if (i == conditions.size() - 1) {
                        stringBuilder.append("；");
                    }
                }
            }
        }

        content.setContent(stringBuilder.toString());
        content.setTime(time);
        return content;
    }

    public String getType() {
        return "trigger:device";
    }
}