import pandas as pd
import plotly   # pip/conda install plotly
# pip install kaleido
# javascript welches den plot enthält
import plotly.io as pio

pio.renderers.default = "browser"
# T14_plotten.py
cities = {"Stadt": ["London", "Berlin", "Madrid", "Rom", "Paris", "Wien", "Bukarest", "Hamburg", "Budapest", "Warsaw", "Barcelona", "München", "Mailand"],
          "Population": [8615246, 3562166, 3165235, 2874038, 2273305, 1805681, 1803425, 1760433, 1754000, 1740119, 1602386, 1493900, 1350680],
          "Land": ["England", "Deutschland", "Spanien", "Italien", "Frankreich", "Österreich", "Romanien", "Deutschland", "Ungarn", "Polen", "Spanien", "Deutschland", "Italien"]}
cities_df = pd.DataFrame(cities)
areas = [1572, 892, 604,  1285, 105,415, 228, 755, 525, 517, 101, 310, 182]
cities_df = pd.DataFrame(cities)
cities_df["Flaeche"] = areas

# **kwargs
fig = cities_df.plot(x="Stadt", y=["Population", "Flaeche"], backend="plotly")
fig.show()


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
}
pd.options.plotting.backend = 'plotly'  # backend gesetzt
fig = quarterly_data.plot()
for i, (col, color) in enumerate(colors.items()):
    print(quarterly_data.columns.get_loc(col))

    fig.data[quarterly_data.columns.get_loc(col)].line.color = color

fig.show()

# klassische Ansatz
import plotly.graph_objs as go
fig = go.Figure()

for column in quarterly_data.columns:
    fig.add_trace(go.Scatter(
        x=quarterly_data.index,
        y=quarterly_data[column],
        mode='lines',
        name=column,
        line={'color': colors.get(column, "#000000")}
    ))

fig.update_layout(
    title="Quarterly energy production",
    xaxis_title="Year Quarter",
    yaxis_title="Generation [TWh]"
)

fig.show()

# '2019 Q1'  Biomass  5.2
# '2019 Q1'  Solar    9.4
print(quarterly_data)
#melted = quarterly_data.reset_index().melt(id_vars="Year Quarter", value_vars=[])
melted = quarterly_data.melt(ignore_index=False)
fig = melted.plot(x=melted.index,
            y=melted["value"],
            colors='Energy_Source',
            color_discrete_map=colors)
fig.show()

fig = go.Figure()
fig.add_trace(go.Bar(
    x=melted.index,
    y=melted["value"],
    )
)
fig.update_layout(
    title="Bars of energy",
    xaxis_title="Year Quarter",
    yaxis_title="Generation [TWh]"
)

fig.show()