from logging import Logger
from typing import override

from test_automation_suite.checks.check import Check
from test_automation_suite.core.context import Context
from test_automation_suite.core.task import Step, StepResult
from test_automation_suite.utility.cmd import run_cmd_t


class CheckBluetooth(Check):
    @override
    def steps(self) -> list[Step]:
        return [*super().steps(), self.check_bluetooth]

    def check_bluetooth(self, _context: Context, logger: Logger) -> StepResult:
        # turn bluetooth power on for the radio to work
        result, error = run_cmd_t("bluetoothctl power on")
        if error:
            logger.error("Failed to turn bt radio on: %s", error)
            return StepResult(success=False)
        # for non interactive mode you need to specify a timeout, or otherwise it will
        # abort immediately again, so scan for 30s
        result, error = run_cmd_t("bluetoothctl -t 30 scan on")
        if error:
            logger.error("Failed to scan bt devices: %s", error)
            return StepResult(success=False)
        # look at result of scan, if at least one "device" is being displayed with a mac
        # address, bt should be fine.
        if "Device" in result:
            logger.info("At least one bt device was found")
            return StepResult(success=True)

        logger.error("No bt was found during scan. (Are there any?)")
        return StepResult(success=False)
