import enum
from typing import List, Optional


class MatchType(enum.Enum):
    strict = "strict"
    prefix = "prefix"
    suffix = "suffix"
    unknown = "unknown"


class Replay:
    def __init__(self, count: int, response: List[int]) -> None:
        self.count = count
        self.response = response

    def copy(self) -> "Replay":
        return Replay(self.count, list(self.response))


class Service:
    def __init__(
        self,
        name: str,
        request: List[int],
        match_type: MatchType,
        delay: int,
        response: Optional[List[int]],
        replay: Optional[List[Replay]],
        replay_loop: bool,
        actions,
        debug_swallow_response_likeliness: float,
    ) -> None:
        self.name = name
        self.request = request
        self.match_type = match_type
        self.delay = delay
        self.response = response
        self.replay = replay
        self._replay_template = [item.copy() for item in replay] if replay else None
        self.replay_loop = replay_loop
        self.actions = actions
        self.debug_swallow_response_likeliness = debug_swallow_response_likeliness

    def next_replay_response(self) -> Optional[List[int]]:
        if not self.replay:
            if self.replay_loop and self._replay_template:
                self.replay = [item.copy() for item in self._replay_template]
            else:
                return None
        if not self.replay:
            return None
        current = self.replay[0]
        current.count -= 1
        response = current.response
        if current.count == 0:
            self.replay.pop(0)
        return response

    def matches_request(self, request: List[int]) -> bool:
        if self.match_type == MatchType.strict:
            return self.request == request
        if self.match_type == MatchType.prefix:
            if len(request) < len(self.request):
                return False
            return request[: len(self.request)] == self.request
        if self.match_type == MatchType.suffix:
            if len(request) < len(self.request):
                return False
            return request[-len(self.request) :] == self.request
        return False
