"""CW (Morse) transmit signal generator: text -> keying -> shaped carrier -> SigMF (ci16_le).

Timing is PARIS: dot = 1.2 / wpm s, dash = 3 dots, gaps of 1 (elements), 3 (characters) and 7 (words) dots.
Prosigns are written in angle brackets and sent without character gaps, e.g. "<AR>", "<SK>", "<BT>".
Each key edge is a raised-cosine ramp of rise_s (0-100 %), centered on the nominal edge, so the element
lengths at the 50 % points are exact.

With space_shift set the carrier is not switched off but FSK keyed: key-down sends tone_freq (mark), key-up
tone_freq + space_shift (space). The frequency follows the same raised-cosine edges and the phase is
continuous, so the envelope is constant apart from a rise_s ramp at the very start and end.
"""

import datetime
import json
import os

import numpy as np

MORSE = {
    "A": ".-", "B": "-...", "C": "-.-.", "D": "-..", "E": ".", "F": "..-.", "G": "--.", "H": "....",
    "I": "..", "J": ".---", "K": "-.-", "L": ".-..", "M": "--", "N": "-.", "O": "---", "P": ".--.",
    "Q": "--.-", "R": ".-.", "S": "...", "T": "-", "U": "..-", "V": "...-", "W": ".--", "X": "-..-",
    "Y": "-.--", "Z": "--..",
    "0": "-----", "1": ".----", "2": "..---", "3": "...--", "4": "....-", "5": ".....", "6": "-....",
    "7": "--...", "8": "---..", "9": "----.",
    ".": ".-.-.-", ",": "--..--", "?": "..--..", "'": ".----.", "!": "-.-.--", "/": "-..-.", "(": "-.--.",
    ")": "-.--.-", "&": ".-...", ":": "---...", ";": "-.-.-.", "=": "-...-", "+": ".-.-.", "-": "-....-",
    "_": "..--.-", '"': ".-..-.", "$": "...-..-", "@": ".--.-.",
}
_DECODE = {v: k for k, v in MORSE.items()}


def to_chars(text):
    """Text -> list of (label, pattern) per character, None for a word gap."""
    out, i, text = [], 0, text.upper()
    while i < len(text):
        c = text[i]
        if c.isspace():
            if out and out[-1] is not None:
                out.append(None)
            i += 1
        elif c == "<":
            j = text.find(">", i)
            if j < 0:
                raise ValueError("unclosed prosign at position %d" % i)
            letters = text[i + 1:j]
            if not letters or any(ch not in MORSE for ch in letters):
                raise ValueError("invalid prosign %s" % text[i:j + 1])
            out.append((text[i:j + 1], "".join(MORSE[ch] for ch in letters)))
            i = j + 1
        elif c in MORSE:
            out.append((c, MORSE[c]))
            i += 1
        else:
            raise ValueError("no Morse code for %r" % c)
    while out and out[-1] is None:
        out.pop()
    if not out:
        raise ValueError("empty text")
    return out


def key_intervals(chars, repeats=1):
    """Key-down intervals in dot units: arrays (on, off)."""
    seq = []
    for r in range(repeats):
        seq += ([None] if r else []) + chars
    on, off, t, prev = [], [], 0, "start"
    for ch in seq:
        if ch is None:
            prev = None
            continue
        t += {"start": 0, None: 7}.get(prev, 3)
        for k, e in enumerate(ch[1]):
            if k:
                t += 1
            on.append(t)
            t += 1 if e == "." else 3
            off.append(t)
        prev = ch
    return np.array(on, float), np.array(off, float)


def units(text, repeats=1):
    """Length of the text in dot units (PARIS = 50)."""
    return key_intervals(to_chars(text), repeats)[1][-1]


def wpm_for_duration(text, duration_s, sample_rate, rise_s=0.005, repeats=1):
    """Speed at which the text fills duration_s exactly: the signal is then round(duration_s * sample_rate)
    samples, from the start of the first key edge to the end of the last one."""
    dot = ((round(duration_s * sample_rate) - 1) / sample_rate - rise_s) / units(text, repeats)
    if dot <= 0:
        raise ValueError("duration_s too short")
    return 1.2 / dot


