package com.eactive.eai.custom.alarm.policy;

import com.eactive.eai.custom.alarm.condition.AlarmCondition;
import com.eactive.eai.custom.alarm.event.AlarmEvent;
import com.eactive.eai.custom.alarm.policy.CounterPolicy.Builder;

public class TimerPolicy extends AlarmPolicy {

    private TimerPolicy(Builder builder) {
        super(builder);
    }
    
    @Override
    public boolean isAlarmTriggered(AlarmEvent event) {

	if (event.getKey().equals(getAlarmKey())) {

	    AlarmCondition condition = event.getCondition();

	    long baseTimeMs = condition.getOcurredAt();

	    if (hasElapsed(baseTimeMs)) {
		return true;
	    } else {
		return false;
	    }

	}

	return false;

    }
    
    public static Builder builder() {
        return new Builder();
    }

    public static class Builder extends AlarmPolicy.Builder<Builder> {

        @Override
        protected Builder self() {
            return this;
        }

        @Override
        public TimerPolicy build() {
            return new TimerPolicy(this);
        }
    }

}
