from __future__ import print_function
import datetime
from .util import parse_time, safe_text


def natural_join(items):
    items = [str(x) for x in items if x]
    if not items:
        return ""
    if len(items) == 1:
        return items[0]
    if len(items) == 2:
        return items[0] + " and " + items[1]
    return ", ".join(items[:-1]) + ", and " + items[-1]


def area_names(alert, configured_areas):
    names = []
    for code in alert.matched_areas:
        names.append(configured_areas.get(code, code))
    if not names and alert.area_desc:
        names = [part.strip() for part in alert.area_desc.split(";") if part.strip()][:6]
    return names


def display_time(value):
    dt = parse_time(value)
    if not dt:
        return ""
    try:
        local = dt.astimezone()
    except Exception:
        local = dt
    hour = local.hour
    suffix = "A M" if hour < 12 else "P M"
    shown = hour % 12 or 12
    if local.minute:
        return "%d:%02d %s" % (shown, local.minute, suffix)
    return "%d %s" % (shown, suffix)


def alert_clause(alert, configured_areas, include_areas=True, include_expiration=True):
    text = alert.event or "Weather alert"
    if include_areas:
        names = area_names(alert, configured_areas)
        if names:
            text += " for " + natural_join(names)
    if include_expiration:
        ending = display_time(alert.ends or alert.expires)
        if ending:
            text += ", in effect until " + ending
    return text


def new_alert_sentence(alerts, configured_areas, settings, update=False):
    clauses = [alert_clause(a, configured_areas, settings.get("include_areas", True), settings.get("include_expiration", True)) for a in alerts]
    lead = "Bayou Radar Skywarn update. " if update else "Bayou Radar Skywarn weather alert. "
    return lead + natural_join(clauses) + ". Data source, National Weather Service."


def clear_sentence(alerts, all_clear=False):
    if all_clear:
        return "Bayou Radar Skywarn all clear. There are no active weather alerts for the monitored area."
    names = sorted(set(a.event for a in alerts if a.event))
    noun = "alert has" if len(names) == 1 else "alerts have"
    return "Bayou Radar Skywarn update. The following %s been cleared: " % noun + natural_join(names) + "."


def tail_sentence(alerts, configured_areas, settings):
    clauses = [alert_clause(a, configured_areas, settings.get("include_areas", True), settings.get("include_expiration", True)) for a in alerts]
    return "Bayou Radar Skywarn active alerts. " + natural_join(clauses) + "."


def description_sentence(alert, configured_areas, settings):
    intro = alert_clause(alert, configured_areas, True, True) + ". "
    description = safe_text(alert.description, int(settings.get("max_description_chars", 850)))
    instruction = safe_text(alert.instruction, 450)
    text = intro + description
    if instruction:
        text += " Safety information. " + instruction
    return text + " Data source, National Weather Service."
