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

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


from fenrirscreenreader.core.i18n import _


class command:
    def __init__(self):
        pass

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

    def shutdown(self):
        pass

    def get_description(self):
        return _("Cycle through key echo modes: character, word, off")

    def run(self):
        settings_manager = self.env["runtime"]["SettingsManager"]
        output_manager = self.env["runtime"]["OutputManager"]

        # Get current settings
        char_echo_mode = settings_manager.get_setting("keyboard", "char_echo_mode")
        word_echo = settings_manager.get_setting_as_bool("keyboard", "word_echo")

        # Determine current state and cycle to next
        # States: character (char=1, word=False) -> word (char=0, word=True) -> off (char=0, word=False)
        if char_echo_mode == "1" and not word_echo:
            # Currently character echo, switch to word echo
            settings_manager.set_setting("keyboard", "char_echo_mode", "0")
            settings_manager.set_setting("keyboard", "word_echo", "True")
            output_manager.present_text(
                _("Echo by word"), interrupt=True
            )
        elif word_echo:
            # Currently word echo, switch to off
            settings_manager.set_setting("keyboard", "char_echo_mode", "0")
            settings_manager.set_setting("keyboard", "word_echo", "False")
            output_manager.present_text(
                _("Echo off"), interrupt=True
            )
        else:
            # Currently off (or caps mode), switch to character echo
            settings_manager.set_setting("keyboard", "char_echo_mode", "1")
            settings_manager.set_setting("keyboard", "word_echo", "False")
            output_manager.present_text(
                _("Echo by character"), interrupt=True
            )

    def set_callback(self, callback):
        pass
