import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from EnergySystem import EnergySystem
from config import pv_config, grid_config, ess_config


if __name__ == '__main__':
    data = pd.read_csv('combined_data.csv')
    time_interval = 15 / 60

    pv_loss = 0.95
    pv_cost_per_kW = 200
    pv_base = 50000
    pv_lifetime = 25

    ess_loss = 0.95
    ess_cost_per_kW = 300
    ess_base = 50000
    ess_lifetime = 25

    grid_loss = 0.95
    sell_price = 0.4 #kWh
    grid_capacity = 5000 #kWh


    pv_step=10000
    ess_step=10000

    pv_capacities = np.linspace(50000, 150000, 11)
    ess_capacities = np.linspace(50000, 150000, 11)
    results = pd.DataFrame(index=pv_capacities, columns = ess_capacities)
    for pv_capacity in pv_capacities:
        print(f"pv_capacity:{pv_capacity}")
        for ess_capacity in ess_capacities:
            print(f"ess_capacity:{ess_capacity}")
            pv = pv_config(capacity=pv_capacity, 
                           cost_per_kW=pv_cost_per_kW,
                           lifetime=pv_lifetime, 
                           loss=pv_loss)
            ess = ess_config(capacity=ess_capacity, 
                             cost_per_kW=ess_cost_per_kW, 
                             lifetime=ess_lifetime, 
                             loss=ess_loss,
                             charge_power=ess_capacity,
                             discharge_power=ess_capacity)
            grid = grid_config(capacity=grid_capacity, 
                               grid_loss=grid_loss,
                               sell_price= sell_price)
            energySystem = EnergySystem(pv_type=pv, 
                                        ess_type=ess, 
                                        grid_type= grid)
            benefit = energySystem.simulate(data, time_interval)
            results.loc[pv_capacity,ess_capacity] = benefit
    results = results.astype(float)

    plt.figure(figsize=(10, 8))  # 设置图形大小
    sns.heatmap(results, annot=True, fmt=".1f", cmap='viridis')
    plt.title('Benefit Heatmap Based on PV and ESS Capacities')
    plt.xlabel('ESS Capacity (kWh)')
    plt.ylabel('PV Capacity (kW)')
    plt.show()

    # pv = pv_config(capacity=100000,cost_per_kW=200,lifetime=25,loss=0.95)
    # ess = ess_config(capacity=100000,cost_per_kW=300,lifetime=25,loss=0.95,charge_power=100000,discharge_power=100000)
    # grid = grid_config(price_schedule=price_schedule, capacity=5000, grid_loss=0.95, sell_price=0.4)
    # grid = grid_config(capacity=50000, grid_loss=0.95, sell_price=0.4)


    # print(benefit)