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

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

from fenrirscreenreader.core.i18n import _
from fenrirscreenreader.utils import line_utils
from fenrirscreenreader.utils import word_utils


class command:
    def __init__(self):
        self.lastIdent = -1

    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(
            "focus", "cursor"
        ):
            return
        if self.env["runtime"]["ScreenManager"].is_screen_change():
            self.lastIdent = 0
            return

        # Skip if no cursor movement at all
        if (not self.env["runtime"]["CursorManager"].is_cursor_horizontal_move() and 
            not self.env["runtime"]["CursorManager"].is_cursor_vertical_move()):
            return
        x, y, curr_line = line_utils.get_current_line(
            self.env["screen"]["new_cursor"]["x"],
            self.env["screen"]["new_cursor"]["y"],
            self.env["screen"]["new_content_text"],
        )
        curr_ident = self.env["screen"]["new_cursor"]["x"]

        if curr_line.isspace():
            # Don't beep for lines with only spaces - no meaningful indentation
            return
            
        # Lines with actual content - calculate proper indentation
        lastIdent, lastY, last_line = line_utils.get_current_line(
            self.env["screen"]["new_cursor"]["x"],
            self.env["screen"]["new_cursor"]["y"],
            self.env["screen"]["old_content_text"],
        )
        if curr_line.strip() != last_line.strip():
            return
        if len(curr_line.lstrip()) == len(last_line.lstrip()):
            return

        curr_ident = len(curr_line) - len(curr_line.lstrip())

        if curr_ident <= 0:
            return
            
        # Initialize lastIdent if needed
        if self.lastIdent == -1:
            self.lastIdent = curr_ident
            
        # Only beep/announce if indentation level has changed
        if self.env["runtime"]["SettingsManager"].get_setting_as_bool(
            "general", "auto_present_indent"
        ) and self.lastIdent != curr_ident:
            if self.env["runtime"]["SettingsManager"].get_setting_as_int(
                "general", "auto_present_indent_mode"
            ) in [0, 1]:
                self.env["runtime"]["OutputManager"].play_frequence(
                    curr_ident * 50, 0.1, interrupt=False
                )
            if self.env["runtime"]["SettingsManager"].get_setting_as_int(
                "general", "autePresentIndentMode"
            ) in [0, 2]:
                self.env["runtime"]["OutputManager"].present_text(
                    _("indented ") + str(curr_ident) + " ",
                    interrupt=False,
                    flush=False,
                )
        
        # Always update lastIdent for next comparison
        self.lastIdent = curr_ident

    def set_callback(self, callback):
        pass
