#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Form Factor Analysis - Scattering Statistics
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

# Daten einlesen
data_files = [
    ('Uncrosslinked_diblock_Eq_0.0.csv', 1e0, '#818A92', 'Uncrosslinked diblock Eq @ 0.0 $k_BT$ (x1e0)', '-'),
    ('Crosslinked_0.0_Eq_0.0.csv', 1e1, '#0072B2', 'Crosslinked @ 0.0 $k_BT$ // Eq 0.0 $k_BT$ (x1e1)', '-'),
    ('Crosslinked_0.0_Eq_0.6.csv', 1e2, '#8C7530', 'Crosslinked @ 0.0 $k_BT$ // Eq 0.6 $k_BT$ (x1e2)', '-'),
    ('Crosslinked_0.6_Eq_0.0.csv', 1e3, '#bf593e', 'Crosslinked @ 0.6 $k_BT$ // Eq 0.0 $k_BT$ (x1e3)', '--'),
    ('Crosslinked_1.2_Eq_0.0.csv', 1e4, '#FEDE9E', 'Crosslinked @ 1.2 $k_BT$ // Eq 0.0 $k_BT$ (x1e4)', ':'),
    ('Ladder_Polymer_Eq_0.0.csv', 1e5, '#D5B26B', 'Ladder Polymer Eq @ 0.0 $k_BT$ (x1e5)', '-.'),
]

fig, ax = plt.subplots(figsize=(10, 6))

for filepath, scaling, color, label, linestyle in data_files:
    try:
        data = np.loadtxt(filepath, delimiter=',', skiprows=1)  # skiprows falls Header vorhanden
        q = data[:, 0]
        Pq = data[:, 1] * scaling
        ax.loglog(q, Pq, label=label, color=color, linestyle=linestyle, linewidth=2)
    except Exception as e:
        print(f"Warnung: Konnte {filepath} nicht laden - {e}")

ax.set_xlabel("q (Å$^{-1}$)", fontsize=16)
ax.set_ylabel("P(q)", fontsize=18)
ax.tick_params(labelsize=16)
ax.grid(True, which='both', linestyle='-', linewidth=0.5, color='#e0e0e0', alpha=0.7)
ax.legend(fontsize=11, framealpha=0.9)

plt.tight_layout()
plt.savefig("./crosslinked_formfactor_stacked.png", dpi=600)
# plt.show()
