from config import pv_config, ess_config, grid_config
import pandas as pd
class EnergySystem:
    def __init__(self, pv_type: pv_config, ess_type: ess_config, grid_type: grid_config):
        self.pv = pv_type
        self.ess = ess_type
        self.grid = grid_type

    # 优先使用PV供电给工厂 - 如果PV输出能满足工厂的需求，则直接供电，多余的电能用来给ESS充电。
    # PV不足时使用ESS补充 - 如果PV输出不足以满足工厂需求，首先从ESS获取所需电量。
    # 如果ESS也不足以满足需求，再从电网获取 - 当ESS中的存储电量也不足以补充时，再从电网购买剩余所需电量。
    def simulate(self, data, time_interval):
        total_benefit = 0
        for index, row in data.iterrows():
            time = row['time']
            sunlight_intensity = row['sunlight']
            factory_demand = row['demand']
            # electricity_price = self.grid.get_price_for_time(time)
            electricity_price = row['price']

            generated_pv_power = self.pv.capacity * sunlight_intensity  # 生成的功率，单位 kW
            generated_pv_energy = generated_pv_power * time_interval * self.pv.loss  # 生成的能量，单位 kWh
            # pv生成的能量如果比工厂的需求要大
            if generated_pv_energy >= factory_demand * time_interval:
                # 剩余的能量(kwh) = pv生成的能量 - 工厂需求的功率 * 时间间隔 
                surplus_energy = generated_pv_energy - factory_demand * time_interval
                # 要充到ess中的能量 = min(剩余的能量,ess的充电功率*时间间隔(ess在时间间隔内能充进的电量),ess的容量-ess储存的能量(ess中能冲进去的电量))
                charge_to_ess = min(surplus_energy, self.ess.charge_power * time_interval, self.ess.capacity - self.ess.storage)
                self.ess.storage += charge_to_ess
                surplus_after_ess = surplus_energy - charge_to_ess
                # 如果还有电量盈余,且pv功率大于ess的充电功率+工厂的需求功率则准备卖电
                if surplus_after_ess > 0 and generated_pv_power > self.ess.charge_power + factory_demand:
                    sold_to_grid = surplus_after_ess
                    sell_income = sold_to_grid * self.grid.sell_price
                    total_benefit += sell_income
                # 节省的能量 = 工厂需求的能量 * 时间段
                total_energy = factory_demand * time_interval
            # pv比工厂的需求小
            else:
                # 从ess中需要的电量 = 工厂需要的电量 - pv中的电量
                needed_from_ess = factory_demand * time_interval - generated_pv_energy
                # 如果ess中村的电量比需要的多
                if self.ess.storage >= needed_from_ess:
                    # 取出电量
                    discharging_power = min(self.ess.discharge_power * time_interval, needed_from_ess)
                    self.ess.storage -= discharging_power
                    # 生下来的能量 = pv的能量 + 放出来的能量
                    total_energy = generated_pv_energy + discharging_power
                else:
                    total_energy = generated_pv_energy + self.ess.storage
                    self.ess.storage = 0
                    needed_from_grid = factory_demand * time_interval - total_energy
                    net_grid = min(self.grid.capacity * time_interval, needed_from_grid) *  self.grid.loss
                    # total_energy += net_grid
            # print(total_energy)
            # 工厂需求量-总能量
            # unmet_demand = max(0, factory_demand * time_interval - total_energy)
            # benefit = (total_energy - unmet_demand) * electricity_price
            benefit = (total_energy) * electricity_price
            total_benefit += benefit

        return total_benefit