"""
Provide nws text for one center for one date.
"""
# stdlib
import datetime
import json

# extras
from paste.request import parse_formvars
from pyiem.util import get_dbconn, html_escape, utc


def application(environ, start_response):
    """Answer request."""
    fields = parse_formvars(environ)
    pgconn = get_dbconn("afos")
    acursor = pgconn.cursor()
    center = fields.get("center", "KOKX")[:4]
    cb = fields.get("callback")
    date = datetime.datetime.strptime(
        fields.get("date", "2020-03-15"), "%Y-%m-%d"
    )
    sts = utc(date.year, date.month, date.day)
    ets = sts + datetime.timedelta(days=1)
    root = {"products": []}
    pil_limiter = ""
    if int(fields.get("opt", 0)) == 1:
        pil_limiter = """
            and substr(pil, 1, 3) in ('AQA', 'CFW', 'DGT', 'DSW', 'FFA',
            'FFS', 'FFW', 'FLS', 'FLW', 'FWW', 'HLS', 'MWS', 'MWW', 'NPW',
            'NOW', 'PNS', 'PSH', 'RER', 'RFW', 'RWR', 'RWS', 'SMW', 'SPS',
            'SRF', 'SQW', 'SVR', 'SVS', 'TCV', 'TOR', 'TSU', 'WCN', 'WSW')
        """

    acursor.execute(
        """
        SELECT data from products where source = %s and
        entered >= %s and entered < %s """
        + pil_limiter
        + """
        ORDER by entered ASC
        """,
        (center, sts, ets),
    )
    for row in acursor:
        root["products"].append({"data": row[0]})

    if cb is None:
        data = json.dumps(root)
    else:
        data = "%s(%s)" % (html_escape(cb), json.dumps(root))

    headers = [("Content-type", "application/json")]
    start_response("200 OK", headers)
    return [data.encode("ascii")]

@@ -45,10 +45,18 @@
 date
+, or not
 .%0A
-
 %22%22%22%0A
@@ -68,23 +68,58 @@
 lib%0A
-import datetime
+from datetime import timezone, datetime, timedelta
 %0Aimp
@@ -485,24 +485,58 @@
     
-date = datetime.
+if fields.get(%22date%22) is not None:%0A        date = 
 date
@@ -549,25 +549,16 @@
 trptime(
-%0A        
 fields.g
@@ -593,23 +593,22 @@
 Y-%25m-%25d%22
+)
 %0A    
-)%0A
     sts 
@@ -650,16 +650,20 @@
 ay)%0A    
+  
+  
 ets = st
@@ -666,25 +666,16 @@
 = sts + 
-datetime.
 timedelt
@@ -683,16 +683,441 @@
 (days=1)
+%0A    else:%0A        sts = datetime.strptime(%0A            fields.get(%22sts%22, %222020-03-15T00:00%22)%5B:16%5D, %22%25Y-%25m-%25dT%25H:%25M%22%0A        )%0A        ets = datetime.strptime(%0A            fields.get(%22ets%22, %222020-03-16T00:00%22)%5B:16%5D, %22%25Y-%25m-%25dT%25H:%25M%22%0A        )%0A        if (ets - sts) %3E timedelta(days=14):%0A            ets = sts + timedelta(days=14)%0A        sts = sts.replace(tzinfo=timezone.utc)%0A        ets = ets.replace(tzinfo=timezone.utc)%0A
 %0A    roo
@@ -1564,27 +1564,16 @@
        %22
-%22%22%0A        
 SELECT d
@@ -1611,24 +1611,16 @@
 = %25s and
-%0A       
  entered
@@ -1622,32 +1622,44 @@
 tered %3E= %25s and 
+%22%0A        f%22
 entered %3C %25s %22%22%22
@@ -1659,55 +1659,21 @@
  %25s 
-%22%22%22%0A        + pil_limiter%0A        + %22%22%22%0A       
+%7Bpil_limiter%7D
  ORD
@@ -1689,27 +1689,16 @@
 ered ASC
-%0A        %22%22
 %22,%0A     

add sts and ets support as per request