#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Timestamp: "2026-01-26 (ywatanabe)"
# File: /home/ywatanabe/proj/figrecipe/examples/04_style_change.py

"""Demonstrate style switching between SCITEX and matplotlib default.

Shows how figrecipe supports different visual styles while maintaining
reproducibility. The same data is plotted with different styles.

Outputs:
    ./05_style_change_out/plot_scitex.png
    ./05_style_change_out/plot_default.png
    ./05_style_change_out/comparison.png
"""

import matplotlib

matplotlib.use("Agg")

from pathlib import Path

import numpy as np
import scitex as stx

import figrecipe as fr


def create_demo_plot(style_name: str, output_dir: Path, logger):
    """Create a demo plot with the specified style."""
    # Load the style (available: SCITEX, MATPLOTLIB)
    fr.load_style(style_name)

    # Generate sample data
    np.random.seed(42)
    x = np.linspace(0, 10, 50)
    y1 = np.sin(x) + np.random.normal(0, 0.1, len(x))
    y2 = np.cos(x) + np.random.normal(0, 0.1, len(x))

    # Create figure
    fig, ax = fr.subplots()

    # Plot data
    ax.plot(x, y1, label="Signal A", id="line_a")
    ax.scatter(x[::5], y2[::5], label="Signal B", id="scatter_b")
    ax.fill_between(x, y1 - 0.2, y1 + 0.2, alpha=0.3, id="fill_a")

    # Decorations
    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Amplitude")
    ax.set_title(f"Style: {style_name}")
    ax.legend()

    # Save
    output_path = output_dir / f"plot_{style_name.lower()}.yaml"
    fr.save(fig, output_path, verbose=True, validate=False)
    logger.info(f"Created {style_name} style plot: {output_path}")

    return output_path


@stx.session
def main(
    CONFIG=stx.session.INJECTED,
    logger=stx.session.INJECTED,
):
    """Demonstrate style switching in figrecipe."""
    output_dir = Path(CONFIG.SDIR_OUT)
    output_dir.mkdir(exist_ok=True)

    logger.info("FigRecipe Style Switching Demo")
    logger.info("=" * 50)

    # Create plots with different styles
    scitex_path = create_demo_plot("SCITEX", output_dir, logger)
    matplotlib_path = create_demo_plot("MATPLOTLIB", output_dir, logger)

    # Create side-by-side comparison using PIL (preserves original styles)
    # Note: fr.compose() would apply uniform styling to both panels
    logger.info("Creating comparison figure (PIL tiling)...")
    from PIL import Image

    img_scitex = Image.open(scitex_path.with_suffix(".png"))
    img_matplotlib = Image.open(matplotlib_path.with_suffix(".png"))

    # Resize to same height for fair comparison
    target_height = max(img_scitex.height, img_matplotlib.height)
    if img_scitex.height != target_height:
        ratio = target_height / img_scitex.height
        img_scitex = img_scitex.resize(
            (int(img_scitex.width * ratio), target_height), Image.LANCZOS
        )
    if img_matplotlib.height != target_height:
        ratio = target_height / img_matplotlib.height
        img_matplotlib = img_matplotlib.resize(
            (int(img_matplotlib.width * ratio), target_height), Image.LANCZOS
        )

    # Create side-by-side comparison
    gap = 20
    total_width = img_scitex.width + gap + img_matplotlib.width
    comparison = Image.new("RGBA", (total_width, target_height), (255, 255, 255, 255))
    comparison.paste(img_scitex, (0, 0))
    comparison.paste(img_matplotlib, (img_scitex.width + gap, 0))

    comparison_path = output_dir / "comparison.png"
    comparison.save(comparison_path)

    logger.info(f"Comparison saved: {comparison_path}")
    logger.info("Style differences (preserved via PIL tiling):")
    logger.info("  - SCITEX: Scientific publication style (40x28mm axes)")
    logger.info("  - MATPLOTLIB: Matplotlib default style (6.4x4.8 inches)")

    return 0


if __name__ == "__main__":
    main()

# EOF
