#!/usr/bin/env python3

import matplotlib.pyplot as plt
from oemof import db

from dingo.core import NetworkDingo
from dingo.tools import config as cfg_dingo

plt.close('all')

cfg_dingo.load_config('config_db_tables')
cfg_dingo.load_config('config_calc')
cfg_dingo.load_config('config_files')
cfg_dingo.load_config('config_misc')

# get engine for database connection
#conn = db.connection(db_section='ontohub_wdb', cfg_file='~/.dingo/config') # <-- TODO: include custom config file from given path (+input for oemof)

# instantiate dingo network object
nd = NetworkDingo(name='network')

# get database connection info from config file
conn = db.connection(section='oedb')

#mv_grid_districts=[360, 571, 593, 368, 491, 425, 416, 372, 387, 407, 403, 373, 482] # some MV grid_districts from SPF region
#mv_grid_districts=[360, 571, 593, 368, 491, 416, 372, 387, 407, 403, 373, 482] # some MV grid_districts from SPF region
#mv_grid_districts=[482]
mv_grid_districts=[360]

nd.import_mv_grid_districts(conn, mv_grid_districts)

conn.close()

nd.mv_parametrize_grid()

nd.mv_routing(debug=False, animation=False)

conn = db.connection(section='oedb')
nd.export_mv_grid(conn, mv_grid_districts)
conn.close()

for edge in nd._mv_grid_districts[0].mv_grid.graph_edges():
    if edge['branch'].type is not None:
        print(edge['branch'].type['name'])
    else:
        print('None')

# lvrg = []
# for mv_grid_district in nd.mv_grid_districts():
#     #print(mv_grid_district._lv_load_area_groups)
#     #print(type(mv_grid_district._lv_load_area_groups))
#     for lv_load_area_group in iter(mv_grid_district._lv_load_area_groups):
#         lvrg.append([str(lv_load_area_group), lv_load_area_group.peak_load_sum, lv_load_area_group.branch_length_sum])
# lvrg = sorted(lvrg, key=lambda x: x[1])
#
# for lvrg_name, lvrg_load, lvrg_length in lvrg:
#     print(lvrg_name, lvrg_load, lvrg_length)



#df = nx.to_pandas_dataframe(nd._mv_grid_districts[0].mv_grid._graph)
# import pprint
# for edge in nd._mv_grid_districts[0].mv_grid._graph.edge.keys():
#     # print(edge, type(edge))
#     pprint.pprint(edge)
#     pprint.pprint(nd._mv_grid_districts[0].mv_grid._graph.edge[edge])

#nd._mv_grid_districts[0].mv_grid.graph_draw()

@@ -1199,16 +1199,18 @@
 lose()%0A%0A
+# 
 for edge
@@ -1261,16 +1261,18 @@
 dges():%0A
+# 
     if e
@@ -1303,24 +1303,25 @@
 t None:%0A
+#
         
 print(ed
@@ -1308,24 +1308,25 @@
 e:%0A#        
+ 
 print(edge%5B'
@@ -1348,16 +1348,18 @@
 name'%5D)%0A
+# 
     else
@@ -1360,16 +1360,18 @@
   else:%0A
+# 
         

add some debug stuff to start file