class CWParams:
    def __init__(self, wpm=15.0, sample_rate=250e3, center_freq=1299.650e6, tone_freq=1000.0, rise_s=0.005,
                 space_shift=None):
        self.wpm = wpm                    # words per minute, PARIS
        self.sample_rate = sample_rate    # Hz
        self.center_freq = center_freq    # Hz, SDR tuning frequency = USB dial frequency
        self.tone_freq = tone_freq        # Hz, carrier offset from center_freq (the tone heard in USB)
        self.rise_s = rise_s              # s, raised-cosine key edge, 0-100 %
        self.space_shift = space_shift or None   # Hz, key-up tone above tone_freq; None = on/off keying
        if not 0 <= rise_s <= self.dot_s:
            raise ValueError("rise_s must be between 0 and one dot (%.1f ms)" % (self.dot_s * 1e3))
        if max(abs(tone_freq), abs(tone_freq + (self.space_shift or 0))) + 2 / max(rise_s, 1e-3) >= sample_rate / 2:
            raise ValueError("sample rate too low for tone_freq / space_shift")

    @property
    def dot_s(self):
        return 1.2 / self.wpm

    @property
    def carrier_freq(self):
        return self.center_freq + self.tone_freq

    @property
    def mode(self):
        return "CW" if self.space_shift is None else "CW FSK %+g Hz" % self.space_shift

    def summary(self, text, repeats=1):
        n = units(text, repeats)
        lines = [
            "%.4g wpm: dot %.1f ms, dash %.1f ms, rise %.1f ms" % (
                self.wpm, self.dot_s * 1e3, 3 * self.dot_s * 1e3, self.rise_s * 1e3),
            "%d x '%s': %d dot units, %.3f s" % (repeats, text, n, n * self.dot_s + self.rise_s),
            "Carrier %.6f MHz = center %.6f MHz %+g Hz, %g Hz sample rate" % (
                self.carrier_freq / 1e6, self.center_freq / 1e6, self.tone_freq, self.sample_rate),
        ]
        if self.space_shift is not None:
            lines.append("FSK: key-up (space) tone %.6f MHz = carrier %+g Hz" % (
                (self.carrier_freq + self.space_shift) / 1e6, self.space_shift))
        return "\n".join(lines)


def _ramp(u):
    u = np.clip(u, 0, 1)
    return 0.5 * (1 - np.cos(np.pi * u))


def envelope(t, on, off, rise):
    """Keying envelope at times t (s) for key-down intervals on/off (s)."""
    if rise <= 0:
        i = np.searchsorted(on, t, "right") - 1
        ic = np.maximum(i, 0)
        return ((i >= 0) & (t < off[ic])).astype(float)
    i = np.searchsorted(on, t + rise / 2, "right") - 1   # last interval whose rising edge has started
    ic = np.maximum(i, 0)
    e = _ramp((t - on[ic]) / rise + 0.5) - _ramp((t - off[ic]) / rise + 0.5)
    return np.where(i >= 0, e, 0.0)


def timing(text, p, repeats=1):
    """Key-down intervals in seconds and the signal length in samples. First ramp starts at t = 0."""
    on, off = key_intervals(to_chars(text), repeats)
    on, off = on * p.dot_s + p.rise_s / 2, off * p.dot_s + p.rise_s / 2
    return on, off, int(np.ceil((off[-1] + p.rise_s / 2) * p.sample_rate - 1e-6)) + 1


def chunks(text, p, repeats=1, chunk=1 << 20):
    """Yields (first sample index, complex128 unit-amplitude samples)."""
    on, off, n_total = timing(text, p, repeats)
    t_end = off[-1] + p.rise_s / 2
    space_cyc = 0.0                                          # accumulated extra phase of the space tone
    for n0 in range(0, n_total, chunk):
        n = np.arange(n0, min(n0 + chunk, n_total))
        t = n / p.sample_rate
        cyc = np.mod(p.tone_freq * n / p.sample_rate, 1.0)   # continuous carrier phase
        key = envelope(t, on, off, p.rise_s)
        if p.space_shift is None:
            yield n0, key * np.exp(2j * np.pi * cyc)
            continue
        # frequency tone_freq + space_shift * (1 - key) from sample n to n + 1, phase continuous
        dcyc = p.space_shift * (1 - key) / p.sample_rate
        acc = space_cyc + np.cumsum(dcyc) - dcyc
        space_cyc = np.mod(acc[-1] + dcyc[-1], 1.0)
        amp = 1.0 if p.rise_s <= 0 else _ramp(t / p.rise_s) * _ramp((t_end - t) / p.rise_s)
        yield n0, amp * np.exp(2j * np.pi * np.mod(cyc + acc, 1.0))


