"""
Distributed energy storage model.
"""

from andes.core.param import NumParam

from ams.core.model import Model

from ams.models.distributed.pvd1 import PVD1Data


class ESD1Data(PVD1Data):
    """
    ESD1 model data.
    """

    def __init__(self):
        PVD1Data.__init__(self)

        self.SOCmin = NumParam(default=0.1, tex_name='SOC_{min}',
                               info='Minimum required value for SOC in limiter',
                               )

        self.SOCmax = NumParam(default=0.9, tex_name='SOC_{max}',
                               info='Maximum allowed value for SOC in limiter',
                               )

        self.SOCinit = NumParam(default=0.8, tex_name='SOC_{init}',
                                info='Initial state of charge',
                                )
        self.SOCend = NumParam(default=0.1, tex_name='SOC_{end}',
                               info='Target state of charge at the end of the period',
                               )

        self.En = NumParam(default=100.0, tex_name='E_n',
                           info='Rated energy capacity',
                           unit="MWh"
                           )

        self.EtaC = NumParam(default=1.0, tex_name='Eta_C',
                             info='Efficiency during charging',
                             vrange=(0, 1),
                             )

        self.EtaD = NumParam(default=1.0, tex_name='Eta_D',
                             info='Efficiency during discharging',
                             vrange=(0, 1),
                             )

        self.cesdc = NumParam(default=0.1, tex_name=r'c_{c,ESD}',
                              info='Charging cost',
                              unit=r'$/p.u.*h',
                              )
        self.cesdd = NumParam(default=0.1, tex_name=r'c_{d,ESD}',
                              info='Discharging cost',
                              unit=r'$/p.u.*h',
                              )

        self.tdc = NumParam(default=0.0,
                            info='Minimum charging duration',
                            tex_name=r't_{dc}',
                            unit='h',
                            )
        self.tdd = NumParam(default=0.0,
                            info='Minimum discharging duration',
                            tex_name=r't_{dd}',
                            unit='h',
                            )

        self.tdc0 = NumParam(default=0.0,
                             info='Initial charging time',
                             tex_name=r't_{dc0}',
                             unit='h',
                             )
        self.tdd0 = NumParam(default=0.0,
                             info='Initial discharging time',
                             tex_name=r't_{dd0}',
                             unit='h',
                             )


class ESD1(ESD1Data, Model):
    """
    Distributed energy storage model, revised from ANDES ``ESD1`` model for
    scheduling.

    Following parameters are omitted from the original dynamic model:
    ``fn``, ``busf``, ``xc``, ``pqflag``, ``igreg``, ``v0``, ``v1``,
    ``dqdv``, ``fdbd``, ``ddn``, ``ialim``, ``vt0``, ``vt1``, ``vt2``,
    ``vt3``, ``vrflag``, ``ft0``, ``ft1``, ``ft2``, ``ft3``, ``frflag``,
    ``tip``, ``tiq``, ``recflag``.

    References
    ----------
    1. ANDES Documentation, ESD1,
       https://andes.readthedocs.io/en/stable/groupdoc/DG.html#esd1
    """

    def __init__(self, system, config):
        ESD1Data.__init__(self)
        Model.__init__(self, system, config)
        self.group = 'DG'
