# 1. Energie nach Quartal
#   alle energieformen in einen plot
import matplotlib.pyplot as plt
import pandas as pd
from pandas.core.config_init import max_cols

energy_df = pd.read_csv("../../data/germany_energy_mix_2019_2024.csv")
# 1) Neue spalte
energy_df["Year Quarter"] = energy_df["Year"].astype(str) + " " + energy_df['Quarter']
energy_df.drop(["Year", "Quarter"], axis=1, inplace=True)
quarterly_data = energy_df.pivot_table(
    index="Year Quarter",
    columns="Energy_Source",
    values="Generation_TWh",
    aggfunc="sum",
    fill_value=0,
)

print(quarterly_data)
colors = {
    'Solar': '#FFD700',           # Gold
    'Wind_Onshore': '#87CEEB',    # Sky Blue
    'Wind_Offshore': '#4682B4',   # Steel Blue
    'Hydroelectric': '#00CED1',   # Dark Turquoise
    'Biomass': '#32CD32',         # Lime Green
    'Natural_Gas': '#FF6347',     # Tomato
    'Coal_Hard': '#2F4F4F',       # Dark Slate Gray
    'Coal_Lignite': '#696969',    # Dim Gray
    'Nuclear': '#FF4500',         # Orange Red
    'Oil': '#8B4513'              # Saddle Brown
}

def energy_plots():
    quarterly_data.plot(color=colors)
    plt.show()

    quarterly_data.plot.area(color=colors, stacked=True)
    plt.show()

    quarterly_data.plot.box()
    plt.show()

# .area(stacked=True)

# 2. In 2 Plots übereinander
#   - greenenergies
#   - nongreenenergies
green_sources = ['Solar', 'Wind_Onshore', 'Wind_Offshore', 'Hydroelectric', 'Biomass']
nongreen_sources = ['Natural_Gas', 'Coal_Hard', 'Coal_Lignite', 'Nuclear', 'Oil']

fig, (ax1, ax2) = plt.subplots(2, 1,
                               figsize=(15, 12),
                               sharex=True)  # als pdf, png speichern

# top-plot die grünen Energien
for source in green_sources:
    ax1.plot(quarterly_data.index,
             quarterly_data[source],
             color=colors.get(source, "green"),
             label=source,
             marker="o", linewidth=2, markersize=4)

for source in nongreen_sources:
    ax2.plot(quarterly_data.index,
             quarterly_data[source],
             color=colors.get(source, "brown"),
             label=source,
             marker="o", linewidth=2, markersize=4)

ax1.set_title("Erneuerbaren Energien")
ax2.set_title("Nichterneuerbaren Energien")

#           jede spalte 1 max = series => max -> einen wert
max_value = quarterly_data.max().max() # * 1.05
max_value = max_value + (10 - max_value % 10)
min_value = quarterly_data.min().min()
print(max_value)
for ax in [ax1, ax2]:
    ax.set_ylim(min_value, max_value)
    ax.grid(True, alpha=0.3)
    ax.legend()
    ax.set_ylabel("EnergieProduktion [TWh]")

# nur jedes 2te quartal
labels = ax2.get_xticklabels()
for i, label in enumerate(labels):
    if i % 2 == 1:
        label.set_visible(False)

fig.tight_layout()  # enger zusammen
fig.savefig("energy_by_quarter.pdf")  # pdf, svg, eps
plt.show()

# 3. Alle energien nach type in einem barplot
#          - pie-plot

all_power = quarterly_data.sum()
print(all_power.T)
p2 = all_power.reset_index()
print(p2)
all_power.plot.pie(colors=list(colors.values()))
plt.show()
# df.plot.pie
