import os

import networkx

from gtfspy import route_types
from gtfspy import util
from gtfspy.networks import walk_transfer_stop_to_stop_network, stop_to_stop_networks_by_type, temporal_network, \
    combined_stop_to_stop_transit_network


def write_walk_transfer_edges(gtfs, output):
    """
    Note: either @param:gtfs, or @param:net must be specified.

    Parameters
    ----------
    gtfs: gtfspy.GTFS
        Not used if @param:net is given
    output: str

    Return
    ------
    net: networkx.Graph
    """
    net = walk_transfer_stop_to_stop_network(gtfs)
    u, v, data = next(net.edges_iter(data=True))
    keys = sorted(list(data.keys()))
    with util.create_file(output, tmpdir=True, keepext=True) as tmpfile:
        with open(tmpfile, 'w') as f:
            f.write("#from to " + " ".join(keys) + "\n")
            networkx.write_edgelist(net, f, data=keys, delimiter=",")


def write_nodes(gtfs, output):
    """
    Note: either @param:gtfs, or @param:net must be specified.

    Parameters
    ----------
    gtfs: gtfspy.GTFS
        Not used if @param:net is given
    output: str
        Path to the output file
    """
    nodes = gtfs.get_table("stops")
    with util.create_file(output, tmpdir=True, keepext=True) as tmpfile:
        nodes.to_csv(tmpfile, encoding='utf-8', index=False)


def write_combined_transit_stop_to_stop_network(gtfs, extract_output_dir):
    """
    Parameters
    ----------
    gtfs : gtfspy.GTFS
    extract_output_dir : str
    """
    multi_di_graph = combined_stop_to_stop_transit_network(gtfs)
    util.makedirs(extract_output_dir)
    _write_stop_to_stop_network(multi_di_graph, os.path.join(extract_output_dir, "combined"))


def write_stop_to_stop_networks(gtfs, output_dir):
    """
    Parameters
    ----------
    gtfs: gtfspy.GTFS
    output_dir: (str, unicode)
        a path where to write
    """
    single_layer_networks = stop_to_stop_networks_by_type(gtfs)
    util.makedirs(output_dir)
    for route_type, net in single_layer_networks.iteritems():
        tag = route_types.ROUTE_TYPE_TO_LOWERCASE_TAG[route_type]
        base_name = os.path.join(output_dir, tag)
        _write_stop_to_stop_network(net, base_name)


def write_temporal_networks_by_route_type(gtfs, extract_output_dir):
    """
    Write temporal networks by route type to disk.

    Parameters
    ----------
    gtfs: gtfspy.GTFS
    extract_output_dir: str
    """
    util.makedirs(extract_output_dir)
    for route_type in route_types.TRANSIT_ROUTE_TYPES:
        pandas_data_frame = temporal_network(gtfs, start_time_ut=None, end_time_ut=None)
        tag = route_types.ROUTE_TYPE_TO_LOWERCASE_TAG[route_type]
        out_file_name = os.path.join(extract_output_dir, tag + ".tnet")
        pandas_data_frame.to_csv(out_file_name, encoding='utf-8')


def write_temporal_network(gtfs, output_filename, start_time_ut=None, end_time_ut=None):
    """
    Parameters
    ----------
    gtfs : gtfspy.GTFS
    output_filename : str
        path to the directory where to store teh extracts
    start_time_ut: int | None
        start time of the extract in unixtime (seconds after epoch)
    end_time_ut: int | None
        end time of the extract in unixtime (seconds after epoch)
    """
    util.makedirs(os.path.dirname(os.path.abspath(output_filename)))
    pandas_data_frame = temporal_network(gtfs, start_time_ut=start_time_ut, end_time_ut=end_time_ut)
    pandas_data_frame.to_csv(output_filename, encoding='utf-8')


def _write_stop_to_stop_network(net, base_name, data=True):
    """
    Write out a network

    Parameters
    ----------
    net: networkx.DiGraph
    base_name: str
        path to the filename (without extension)
    """
    if data:
        networkx.write_edgelist(net, base_name + "_with_data.edg", data=True)
    else:
        networkx.write_edgelist(net, base_name + ".edg")


def main():
    import argparse

    parser = argparse.ArgumentParser(description="Create network extracts from already imported GTFS files.")
    subparsers = parser.add_subparsers(dest='cmd')

    # parsing import
    parser_routingnets = subparsers.add_parser('extract_temporal', help="Direct import GTFS->sqlite")
    parser_routingnets.add_argument('gtfs', help='Input GTFS .sqlite (must end in .sqlite)')
    parser_routingnets.add_argument('basename', help='Basename for the output files')  # Parsing copy

    args = parser.parse_args()

    # if the first argument is import, import a GTFS directory to a .sqlite database.
    # Both directory and
    if args.cmd == 'extract_temporal':
        gtfs_fname = args.gtfs
        output_basename = args.basename

        from gtfspy.gtfs import GTFS
        gtfs = GTFS(gtfs_fname)

        nodes_filename = output_basename + ".nodes.csv"
        with util.create_file(nodes_filename, tmpdir=True, keepext=True) as tmpfile:
            write_nodes(gtfs, tmpfile)

        transfers_filename = output_basename + ".transfers.edg"
        with util.create_file(transfers_filename, tmpdir=True, keepext=True) as tmpfile:
            write_walk_transfer_edges(gtfs, tmpfile)

        temporal_network_filename = output_basename + ".temporal_network.csv"
        with util.create_file(temporal_network_filename , tmpdir=True, keepext=True) as tmpfile:
            write_temporal_network(gtfs, tmpfile)

    else:
        print("Unrecognized command: %s" % args.cmd)
        exit(1)


if __name__ == "__main__":
    main()

@@ -464,436 +464,201 @@
 str%0A
-%0A    Return%0A    ------%0A    net: networkx.Graph%0A    %22%22%22%0A    net = walk_transfer_stop_to_stop_network(gtfs)%0A    u, v, data = next(net.edges_iter(data=True))%0A    keys = sorted(list(data.keys()))%0A    with util.create_file(output, tmpdir=True, keepext=True) as tmpfile:%0A        with open(tmpfile, 'w') as f:%0A            f.write(%22#from to %22 + %22 %22.join(keys) + %22%5Cn%22)%0A            networkx.write_edgelist(net, f, data=keys, delimiter=%22,%22
+    %22%22%22%0A    transfers = gtfs.get_table(%22stop_distances%22)%0A    with util.create_file(output, tmpdir=True, keepext=True) as tmpfile:%0A        transfers.to_csv(tmpfile, encoding='utf-8', index=False
 )%0A%0A%0A
@@ -4696,19 +4696,19 @@
 ansfers.
-edg
+csv
 %22%0A      

rename .edg to .csv so that all types of the files match