from typing import List, Dict

class PV:
    def __init__(self, efficiency_ref: float, NOCT: float, I_ref: float, kappa: float, T_ref: float, surface: float,
                 irradiation_data: List[float], ambient_temperature_data: List[float]):
        self._efficiency_ref: float = efficiency_ref  # -
        self._NOCT: float = NOCT  # °C
        self._I_ref: float = I_ref/1000  # kW.m-2
        self._kappa: float = kappa  # K-1
        self._T_ref: float = T_ref  # °C

        self._surface: float = surface  # m2

        self._irradiation_data: List[float] = irradiation_data  # W.m-2
        self._ambient_temperature_data: List[float] = ambient_temperature_data  # °C

        self._money_balance = 0  # €, the money earned at each time step
        self._energy_balance = 0  # kWh, the energy produced at each time step

    def _calculate_production(self, t: int) -> float:
        irradiation = self._irradiation_data[t]
        ambient_temperature = self._ambient_temperature_data[t]

        cell_temperature = ambient_temperature + (self._NOCT - 20) * irradiation / self._I_ref
        efficiency = self._efficiency_ref * (1 - self._kappa * (cell_temperature - self._T_ref))
        production = self._surface * efficiency * irradiation  # kWh

        return production  # kWh

    def create_order(self, t: int) -> Dict:
        production = self._calculate_production(t)  # kWh
        price = 0.1  # €/kWh
        order = {"order_type": "sell", "quantity": production, "price": price}

        return order

    def update(self, market_result: Dict):
        quantity_sold = market_result["quantity"]  # kWh
        price = market_result["price"]  # €/kWh

        self._energy_balance += quantity_sold
        self._money_balance += quantity_sold * price  # updates the money earned

    # @property
    def get_money_balance(self):
        return self._money_balance

    # @property
    def get_energy_balance(self):
        return self._energy_balance

