import logging
from time import sleep

from GramAddict.core.behaviors import BehaviorContext, BehaviorPlugin, BehaviorResult
from GramAddict.core.physics.humanized_input import humanized_scroll

logger = logging.getLogger(__name__)


class CloseFriendsGuardPlugin(BehaviorPlugin):
    """
    Checks for close friends badge and skips.

    Priority: 99.
    """

    def __init__(self):
        super().__init__()
        self._enabled = True

    @property
    def name(self) -> str:
        return "close_friends_guard"

    @property
    def priority(self) -> int:
        return 99

    @property
    def exclusive(self) -> bool:
        return True

    def can_activate(self, ctx: BehaviorContext) -> bool:
        if not getattr(self, "_enabled", True):
            return False

        xml = ctx.context_xml if ctx.context_xml else ctx.device.dump_hierarchy()
        return "close friend" in xml.lower()

    def execute(self, ctx: BehaviorContext) -> BehaviorResult:
        logger.info("💚 [CloseFriendsGuard] Close friends post detected. Skipping...")
        humanized_scroll(ctx.device, is_skip=True)
        sleep(1.0 * ctx.sleep_mod)
        return BehaviorResult(executed=True, should_skip=True)
