// Kilo Text Editor - Memory Management Stress Test
// This tests Box method calls and memory management extensively

// KiloEditor Box - stores editor state
box KiloEditor {
    init { lines, cursor_row, cursor_col, filename, dirty }
    
    // Initialize empty editor
    init_empty() {
        me.lines = new ArrayBox()
        me.cursor_row = 0  
        me.cursor_col = 0
        me.filename = ""
        me.dirty = false
        
        // Start with one empty line
        local empty_line
        empty_line = new StringBox()
        me.lines.push(empty_line)
    }
    
    // Get current line as StringBox
    get_current_line() {
        return me.lines.get(me.cursor_row)
    }
    
    // Insert character at cursor position
    insert_char(ch) {
        local current_line
        local left_part
        local right_part  
        local new_line
        
        current_line = me.get_current_line()
        
        // Split line at cursor position
        left_part = current_line.substring(0, me.cursor_col)
        right_part = current_line.substring(me.cursor_col, current_line.length())
        
        // Create new line with inserted character
        new_line = left_part.concat(ch).concat(right_part)
        
        // Replace line in array
        me.lines.set(me.cursor_row, new_line)
        
        // Move cursor
        me.cursor_col = me.cursor_col + 1
        me.dirty = true
    }
    
    // Delete character before cursor  
    delete_char() {
        local current_line
        local left_part
        local right_part
        local new_line
        
        if me.cursor_col > 0 {
            current_line = me.get_current_line()
            
            // Split line at cursor position
            left_part = current_line.substring(0, me.cursor_col - 1)
            right_part = current_line.substring(me.cursor_col, current_line.length())
            
            // Create new line without deleted character
            new_line = left_part.concat(right_part)
            
            // Replace line in array
            me.lines.set(me.cursor_row, new_line)
            
            // Move cursor back
            me.cursor_col = me.cursor_col - 1
            me.dirty = true
        }
    }
    
    // Insert new line at cursor
    insert_newline() {
        local current_line
        local left_part
        local right_part
        
        current_line = me.get_current_line()
        
        // Split current line
        left_part = current_line.substring(0, me.cursor_col)
        right_part = current_line.substring(me.cursor_col, current_line.length())
        
        // Update current line with left part
        me.lines.set(me.cursor_row, left_part)
        
        // Insert new line with right part
        me.lines.insert(me.cursor_row + 1, right_part)
        
        // Move cursor to start of new line
        me.cursor_row = me.cursor_row + 1
        me.cursor_col = 0
        me.dirty = true
    }
    
    // Get editor stats for memory testing
    get_stats() {
        local stats
        local total_chars
        local line_count
        local i
        local current_line
        
        stats = new StringBox()
        line_count = me.lines.length()
        total_chars = 0
        
        // Count total characters across all lines
        i = 0
        loop(i < line_count) {
            current_line = me.lines.get(i)
            total_chars = total_chars + current_line.length()
            i = i + 1
        }
        
        stats = stats.concat("Lines: ").concat(line_count.toString())
        stats = stats.concat(" | Chars: ").concat(total_chars.toString())
        stats = stats.concat(" | Cursor: ").concat(me.cursor_row.toString())
        stats = stats.concat(",").concat(me.cursor_col.toString())
        
        return stats
    }
    
    // Render editor content (simplified)
    render() {
        local line_count
        local i
        local current_line
        local line_display
        
        print("=== Kilo Editor ===")
        
        line_count = me.lines.length()
        i = 0
        
        loop(i < line_count) {
            current_line = me.lines.get(i)
            
            // Create line display with line number
            line_display = (i + 1).toString().concat(": ").concat(current_line.toString())
            
            // Mark current line with cursor
            if i == me.cursor_row {
                line_display = line_display.concat(" <-- cursor at col ").concat(me.cursor_col.toString())
            }
            
            print(line_display)
            i = i + 1
        }
        
        print("---")
        print(me.get_stats())
        print("===================")
    }
}

// Test the kilo editor with intensive Box operations
local editor
local test_char
local i

print("Starting Kilo Editor Memory Test...")

// Create editor
editor = new KiloEditor()
editor.init_empty()

print("Initial state:")
editor.render()

// Test 1: Insert multiple characters (stress StringBox creation)
print("Test 1: Inserting 'Hello World'...")
editor.insert_char("H")
editor.insert_char("e")
editor.insert_char("l")
editor.insert_char("l")
editor.insert_char("o")
editor.insert_char(" ")
editor.insert_char("W")
editor.insert_char("o")
editor.insert_char("r")
editor.insert_char("l")
editor.insert_char("d")

editor.render()

// Test 2: Insert new lines (stress ArrayBox operations)
print("Test 2: Adding new lines...")
editor.insert_newline()
editor.insert_char("L")
editor.insert_char("i")
editor.insert_char("n")
editor.insert_char("e")
editor.insert_char(" ")
editor.insert_char("2")

editor.insert_newline()
editor.insert_char("L")
editor.insert_char("i")
editor.insert_char("n")
editor.insert_char("e")
editor.insert_char(" ")
editor.insert_char("3")

editor.render()

// Test 3: Delete operations (memory cleanup test)
print("Test 3: Testing deletions...")
editor.delete_char()
editor.delete_char()
editor.delete_char()

editor.render()

// Test 4: Intensive operations for memory stress
print("Test 4: Memory stress test - 50 operations...")
i = 0
loop(i < 10) {
    editor.insert_char("X")
    editor.delete_char()
    editor.insert_char("Y")
    editor.delete_char()
    editor.insert_char("Z")
    i = i + 1
}

editor.render()

print("Kilo Editor Memory Test Complete!")
local final_stats
final_stats = editor.get_stats()
print(final_stats)