#!/usr/bin/env python

# Copyright (C) 2020  R. A. Spencer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""Command line interface to gpx_trip."""

import argparse
import logging

import gpx_trip

import yaml

logger = logging.getLogger(__name__)

logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())

parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["map", "details"])
parser.add_argument("file")
parser.add_argument("--output", default="out.png")
parser.add_argument("--stops", default=None)

args = parser.parse_args()

if args.stops is not None:
    stops = yaml.safe_load(open(args.stops).read())
else:
    stops = []

if args.action == "map":
    gpx_trip.construct_trip_map(
        open(args.file), output_file=args.output, predefined_stops=stops
    )
else:
    info = gpx_trip.extract_info(open(args.file), predefined_stops=stops)
    print("Stops:")
    for i, stop in enumerate(info["stops"]):
        print(" ", stop["short_name"])
        print("    Long name:", stop["name"])
        print("    Latitude:", stop["lat"])
        print("    Longitude:", stop["lon"])
        print("    Time spent at stop:", info["time_at_stops"][i])
    print("Trips:")
    for trip in info["trips"]:
        print(
            "    {} -> {}".format(
                info["stops"][trip["from"]]["short_name"],
                info["stops"][trip["to"]]["short_name"],
            )
        )
        print(
            "      From {} to {} ({})".format(
                trip["start"].strftime("%H:%M:%S"),
                trip["end"].strftime("%H:%M:%S"),
                trip["end"] - trip["start"],
            )
        )
