# Settings Implementation Summary

## Overview
This document describes the implementation of settings management functionality for the TGA (Thermal Gravimetric Analysis) application.

## Changes Made

### 1. Settings Module (`settings.rs`)
**Added functionality:**
- `Serialize` trait implementation for all settings structures
- `save_to_file(&self, path: &Path)` - Saves settings to a specified JSON file
- `save_to_default_location(&self)` - Saves to `tgasettings.json` in current directory
- `create_or_recreate_config(&self)` - Creates or recreates the settings file with current settings

**Settings structure contains:**
- `calibration_line`: Calibration coefficients (k, b) for mass = k * voltage + b
- `n_points`: Number of points for plot rendering
- `symbolic_expression`: Symbolic expression setting
- `log_level`: Logging level (off, error, warn, info, debug, trace)

### 2. Test Options Module (`test_options.rs`)
**Complete rewrite with new functionality:**

**Structure:**
```rust
pub struct TestOptions {
    show_settings_window: bool,  // Flag to show/hide settings window
    temp_k: String,              // Temporary edit field for k coefficient
    temp_b: String,              // Temporary edit field for b coefficient
    temp_n_points: String,       // Temporary edit field for number of points
    temp_log_level: String,      // Temporary edit field for log level
}
```

**Methods:**
- `show()` - Main UI display with button "Show and redact settings"
- `show_settings_window()` - Displays the settings editor window with:
  - Current settings display (read-only)
  - Editable fields for all settings
  - ComboBox for log level selection
  - Save, Reset to Defaults, and Close buttons
- `load_current_settings()` - Loads current settings into edit fields
- `apply_settings()` - Validates and applies edited settings, saves to file

**Settings Window Features:**
- Shows current active configuration
- Allows editing all settings parameters
- Validates input (numeric values for k, b, n_points)
- Saves settings to `tgasettings.json`
- Provides feedback messages on success/error
- Reset to defaults functionality

### 3. Model Module (`model.rs`)
**Added functionality:**

**New imports:**
```rust
use tabled::{Table, Tabled};
```

**New helper structure:**
```rust
#[derive(Tabled)]
struct SettingsRow {
    parameter: String,
    value: String,
}
```

**New function:**
- `print_settings_table(settings: &Settings)` - Prints settings as a formatted table to terminal

**Modified:**
- `PlotModel::default()` - Now calls `print_settings_table()` to display settings on startup

**Terminal output example:**
```
=== TGA Application Settings ===
+----------------------+--------+
| Parameter            | Value  |
+----------------------+--------+
| Calibration k        | 40.0000|
| Calibration b        | -1.0000|
| Number of points     | 1000   |
| Symbolic expression  | mass   |
| Log level            | info   |
+----------------------+--------+
================================
```

### 4. Controller Integration (`controller_buttons_and_panels.rs`)
**Modified:**
- `TopDropDownMenues::top_menus()` - Added `test_options: &mut TestOptions` parameter
- Updated function signature to pass TestOptions to the menu

### 5. Main Application (`experimental_kinetics_gui_main.rs`)
**Modified:**
- `PlotApp` structure - Added `test_options: TestOptions` field
- `PlotApp::new()` - Initializes TestOptions
- `PlotApp::render_ui()` - Calls settings window rendering when open

## Usage

### For Users:
1. Click "Test Options" menu → "Show and redact settings"
2. View current settings in the top section
3. Edit settings in the edit section:
   - Enter new values for k and b (calibration coefficients)
   - Enter number of plot points
   - Select log level from dropdown
4. Click "Save Settings" to apply and save to file
5. Click "Reset to Defaults" to restore default values
6. Click "Close" to close the window

### For Developers:
```rust
// Load settings (automatically loads from tgasettings.json if exists)
let settings = Settings::new().unwrap_or_else(|_| Settings::default());

// Modify settings
settings.set_calibration_coeffs(50.0, -2.0);
settings.set_n_points(Some(2000));
settings.set_log_level(Some("debug"));

// Save to file
settings.create_or_recreate_config()?;
```

## File Format
Settings are saved as JSON in `tgasettings.json`:
```json
{
  "calibration_line": {
    "k": 40.0,
    "b": -1.0
  },
  "n_points": 1000,
  "symbolic_expression": "mass",
  "log_level": "info"
}
```

## Code Comments
All code is properly commented in English, explaining:
- Structure purposes and fields
- Method functionality
- UI component behavior
- Data flow and validation logic
