#!/usr/bin/python3
#
# Basic program to import TSV points into TangoGPS/FoxtrotGPS as points of interest
#
# Copyright 2009 Paul Wise
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import os
import random
import sqlite3
import fileinput

cat_map = {
	'pharmacy': (5,1),
	'restaurant': (9,0),
	'supermarket': (12,1),
	'bank': (2,1),
	'monument': (12,2),
	'place_of_worship': (7,0),
	'hotel': (1,1),
	'cinema': (4,1),
	'parking': (3,1),
	'fast_food': (9,7),
	'station': (8,5),
	'archaeological_site': (11,7),
	'bus_station': (8,1),
	'cafe': (6,1),
	'motel': (1,2),
	'hospital': (5,2),
	'atm': (10,2),
	'doctors': (5,3),
	'airport': (8,8),
}

subcat_map = {
	'burger': 8,
	'regional': 1,
	'pizza': 6, 
	'italian': 10,
	'mexican': 11
}

poi = []

con = sqlite3.connect(os.path.expanduser('~/.foxtrotgps/poi.db'))
cur = con.cursor()

for line in fileinput.input():
	line = line.split("\t")
	try:
		(id, osm_id, lat, lon, type, subtype, title) = [field.strip() for field in line]
		desc = ''
	except:
		(id, osm_id, lat, lon, type, subtype, title, desc) = [field.strip() for field in line]
	if not title:
		title = desc
		desc = ''
	lon = float(lon)
	lat = float(lat)
	if lat == 0 or lon == 0:
		continue
	try:
		(cat, subcat) = cat_map[type]
	except:
		(cat, subcat) = (0.0,0.0)
	try:
		subcat = subcat_map[subtype]
	except:
		pass
	rand1 = random.randint(100000000,1000000000)
	rand2 = random.randint(100000000,1000000000)
	rand = '%s%s' % (rand1,rand2)
	poi.append((rand,lat,lon,title,desc,cat,subcat))

cur.executemany('INSERT INTO poi (idmd5,lat,lon,keywords,desc,visibility,cat,subcat,price_range,extended_open) VALUES (?,?,?,?,?,1.0,?,?,1.0,0.0)', poi)

con.commit()
con.close()
