# Library Manager System

## Overview

The Library Manager system provides centralized management of JSON library files in KiThe. Instead of hardcoding file paths throughout the codebase, all library paths are managed in one place with support for version switching.

## Key Components

### 1. LibraryManager (`src/library_manager.rs`)
- Core manager that handles JSON file paths
- Stores configuration in `library_config.json`
- Provides thread-safe global access

### 2. Settings (`src/settings.rs`)
- User-friendly interface for library management
- Maps display names to internal library keys
- Supports batch updates

### 3. Settings GUI (`src/gui/settings_gui.rs`)
- GUI panel for changing library versions
- File browser integration
- Real-time status feedback

## Usage

### For Library Users (Programmatic)

```rust
use KiThe::library_manager::get_library_manager_mut;
use std::collections::HashMap;

// Get current paths
let manager = KiThe::library_manager::get_library_manager();
println!("Current substance base: {}", manager.substance_base_path());

// Update single library
let manager_mut = get_library_manager_mut();
manager_mut.set_substance_base("substance_base_v3.json")?;

// Batch update
let mut updates = HashMap::new();
updates.insert("substance_base", "substance_base_v3.json");
updates.insert("reactbase", "Reactbase_v2.json");
manager_mut.update_libraries(updates)?;
```

### For GUI Applications

```rust
use KiThe::settings::Settings;

let mut settings = Settings::new();

// Get current version
let version = settings.get_library_version("Substance Base");

// Update version
settings.set_library_version("Substance Base", "substance_base_v3.json")?;

// Reset to defaults
settings.reset_to_defaults()?;
```

### For GUI Integration

```rust
use KiThe::gui::settings_gui::SettingsGui;

let mut settings_gui = SettingsGui::new();

// In your main GUI loop
settings_gui.show(ctx, &mut show_settings);
```

## Configuration File

The system creates `library_config.json` with default values:

```json
{
  "substance_base": "substance_base_v2.json",
  "all_keys_substance": "all_keys_substance.json", 
  "reactbase": "Reactbase.json",
  "dict_reaction": "dict_reaction.json"
}
```

## Library Mappings

| Display Name | Internal Key | Default File |
|--------------|--------------|--------------|
| Substance Base | substance_base | substance_base_v2.json |
| All Keys Substance | all_keys_substance | all_keys_substance.json |
| Reactbase | reactbase | Reactbase.json |
| Dict Reaction | dict_reaction | dict_reaction.json |

## Benefits

1. **Centralized Management**: All library paths in one place
2. **Version Switching**: Easy switching between library versions
3. **GUI Integration**: User-friendly settings panel
4. **Backward Compatibility**: Existing code works without changes
5. **Thread Safety**: Safe concurrent access to configuration

## Migration Guide

### Before
```rust
let mut file = File::open("substance_base_v2.json").unwrap();
```

### After
```rust
let lib_manager = get_library_manager();
let mut file = File::open(lib_manager.substance_base_path()).unwrap();
```

## Examples

See `examples/library_manager_example.rs` for complete usage examples.