from rtctools_heat_network.pycml import Variable

from .electricity_base import ElectricityTwoPort
from .._internal import BaseAsset


class ElectricityCable(ElectricityTwoPort, BaseAsset):
    """
    The electricity cable component is used to model voltage and power drops in the electricity
    lines. We model the power losses by over estimating them with the maximum current. We ensure
    that the power is always less than what the current is able to carry by an equality constraint
    at the demand where we enforce the minimum voltage.
    """

    def __init__(self, name, **modifiers):
        super().__init__(name, **modifiers)

        self.component_type = "electricity_cable"
        self.disconnectable = False

        self.length = 1.0

        # Powerloss with inequality in the heat-mixin
        # values for NAYY 4x50 SE
        # from: https://pandapower.readthedocs.io/en/v2.6.0/std_types/basic.html
        self.max_current = 142.0
        self.max_voltage = 1.5e4
        self.min_voltage = 1.0e4
        self.nominal_current = self.max_current / 2.0
        self.nominal_voltage = (self.max_voltage + self.min_voltage) / 2.0
        self.r = 1.0e-4 * self.length  # TODO: temporary value
        self.add_variable(Variable, "Power_loss", min=0.0, nominal=self.r * self.max_current**2)

        self.add_equation(
            (
                (self.ElectricityOut.V - (self.ElectricityIn.V - self.r * self.ElectricityIn.I))
                / ((self.nominal_current * self.r * self.nominal_current) ** 0.5)
            )
        )
        self.add_equation(((self.ElectricityIn.I - self.ElectricityOut.I) / self.nominal_current))
        self.add_equation(
            (
                (self.ElectricityOut.Power - (self.ElectricityIn.Power - self.Power_loss))
                / (self.nominal_voltage * self.nominal_current * self.r * self.max_current**2)
                ** 0.5
            )
        )
