# Path: pytrainer/lib/date.py
# def unixtime2date(unixtime):
#     tm = time.gmtime(unixtime)
#     year = tm[0]
#     month = tm[1]
#     day = tm[2]
#     return "%0.4d-%0.2d-%0.2d" %(year,month,day)
# 
# Path: pytrainer/lib/ddbb.py
# class DDBB(Singleton):
# class ForcedInteger(TypeDecorator):
#     def __init__(self, url=None):
#     def get_connection_url(self):
#     def connect(self):
#     def disconnect(self):
#     def select(self,table,cells,condition=None, mod=None):
#     def create_tables(self, add_default=True):
#     def drop_tables(self):
#     def create_backup(self):
#     def process_bind_param(self, value, dialect):


import logging
from pytrainer.lib.date import unixtime2date
from sqlalchemy import Column, Unicode, Float, Integer, Date
from pytrainer.lib.ddbb import DeclarativeBase
        from .lib.gpx import Gpx
            from .gui.warning import Warning

    def addWaypoint(self,lon=None,lat=None,name=None,comment=None,sym=None):
        logging.debug(">>")
        waypoint = Waypoint(lon=lon, lat=lat, name=name, comment=comment, sym=sym)
        logging.debug("Adding waypoint with details lat %s,lon %s,comment %s,name %s,sym %s" % (lat,lon,comment,name,sym)  )
        self.pytrainer_main.ddbb.session.add(waypoint)
        self.pytrainer_main.ddbb.session.commit()
        logging.debug("<<")
        return waypoint.id

    def getwaypointInfo(self,id_waypoint):
        logging.debug(">>")
        retorno = self.pytrainer_main.ddbb.select("waypoints",
                                "lat,lon,ele,comment,time,name,sym",
                                "id_waypoint=%s" %id_waypoint)
        logging.debug("<<")
        return retorno

    def getAllWaypoints(self):
        logging.debug(">>")
        retorno = self.pytrainer_main.ddbb.select("waypoints","id_waypoint,lat,lon,ele,comment,time,name,sym","1=1 order by name")
        logging.debug("<<")
        return retorno

    def actualize_fromgpx(self,gpxfile):
        logging.debug(">>")
        gpx = Gpx(self.data_path,gpxfile)
        tracks = gpx.getTrackRoutes()

        if len(tracks) > 1:

# Path: pytrainer/lib/date.py
# def unixtime2date(unixtime):
#     tm = time.gmtime(unixtime)
#     year = tm[0]
#     month = tm[1]
#     day = tm[2]
#     return "%0.4d-%0.2d-%0.2d" %(year,month,day)
# 
# Path: pytrainer/lib/ddbb.py
# class DDBB(Singleton):
# class ForcedInteger(TypeDecorator):
#     def __init__(self, url=None):
#     def get_connection_url(self):
#     def connect(self):
#     def disconnect(self):
#     def select(self,table,cells,condition=None, mod=None):
#     def create_tables(self, add_default=True):
#     def drop_tables(self):
#     def create_backup(self):
#     def process_bind_param(self, value, dialect):


# Path: pytrainer/waypoint.py
import logging
from pytrainer.lib.date import unixtime2date
from sqlalchemy import Column, Unicode, Float, Integer, Date
from pytrainer.lib.ddbb import DeclarativeBase
        from .lib.gpx import Gpx
            from .gui.warning import Warning


    def addWaypoint(self,lon=None,lat=None,name=None,comment=None,sym=None):
        logging.debug(">>")
        waypoint = Waypoint(lon=lon, lat=lat, name=name, comment=comment, sym=sym)
        logging.debug("Adding waypoint with details lat %s,lon %s,comment %s,name %s,sym %s" % (lat,lon,comment,name,sym)  )
        self.pytrainer_main.ddbb.session.add(waypoint)
        self.pytrainer_main.ddbb.session.commit()
        logging.debug("<<")
        return waypoint.id

    def getwaypointInfo(self,id_waypoint):
        logging.debug(">>")
        retorno = self.pytrainer_main.ddbb.select("waypoints",
                                "lat,lon,ele,comment,time,name,sym",
                                "id_waypoint=%s" %id_waypoint)
        logging.debug("<<")
        return retorno

    def getAllWaypoints(self):
        logging.debug(">>")
        retorno = self.pytrainer_main.ddbb.select("waypoints","id_waypoint,lat,lon,ele,comment,time,name,sym","1=1 order by name")
        logging.debug("<<")
        return retorno

    def actualize_fromgpx(self,gpxfile):
        logging.debug(">>")
        gpx = Gpx(self.data_path,gpxfile)
        tracks = gpx.getTrackRoutes()

        if len(tracks) > 1:

            time = unixtime2date(tracks[0][1])