# ---------------------------------------------------------------------------------------------------------
# SigMF
# ---------------------------------------------------------------------------------------------------------

def start_time(start_utc=None):
    """Aware UTC datetime: start_utc (ISO 8601), or the next full UTC minute."""
    if start_utc is None:
        now = datetime.datetime.now(datetime.timezone.utc)
        return datetime.datetime.fromtimestamp((int(now.timestamp()) // 60 + 1) * 60, datetime.timezone.utc)
    t0 = datetime.datetime.fromisoformat(start_utc.replace("Z", "+00:00"))
    return t0 if t0.tzinfo else t0.replace(tzinfo=datetime.timezone.utc)


def format_iso(t):
    return t.astimezone(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")


def _check_new(*paths, overwrite=False):
    if not overwrite:
        for path in paths:
            if os.path.exists(path):
                raise FileExistsError("%s exists (set overwrite=True to replace it)" % path)


def write_sigmf(base, text, p, t0, repeats=1, backoff_db=1.0, author="", tx_gain=None, overwrite=False):
    """Write base.sigmf-data (ci16_le) and base.sigmf-meta. Returns (meta file, peak int16).
    Existing files are only replaced with overwrite=True; otherwise neither file is touched."""
    from sigmf import SigMFFile

    data_file, meta_file = base + ".sigmf-data", base + ".sigmf-meta"
    _check_new(data_file, meta_file, overwrite=overwrite)
    amplitude = (2 ** 15 - 1) * 10 ** (-backoff_db / 20)
    on, off, n_total = timing(text, p, repeats)
    iq = np.zeros(2 * n_total, dtype="<i2")
    for n0, z in chunks(text, p, repeats):
        z = amplitude * z
        iq[2 * n0:2 * (n0 + len(z)):2] = np.round(z.real)
        iq[2 * n0 + 1:2 * (n0 + len(z)):2] = np.round(z.imag)
    iq.tofile(data_file)
    peak = int(np.max(np.abs(iq)))

    chars = to_chars(text)
    global_info = {
        SigMFFile.DATATYPE_KEY: "ci16_le",
        SigMFFile.SAMPLE_RATE_KEY: p.sample_rate,
        SigMFFile.AUTHOR_KEY: author,
        SigMFFile.DESCRIPTION_KEY: "%s %.4g wpm: %s" % (p.mode, p.wpm, text.upper()),
        SigMFFile.EXTENSIONS_KEY: [{"name": "cw", "version": "1.0.0", "optional": True},
                                   {"name": "tx", "version": "1.0.0", "optional": True}],
        "tx:backoff_db": backoff_db,
        "cw:text": text.upper(),
        "cw:morse": " / ".join(" ".join(c[1] for c in w) for w in _words(chars)),
        "cw:wpm": p.wpm,
        "cw:dot_s": p.dot_s,
        "cw:rise_s": p.rise_s,
        "cw:tone_freq": p.tone_freq,
        "cw:repeats": repeats,
    }
    if p.space_shift is not None:
        global_info["cw:space_shift"] = p.space_shift
    if tx_gain is not None:
        global_info[SigMFFile.EXTENSIONS_KEY].insert(0, {"name": "vrt", "version": "1.0.0", "optional": True})
        global_info["vrt:tx_gain"] = tx_gain

    meta = SigMFFile(data_file=data_file, global_info=global_info)
    meta.add_capture(0, metadata={SigMFFile.FREQUENCY_KEY: p.center_freq,
                                  SigMFFile.DATETIME_KEY: format_iso(t0)})
    bw = 1 / max(p.rise_s, 1e-3)
    f_space = p.carrier_freq + (p.space_shift or 0)
    meta.add_annotation(0, n_total, metadata={
        SigMFFile.FLO_KEY: min(p.carrier_freq, f_space) - bw,
        SigMFFile.FHI_KEY: max(p.carrier_freq, f_space) + bw,
        SigMFFile.LABEL_KEY: "%s %.4g wpm" % (p.mode, p.wpm),
        SigMFFile.COMMENT_KEY: text.upper(),
    })
    meta.tofile(meta_file, overwrite=overwrite)
    return meta_file, peak


def _words(chars):
    words, cur = [], []
    for c in chars:
        if c is None:
            words.append(cur)
            cur = []
        else:
            cur.append(c)
    return words + [cur]


def read_sigmf(meta_file):
    """Returns (complex64 samples, metadata dict)."""
    meta = json.load(open(meta_file))
    iq = np.fromfile(meta_file[:-len(".sigmf-meta")] + ".sigmf-data", dtype="<i2").astype(np.float32)
    return (iq[0::2] + 1j * iq[1::2]).astype(np.complex64), meta


# ---------------------------------------------------------------------------------------------------------
# checks
# ---------------------------------------------------------------------------------------------------------

def decode(x, fs, wpm, tone_freq=None, space_shift=None):
    """Decode the keying of a noise-free recording. Returns (text, key-down edges in s as (on, off) arrays).
    With space_shift (and tone_freq) the keying is FSK: key down where the frequency is nearer the mark tone."""
    a = np.abs(x)
    if space_shift is None:
        key = a > 0.5 * np.max(a)
    else:
        # mix the mid frequency to 0 Hz, then the sign of the phase step over 2 h samples centered on each
        # sample: mean frequency below 0 = mark. The symmetric average keeps the 50 % edge positions. The
        # start and end amplitude ramps have the shape of the first and last key edge, so the 50 % amplitude
        # points are those key edges.
        n = np.arange(len(x))
        y = x * np.exp(-2j * np.pi * np.mod((tone_freq + space_shift / 2) * n / fs, 1.0))
        h = max(1, int(min(fs / (8 * abs(space_shift)), fs * 1.2 / wpm / 4)))   # < 1/4 cycle, < 1/2 dot
        up = a > 0.5 * np.max(a)
        key = (np.sign(space_shift) * np.imag(y[2 * h:] * np.conj(y[:-2 * h])) < 0) | ~(up[2 * h:] & up[:-2 * h])
        key = np.pad(key, h, constant_values=True) & up
    d = np.diff(key.astype(np.int8))
    on = (np.flatnonzero(d == 1) + 1) / fs
    off = (np.flatnonzero(d == -1) + 1) / fs
    if key[0]:
        on = np.r_[0.0, on]
    if key[-1]:
        off = np.r_[off, len(x) / fs]
    dot = 1.2 / wpm
    text, pat = "", ""
    for k in range(len(on)):
        pat += "." if (off[k] - on[k]) < 2 * dot else "-"
        gap = (on[k + 1] - off[k]) / dot if k + 1 < len(on) else np.inf
        if gap >= 2:
            text += _DECODE.get(pat, "<%s>" % pat)
            pat = ""
            if gap >= 5:
                text += " "
    return text.strip(), (on, off)


def to_audio(x, p, rate=12000):
    """USB receiver tuned to center_freq: real audio at rate Hz, the carrier heard at tone_freq. Peak 1."""
    from fractions import Fraction
    from scipy.signal import resample_poly

    lo, hi = sorted([p.tone_freq, p.tone_freq + (p.space_shift or 0)])
    if not (0 < lo and hi + 2 / max(p.rise_s, 1e-3) < rate / 2):
        raise ValueError("tone_freq (and + space_shift) must be positive and below rate / 2 for USB audio")
    r = Fraction(rate) / Fraction(p.sample_rate).limit_denominator(10 ** 9)
    a = resample_poly(x.astype(np.complex128), r.numerator, r.denominator).real
    return a / np.max(np.abs(a))


def write_wav(path, audio, rate=12000, backoff_db=1.0, overwrite=False):
    """16-bit mono WAV, peak backoff_db below full scale. Returns the peak int16 value.
    An existing file is only replaced with overwrite=True."""
    import wave

    _check_new(path, overwrite=overwrite)
    pcm = np.round(audio / np.max(np.abs(audio)) * (2 ** 15 - 1) * 10 ** (-backoff_db / 20)).astype("<i2")
    with wave.open(path, "wb") as f:
        f.setnchannels(1)
        f.setsampwidth(2)
        f.setframerate(int(rate))
        f.writeframes(pcm.tobytes())
    return int(np.max(np.abs(pcm)))


def as_decoded(text, repeats=1):
    """The text as decode() returns it (prosigns that equal a character show as that character)."""
    s = " ".join("".join(_DECODE.get(c[1], "<%s>" % c[1]) for c in w) for w in _words(to_chars(text)))
    return " ".join([s] * repeats)
