// 🧪 Chip-8 fini Propagation Simple Test - Phase 10 Feature Verification

// Simple CPU component for fini testing
box SimpleCPU {
    init { memory_ref, graphics_ref, sound_ref }
    
    SimpleCPU() {
        me.memory_ref = null
        me.graphics_ref = null  
        me.sound_ref = null
        print("🔧 SimpleCPU created")
    }
    
    // ⭐ Phase 10: fini propagation test
    fini() {
        print("🔄 CPU cleanup triggered - fini propagation starting")
        
        // Clean up dependent components in order
        if (me.memory_ref != null) {
            me.memory_ref.cleanup()
            print("📦 Memory cleanup completed")
        }
        
        if (me.graphics_ref != null) {
            me.graphics_ref.cleanup()
            print("🖼️ Graphics cleanup completed")
        }
        
        if (me.sound_ref != null) {
            me.sound_ref.cleanup()
            print("🔊 Sound cleanup completed")
        }
        
        print("✅ CPU fini propagation complete")
    }
    
    link_components(memory, graphics, sound) {
        me.memory_ref = memory
        me.graphics_ref = graphics
        me.sound_ref = sound
        print("🔗 Components linked to CPU")
    }
}

// Simple Memory component
box SimpleMemory {
    init { data, alive }
    
    SimpleMemory() {
        me.data = new ArrayBox()
        me.alive = true
        me.data.push("memory_data_1")
        me.data.push("memory_data_2")
        print("💾 SimpleMemory created with 2 data items")
    }
    
    read_data() {
        if (me.alive) {
            return me.data.get(0)
        } else {
            print("⚠️ Memory component destroyed - access blocked")
            return null
        }
    }
    
    cleanup() {
        print("🧹 Memory cleanup: clearing data")
        me.data.clear()
        me.alive = false
    }
}

// Simple Graphics component
box SimpleGraphics {
    init { pixels, alive }
    
    SimpleGraphics() {
        me.pixels = new ArrayBox()
        me.alive = true
        me.pixels.push(0)
        me.pixels.push(1)
        me.pixels.push(0)
        print("🖼️ SimpleGraphics created with 3 pixels")
    }
    
    draw() {
        if (me.alive) {
            print("🎨 Drawing graphics...")
            return true
        } else {
            print("⚠️ Graphics component destroyed - draw blocked")
            return false
        }
    }
    
    cleanup() {
        print("🧹 Graphics cleanup: clearing display")
        me.pixels.clear()
        me.alive = false
    }
}

// Simple Sound component  
box SimpleSound {
    init { volume, alive }
    
    SimpleSound() {
        me.volume = 50
        me.alive = true
        print("🔊 SimpleSound created with volume " + me.volume)
    }
    
    play() {
        if (me.alive) {
            print("🔔 Playing sound at volume " + me.volume)
            return true
        } else {
            print("⚠️ Sound component destroyed - playback blocked")
            return false
        }
    }
    
    cleanup() {
        print("🧹 Sound cleanup: stopping audio")
        me.volume = 0
        me.alive = false
    }
}

static box Main {
    init { console }
    
    main() {
        me.console = new ConsoleBox()
        me.console.log("🧪 Phase 10: Chip-8 fini Propagation Test")
        
        // Create CPU and components
        local cpu = new SimpleCPU()
        local memory = new SimpleMemory()
        local graphics = new SimpleGraphics()
        local sound = new SimpleSound()
        
        // Link components for fini propagation
        cpu.link_components(memory, graphics, sound)
        
        // Test normal operation
        me.console.log("=== Normal Operation Test ===")
        local mem_data = memory.read_data()
        me.console.log("Memory data: " + mem_data)
        
        local gfx_result = graphics.draw()
        me.console.log("Graphics draw result: " + gfx_result)
        
        local sound_result = sound.play()
        me.console.log("Sound play result: " + sound_result)
        
        // Test fini propagation
        me.console.log("=== fini Propagation Test ===")
        cpu.fini()
        
        // Test operation after fini
        me.console.log("=== Post-fini Operation Test ===")
        local mem_data2 = memory.read_data()
        me.console.log("Memory data after fini: " + mem_data2)
        
        local gfx_result2 = graphics.draw()
        me.console.log("Graphics draw after fini: " + gfx_result2)
        
        local sound_result2 = sound.play()
        me.console.log("Sound play after fini: " + sound_result2)
        
        me.console.log("🎉 Phase 10 fini propagation test complete!")
        return "fini propagation test finished"
    }
}