from .check_tech_constraints import check_load, check_voltage
from .reinforce_measures import reinforce_branches_current, reinforce_branches_voltage, extend_substation, new_substation


def reinforce_grid(grid, mode):
    """ Evaluates grid reinforcement needs and performs measures
    Args:
        grid: GridDingo object
        mode: kind of grid ('MV' or 'LV')

    Returns:

    Notes:

    References:
    .. [1] dena VNS
    .. [2] Ackermann et al. (RP VNS)

    """

    # kind of grid to be evaluated (MV or LV)
    if mode == 'MV':
        crit_branches, crit_stations = check_load(grid, mode)
        #print(crit_branches)

        # mark critical branches
        #for branch in crit_branches:
        #    branch['branch'].critical = True

        # do reinforcement
        reinforce_branches_current(grid, crit_branches)

        # run PF again to check for voltage issues
        grid.network.run_powerflow(conn=None, method='onthefly')

        crit_nodes = check_voltage(grid, mode)
        #print(crit_nodes)

        # determine all branches on the way from HV-MV substation to crit. nodes
        crit_branches_v = grid.find_and_union_paths(grid.station(), crit_nodes)

        # do reinforcement
        reinforce_branches_voltage(grid, crit_branches_v)
        # grid.graph_draw()

        grid.network.run_powerflow(conn=None, method='onthefly')
        crit_branches, crit_stations = check_load(grid, mode)
        crit_nodes = check_voltage(grid, mode)


    elif mode == 'LV':
        raise NotImplementedError
        #check_load(grid, mode)



    #nodes = grid.
    #check_voltage(grid, mode, nodes)



@@ -633,126 +633,8 @@
 s)%0A%0A
-        # mark critical branches%0A        #for branch in crit_branches:%0A        #    branch%5B'branch'%5D.critical = True%0A%0A
     
@@ -722,16 +722,62 @@
        #
+ if branches or stations have been reinforced:
  run PF 
@@ -810,16 +810,63 @@
  issues%0A
+        if crit_branches or crit_stations:%0A    
         
@@ -982,28 +982,175 @@
     
-#print(crit_nodes)%0A%0A
+crit_nodes_count_prev_step = len(crit_nodes)%0A        #print(crit_nodes)%0A%0A        # as long as there are voltage issues, do reinforcement%0A        while crit_nodes:%0A    
     
@@ -1226,16 +1226,20 @@
 . nodes%0A
+    
         
@@ -1311,32 +1311,36 @@
 nodes)%0A%0A        
+  
+  
 # do reinforceme
@@ -1334,32 +1334,36 @@
 o reinforcement%0A
+    
         reinforc
@@ -1412,16 +1412,20 @@
         
+    
 # grid.g
@@ -1437,16 +1437,20 @@
 draw()%0A%0A
+    
         
@@ -1506,16 +1506,21 @@
 hefly')%0A
+%0A    
         
@@ -1528,104 +1528,583 @@
 rit_
-branches, crit_stations
+nodes = check_voltage(grid, mode)%0A%0A            # if there are critical nodes left but no larger cable available, stop reinforcement%0A            if len(crit_nodes)
  =
+=
  c
-heck_load(grid, mode)%0A        crit_nodes = check_voltage(grid, mode)%0A
+rit_nodes_count_prev_step:%0A                print('==%3E There are', len(grid.find_and_union_paths(grid.station(), crit_nodes)),%0A                      'branches that cannot be reinforced (no appropriate cable available).')%0A                break%0A%0A            crit_nodes_count_prev_step = len(crit_nodes)%0A%0A        if not crit_nodes:%0A            print('==%3E All voltage issues could be solved using reinforcement.')
 %0A%0A  

use iterative grid reinforcement for voltage issues