# GridCal
# Copyright (C) 2022 Santiago Peñate Vera
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
import numpy as np
import scipy.sparse as sp
import GridCal.Engine.Core.topology as tp


class TransformerData:

    def __init__(self, ntr, nbus, ntime=1):
        """

        :param ntr:
        :param nbus:
        """
        self.ntr = ntr
        self.ntime = ntime

        self.names = np.zeros(ntr, dtype=object)
        self.active = np.zeros((ntr, ntime), dtype=int)
        self.R = np.zeros(ntr, dtype=float)
        self.X = np.zeros(ntr, dtype=float)
        self.G = np.zeros(ntr, dtype=float)
        self.B = np.zeros(ntr)

        self.tap_f = np.ones(ntr)  # tap generated by the difference in nominal voltage at the form side
        self.tap_t = np.ones(ntr)  # tap generated by the difference in nominal voltage at the to side
        self.tap_mod = np.ones(ntr)  # normal tap module
        self.tap_ang = np.zeros(ntr)  # normal tap angle
        self.is_bus_to_regulated = np.zeros(ntr, dtype=bool)
        self.bus_to_regulated_idx = np.zeros(ntr, dtype=int)
        self.tap_position = np.zeros(ntr, dtype=int)
        self.min_tap = np.zeros(ntr, dtype=int)
        self.max_tap = np.zeros(ntr, dtype=int)
        self.tap_inc_reg_up = np.zeros(ntr)
        self.tap_inc_reg_down = np.zeros(ntr)
        self.vset = np.ones(ntr)
        self.control_mode = np.zeros(ntr, dtype=object)

        self.C_tr_bus = sp.lil_matrix((ntr, nbus), dtype=int)  # this ons is just for splitting islands

    def slice(self, tr_idx, bus_idx, time_idx=None):
        """

        :param tr_idx:
        :param bus_idx:
        :param time_idx
        :return:
        """
        if time_idx is None:
            idx = tr_idx
        else:
            idx = np.ix_(tr_idx, time_idx)

        data = TransformerData(ntr=len(tr_idx), nbus=len(bus_idx))

        data.active = self.active[idx]

        data.names = self.names[tr_idx]
        data.R = self.R[tr_idx]
        data.X = self.X[tr_idx]
        data.G = self.G[tr_idx]
        data.B = self.B[tr_idx]

        data.tap_f = self.tap_f[tr_idx]
        data.tap_t = self.tap_t[tr_idx]
        data.tap_mod = self.tap_mod[tr_idx]
        data.tap_ang = self.tap_ang[tr_idx]
        data.is_bus_to_regulated = self.is_bus_to_regulated[tr_idx]
        data.tap_position = self.tap_position[tr_idx]
        data.min_tap = self.min_tap[tr_idx]
        data.max_tap = self.max_tap[tr_idx]
        data.tap_inc_reg_up = self.tap_inc_reg_up[tr_idx]
        data.tap_inc_reg_down = self.tap_inc_reg_down[tr_idx]
        data.vset = self.vset[tr_idx]

        data.C_tr_bus = self.C_tr_bus[np.ix_(tr_idx, bus_idx)]

        return data

    def get_island(self, bus_idx, t_idx=0):
        """
        Get the elements of the island given the bus indices
        :param bus_idx: list of bus indices
        :return: list of line indices of the island
        """
        if self.ntr:
            return tp.get_elements_of_the_island(self.C_tr_bus, bus_idx, active=self.active[:, t_idx])
        else:
            return np.zeros(0, dtype=int)

    def __len__(self):
        return self.ntr
