#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributors.

import time


class command:
    def __init__(self):
        pass

    def initialize(self, environment):
        self.env = environment

    def shutdown(self):
        pass

    def get_description(self):
        return ""

    def run(self):
        if not self.env["runtime"]["SettingsManager"].get_setting_as_bool(
            "speech", "auto_read_incoming"
        ):
            return

        if "pendingPromptText" not in self.env["commandBuffer"]:
            return

        pending_text = self.env["commandBuffer"]["pendingPromptText"]
        if not pending_text:
            return

        pending_time = self.env["commandBuffer"].get("pendingPromptTime", 0)
        delay = self.env["runtime"]["SettingsManager"].get_setting_as_float(
            "speech", "batch_flush_interval"
        )
        if time.time() - pending_time < delay:
            return

        self.env["runtime"]["OutputManager"].present_text(
            pending_text, interrupt=False, flush=False
        )
        self.env["commandBuffer"]["pendingPromptText"] = ""

    def set_callback(self, callback):
        pass
