/* global exports require process */
// deploy shape (linestring) service for the HPMS link data
//
// route will be /hpms/links/zoom/row/col or whatever, .json
//
// and eventually  .kml too, I can handle that now
//
//
//

var _ = require('lodash')
var shape_service = require('shapes_postgis').shape_geojson_generation


var env = process.env;
var puser = process.env.PSQL_USER ;
var ppass; // = process.env.PSQL_PASS ;
var phost = process.env.PSQL_HOST || '127.0.0.1';
var pport = process.env.PSQL_PORT || 5432;

// options: options, or use environment variables
// * options.host:  the database host || process.env.PSQL_USER. Default 127.0.0.1
// * options.port:  the database port || process.env.PSQL_PASS. Default 5432
// * options.username: the db user    || process.env.PSQL_HOST. Required
// * options.password: the db user's password  || process.env.PSQL_PORT. Required

// app must be an express app
exports.hpms_links = function hpms_links(options){

    if(options.host !== undefined) phost = options.host
    if(options.port !== undefined) pport = options.port
    if(options.username !== undefined) puser = options.username
    if(options.password !== undefined) ppass = options.password
    var ss_opts = {}
    _.extend(ss_opts
            ,options
                          ,{'db':'spatialvds'
                          ,'table':'hpms.hpms_data'
                          ,'alias':'hd'
                          ,'host':phost
                          ,'port':pport
                          ,'username':puser
                          ,'password':ppass
                          ,'select_properties':{'link_desc'           : 'linkname'
                                               ,'from_name'         :'from'
                                               ,'to_name'       : 'to'
                                               ,'locality'     : 'city'
                                               ,'year_record'       : 'year'
                                               ,'hd.id'   : 'hpms_id'
                                               ,'hg.id'    : 'geo_id'
                                               }
                          ,'id_col':['hpms_id']
                          ,'geo_col':'hg.geom'
                          ,'join_tables':[{'table':'hpms.hpms_link_geom'
                                          ,'alias':'hlg'
                                          ,'join' :'on (hd.id = hlg.hpms_id)'}
                                         ,{'table':'hpms.hpms_geom'
                                          ,'alias':'hg'
                                          ,'join' :'on (hg.id = hlg.geo_id)'}
                                         ]
                            }
                           )
    var hpms_link_handler = shape_service(ss_opts)
    return hpms_link_handler
}
