"""Notification state only; never contributes evidence to R5 decisions."""
import math


class NotificationState:
    def __init__(self):
        self.last_pts = None
        self.last_hit = None
        self.last_issued = None
        self.latched = False
        self.reason = 'NO_CURRENT_HIT'

    def observe(self, pts, hit):
        if not math.isfinite(pts) or type(hit) is not bool:
            raise ValueError('Invalid notification observation.')
        if self.last_pts is not None and pts <= self.last_pts:
            raise ValueError('Notification PTS must increase.')
        self.last_pts = pts
        if self.last_hit is not None and pts-self.last_hit >= 2.:
            self.latched = False
        if hit:
            self.last_hit = pts
        self.reason = 'CURRENT_HIT' if hit else 'NO_CURRENT_HIT'
        return hit

    def admitted(self, now):
        self.last_issued = now
        self.latched = True
        self.reason = 'EVENT_ISSUED'

    def denied(self):
        self.reason = 'WEB_COOLDOWN'

    def snapshot(self):
        return dict(latched=self.latched, last_pts=self.last_pts,
                    last_hit_pts=self.last_hit, last_issued_monotonic=self.last_issued,
                    reason=self.reason)
