"""
Generates a map of WPC Quantitative Precipitation
Forecast (QPF) and most recent US Drought Monitor to the date choosen to
plot the WPC forecast
"""

from datetime import timedelta

import numpy as np
import pygrib
from pyiem.exceptions import NoDataFound
from pyiem.plot import MapPlot, get_cmap, pretty_bins
from pyiem.util import archive_fetch, mm2inch, utc

PDICT = {"120": "Five Day", "168": "Seven Day"}
PDICT2 = {"0": "0z (7 PM CDT)", "12": "12z (7 AM CDT)"}
PDICT3 = {
    "both": "Plot both USDM + WPC Forecast",
    "wpc": "Plot just WPC Forecast",
}
PDICT4 = {
    "auto": "Auto-scale",
    "10": "10 inch max",
    "7": "7 inch max",
    "3.5": "3.5 inch max",
}


def get_description():
    """Return a dict describing how to call this plotter"""
    desc = {"data": False, "cache": 600, "description": __doc__}
    utcnow = utc()
    desc["arguments"] = [
        dict(
            type="csector",
            name="csector",
            default="IA",
            label="Select state/sector to plot",
        ),
        dict(
            type="date",
            name="date",
            default=utcnow.strftime("%Y/%m/%d"),
            label="Select WPC Issuance Date:",
            min="2018/05/11",
            max=utcnow.strftime("%Y/%m/%d"),
        ),
        dict(
            type="select",
            name="z",
            default="0",
            options=PDICT2,
            label="Select WPC Issuance Time",
        ),
        dict(
            type="select",
            name="f",
            default="120",
            options=PDICT,
            label="Select WPC Forecast Period:",
        ),
        dict(
            type="select",
            name="opt",
            default="both",
            options=PDICT3,
            label="Plotting Options:",
        ),
        dict(
            type="select",
            name="scale",
            default="auto",
            options=PDICT4,
            label="WPC Plotting Max Value for Color Ramp:",
        ),
        dict(
            type="cmap", name="cmap", default="gist_ncar", label="Color Ramp:"
        ),
    ]
    return desc


def plotter(ctx: dict):
    """Go"""
    dt = ctx["date"]
    z = ctx["z"]
    period = ctx["f"]
    scale = ctx["scale"]
    valid = utc(dt.year, dt.month, dt.day, int(z))
    with archive_fetch(
        valid.strftime(f"%Y/%m/%d/model/wpc/p{period}m_%Y%m%d%Hf{period}.grb")
    ) as gribfn:
        if gribfn is None:
            raise NoDataFound(f"gribfn {gribfn} missing")
        with pygrib.open(gribfn) as grbs:
            grb = grbs[1]
            lats, lons = grb.latlons()
            precip = mm2inch(grb.values)

    title = (
        f"Weather Prediction Center {PDICT[period]} "
        "Quantitative Precipitation Forecast"
    )
    _t = "US Drought Monitor Overlaid, " if ctx["opt"] == "both" else ""
    subtitle = (
        f"{_t}WPC Forecast {valid:%d %b %Y %H} UTC to "
        f"{(valid + timedelta(hours=int(period))):%d %b %Y %H} UTC"
    )
    mp = MapPlot(
        apctx=ctx,
        title=title,
        subtitle=subtitle,
        continentalcolor="white",
        titlefontsize=16,
        nocaption=True,
    )
    cmap = get_cmap(ctx["cmap"])
    cmap.set_under("#EEEEEE")
    cmap.set_over("black")
    if scale == "auto":
        levs = pretty_bins(0, np.max(precip))
        levs[0] = 0.01
    elif scale == "10":
        levs = np.arange(0, 10.1, 1.0)
        levs[0] = 0.01
    elif scale == "7":
        levs = np.arange(0, 7.1, 0.5)
        levs[0] = 0.01
    elif scale == "3.5":
        levs = np.arange(0, 3.6, 0.25)
        levs[0] = 0.01
    mp.pcolormesh(
        lons,
        lats,
        precip,
        levs,
        cmap=cmap,
        units="inch",
        clip_on=(ctx["csector"] == "iailin"),
    )
    if ctx["opt"] == "both":
        mp.draw_usdm(valid=valid, filled=False, hatched=True)
    if ctx["csector"] == "iailin":
        mp.drawcounties()

    return mp.fig
