/**
 * 
 * 
 */

////////////////////// Configuration ////////////////////////////////////

// Libraries
var yaml = require('yamljs')
var pg = require('pg')
var fs = require('fs')
var winston = require('winston')
var axios = require('axios')
var Promise = require('bluebird')
Promise.longStackTraces()

// Modules
var usgs_constants = require("./lib/usgs_api/usgs_constants.js")
var usgs_functions = require("./lib/usgs_api/usgs_functions.js")
var usgs_helper = require("./lib/usgs_api/usgs_helpers.js")
var pg_handler = require('./lib/postgres/postgres_handlers.js')
var app_helpers = require('./lib/helpers/app_helpers.js')()

// Constants for handling USGS API
const DL_OPTION_DOWNLOAD_CODE = "STANDARD"
const DL_USGS_RESPONSE_CODE = usgs_helper.get_usgs_response_code('download')
const MAX_SINGLE_REQUEST_ATTEMPTS = 5

// Base URL for http promise library
axios.defaults.baseURL = usgs_constants.USGS_URL

// USGS API key promise
var get_api_key = usgs_helper.get_api_key()

// Database connection
const db_config = yaml.load("./lib/postgres/config.yaml")
var pg_pool = pg_handler.pg_pool(db_config)

// Logging
const TODAY_DATE = app_helpers.get_date_string()
const LOG_LEVEL_INFO = 'info'
const LOG_LEVEL_ERROR = 'error'
const LOG_FILE = 'update_scenes_to_order_' + TODAY_DATE

app_helpers.delete_old_files(LOG_FILE, 'logs/', '.log')
app_helpers.set_logger_level('debug')
app_helpers.set_logfile(LOG_FILE)
app_helpers.write_message(LOG_LEVEL_INFO, 'START '+LOG_FILE, '')




//////////////////////////////////////////////////////////////////////////////////

////////////////////// Ground Control ////////////////////////////////////////////

// 10 download requests at a time
// (but really, do 5)

// Initial SELECT query
const query_text = "SELECT * FROM vw_last_days_scenes"


pg_handler.pool_query_db(pg_pool, query_text, [], function(query_result) {
  console.log(query_result.rows)
})







@@ -661,51 +661,8 @@
 API%0A
-const DL_OPTION_DOWNLOAD_CODE = %22STANDARD%22%0A
 cons
@@ -888,23 +888,29 @@
 ise%0A
+//
 var 
-get_
 api_key
+_promise
  = u
@@ -924,19 +924,23 @@
 per.
-get_
 api_key
+_promise
 ()%0A%0A
@@ -1076,16 +1076,19 @@
 Logging%0A
+/*%0A
 const TO
@@ -1212,31 +1212,29 @@
  = '
-update_scenes_to_order_
+download_landsat_data
 ' + 
@@ -1438,16 +1438,18 @@
 LE, '')%0A
+*/
 %0A%0A%0A%0A////
@@ -1668,16 +1668,240 @@
 , do 5)%0A
+// %0A// Error Codes%0A// DOWNLOAD_ERROR  An error occurred while looking up downloads%0A// DOWNLOAD_RATE_LIMIT The number of unattampted downloads has been exceeded%0A%0A// 1985 scene, needs_ordering = null%0A// LT50180351985215AA006%0A%0A
 %0A// Init
@@ -1929,20 +1929,32 @@
 t query_
-text
+last_days_scenes
  = %22SELE
@@ -1985,16 +1985,420 @@
 cenes%22%0A%0A
+var api_key_promise = usgs_helper.get_api_key()%0A%0A%0A%0Aconst main = function() %7B%0A  return api_key_promise.then(function(apiKey) %7B%0A    return usgs_functions.usgsapi_getorderproducts(%0A      apiKey, usgs_constants.NODE_EE, 'LANDSAT_TM', %5B'LT50180351985215AA006'%5D%0A    ).then(function (response) %7B%0A      console.log('response:', response)%0A    %7D)%0A  %7D).catch(function (err) %7B%0A    console.log(err)%0A  %7D)%0A%7D%0A%0Amain()%0A%0A/*
 %0Apg_hand
@@ -2434,12 +2434,24 @@
 ery_
-text
+last_days_scenes
 , %5B%5D
@@ -2479,27 +2479,30 @@
 lt) %7B%0A  
-console.log
+process_result
 (query_r
@@ -2510,18 +2510,16 @@
 sult
-.rows
 )%0A%7D)%0A
+*/%0A
 %0A%0A%0A%0A

move old download script to old version fo file