"""Q65 EME receiver: SigMF recordings -> Doppler-corrected 12 kHz baseband -> known-message search,
polarisation combining, and decoding with WSJT-X jt9.

The echo time and the Doppler come from sigmf_dedoppler.py (SPICE, sub-radar point, any tx/rx site pair).
The Doppler is removed by the integrated phase of the interpolated Doppler, as in sigmf_dedoppler.py, and the
result is resampled to 12 kHz complex baseband: a USB receiver on the capture frequency, the Q65 tones at
their WSJT-X audio frequencies. Sample k of a baseband stream is at file time k / 12000 s.
"""

import datetime
import json
import pathlib
from fractions import Fraction

import numpy as np
import scipy.interpolate
import scipy.signal
import spiceypy as sp

import q65
import sigmf_dedoppler as sd

RATE = 12000      # Hz, WSJT-X audio sample rate


def load_kernels(kernel_dir=sd.KERNEL_DIR):
    for kernel in sd.KERNELS:
        sp.furnsh(str(pathlib.Path(kernel_dir) / kernel))


def et_to_datetime(et):
    return datetime.datetime.fromisoformat(sp.et2utc(et, "ISOC", 6)).replace(tzinfo=datetime.timezone.utc)


# ---------------------------------------------------------------------------------------------------------
# recording and transmission
# ---------------------------------------------------------------------------------------------------------

def open_recording(meta_file):
    """Metadata of a ci16_le / cf32_le SigMF recording: dict with global, capture, fs, et_start, n_samples."""
    meta_path = pathlib.Path(meta_file)
    meta = json.loads(meta_path.read_text())
    g, c = meta["global"], meta["captures"][0]
    dtype = sd.DTYPES[g["core:datatype"]]
    data_path = meta_path.with_name(meta_path.name.replace(".sigmf-meta", ".sigmf-data"))
    return {
        "meta_file": str(meta_path), "data_path": data_path, "dtype": dtype, "global": g, "capture": c,
        "fs": float(g["core:sample_rate"]), "f_center": float(c["core:frequency"]),
        "et_start": sp.str2et(c["core:datetime"].rstrip("Z")),
        "n_samples": data_path.stat().st_size // (2 * dtype.itemsize),
    }


def tx_info(rec, tx_dir, tx_time=None, target=None):
    """The transmission behind a recording, from core:description 'waveform.sigmf-meta;nr;tx_unix;target' and the
    Q65 metadata of the transmitted waveform file (written by tx_q65.ipynb)."""
    desc = rec["global"].get("core:description", "")
    tx_unix, desc_target, _ = sd.parse_description(desc)
    tx_file = pathlib.Path(tx_dir) / desc.split(";")[0]
    txg = json.loads(tx_file.read_text())["global"]
    if "q65:tones" not in txg:
        raise ValueError("%s has no Q65 metadata" % tx_file)
    if tx_time is not None:
        et_tx = sd.parse_time(str(tx_time))
    elif tx_unix is not None:
        et_tx = sd.unix_to_et(tx_unix)
    else:
        raise ValueError("no transmit time in core:description, set tx_time")
    lead = txg["q65:start_delay_s"] if txg.get("q65:lead_in", True) else 0.0
    p = q65.Q65Params(submode=txg["q65:submode"], tr_period=int(txg["q65:tr_period"]), sample_rate=RATE,
                      audio_freq=float(txg["q65:audio_freq"]))
    return {
        "tx_file": str(tx_file), "p": p, "tones": np.array(txg["q65:tones"]),
        "message": txg.get("q65:message_sent", txg.get("q65:message", "")),
        "et_tx_file": et_tx, "et_tx_signal": et_tx + lead, "start_delay": txg["q65:start_delay_s"],
        "target": (target or desc_target or "moon").lower(),
    }


def echo_prediction(rec, tx, tx_site, rx_site):
    """Echo arrival of the Q65 signal start (file time, s), round trip time, and the Doppler on a 1 s grid."""
    target = tx["target"]
    frame = sd.TARGETS[target]
    T, R = tx_site.upper(), rx_site.upper()
    et_echo, rtt = sd.echo_reception_time(target.upper(), frame, tx["et_tx_signal"], T, R)
    duration = rec["n_samples"] / rec["fs"]
    t_grid = np.arange(-2.0, np.ceil(duration) + 3.0)
    dlt = np.array([sd.radar_srp(target.upper(), frame, rec["et_start"] + t, T, R)[0] for t in t_grid])
    return {"t_echo": et_echo - rec["et_start"], "et_echo": et_echo, "rtt": rtt,
            "t_grid": t_grid, "doppler": -dlt * rec["f_center"]}


def baseband(rec, t_grid, doppler, correct_doppler=True):
    """Doppler-corrected complex baseband at 12 kHz, normalized to unit noise power."""
    fs = rec["fs"]
    raw = np.memmap(rec["data_path"], dtype=rec["dtype"], mode="r", shape=(2 * rec["n_samples"],))
    x = raw[0::2].astype(np.float64) + 1j * raw[1::2].astype(np.float64)
    if correct_doppler:
        cycles = scipy.interpolate.CubicSpline(t_grid, doppler).antiderivative()
        x *= np.exp(-2j * np.pi * np.mod(cycles(np.arange(len(x)) / fs) - cycles(0.0), 1.0))
    r = Fraction(RATE) / Fraction(fs).limit_denominator(10 ** 9)
    z = scipy.signal.resample_poly(x, r.numerator, r.denominator)     # zero-phase: timing is kept
    return z / np.sqrt(np.median(np.abs(z) ** 2) / np.log(2))         # median: robust against the echo


# ---------------------------------------------------------------------------------------------------------
# known-message search, echo strength, polarisation combining
# ---------------------------------------------------------------------------------------------------------

def tone_search(z, tones, p, t_center, f0=None, dt_span=1.0, f_span=300.0, oversample_t=12, oversample_f=2):
    """Matched search for the known tone sequence over start time and frequency.

    Symbol-length rectangular FFTs every 1/oversample_t symbol, oversample_f bins per baud, each bin
    normalized to unit mean noise. S[i, j] is the mean power at the 85 expected tones for a signal starting at
    t_center + dt[i] with the sync tone at f0 + df[j] (f0 default audio_freq): 1 for noise, 1 + S/N per tone
    for the echo.
    """
    f0 = p.audio_freq if f0 is None else f0
    nsps = p.nsps
    hop, nfft = nsps // oversample_t, nsps * oversample_f
    step = 2 ** "ABCDE".index(p.submode) * oversample_f              # tone spacing in bins
    t_first = t_center - dt_span
    i0 = int(round(t_first * RATE))
    n_dt = int(round(2 * dt_span * RATE / hop)) + 1
    n_frames = n_dt + oversample_t * (q65.N_SYMBOLS - 1)
    seg = np.zeros((n_frames - 1) * hop + nsps, dtype=complex)
    a, b = max(i0, 0), min(i0 + len(seg), len(z))
    seg[a - i0:b - i0] = z[a:b]
    frames = np.lib.stride_tricks.sliding_window_view(seg, nsps)[::hop][:n_frames]
    P = np.abs(np.fft.fft(frames, nfft, axis=1)) ** 2
    P /= np.median(P, axis=0, keepdims=True) / np.log(2)
    df_bin = RATE / nfft
    b0 = int(round(f0 / df_bin))
    nb = int(round(f_span / df_bin))
    offs = np.arange(-nb, nb + 1)
    S = np.zeros((n_dt, len(offs)))
    for k, tone in enumerate(tones):
        S += P[oversample_t * k:oversample_t * k + n_dt][:, (b0 + offs + step * tone) % nfft]
    S /= q65.N_SYMBOLS
    dt = (i0 + np.arange(n_dt) * hop) / RATE - t_center
    return {"dt": dt, "df": offs * df_bin + (b0 * df_bin - f0), "S": S, "f0": f0}


def search_peak(res, f_search=20.0):
    """Peak of a tone_search within |df| <= f_search, its significance against the whole searched grid."""
    S, dt, df = res["S"], res["dt"], res["df"]
    sel = np.abs(df) <= f_search
    i, j = np.unravel_index(np.argmax(np.where(sel[None, :], S, -np.inf)), S.shape)
    med = np.median(S)
    sigma = 1.4826 * np.median(np.abs(S - med))
    return {"dt": dt[i], "df": df[j], "value": S[i, j], "sigma": (S[i, j] - med) / sigma}


def dehopped(z, tones, p, t_start, f0, offsets):
    """Complex FFT (1 bin = 1 baud) of each of the 85 symbols, at bin offsets from the expected tone of that
    symbol (row k = symbol k), normalized to unit noise power per bin; noise measured 300-1500 Hz away."""
    nsps = p.nsps
    i0 = int(round(t_start * RATE))
    seg = np.zeros(q65.N_SYMBOLS * nsps, dtype=complex)
    a, b = max(i0, 0), min(i0 + len(seg), len(z))
    seg[a - i0:b - i0] = z[a:b]
    F = np.fft.fft(seg.reshape(q65.N_SYMBOLS, nsps), axis=1)
    fb = np.fft.fftfreq(nsps, 1 / RATE)
    centre = f0 + 32 * p.tone_spacing
    far = (np.abs(fb - centre) > 300) & (np.abs(fb - centre) < 1500)
    F /= np.sqrt(np.mean(np.abs(F[:, far]) ** 2))
    step = 2 ** "ABCDE".index(p.submode)                              # tone spacing in bins
    bins = int(round(f0 / p.baud)) + step * np.asarray(tones)[:, None] + np.asarray(offsets)[None, :]
    return F[np.arange(q65.N_SYMBOLS)[:, None], bins % nsps]


def echo_strength(z, tones, p, t_start, f0, spread_bins=1):
    """Excess power per 1-baud bin around the expected tones (noise = 1), and the S/N in 2500 Hz (WSJT-X
    convention) from the bins within +-spread_bins of the tone."""
    offsets = np.arange(-4, 5)
    D = np.abs(dehopped(z, tones, p, t_start, f0, offsets)) ** 2
    profile = D.mean(axis=0) - 1
    excess = profile[np.abs(offsets) <= spread_bins].sum()
    snr = 10 * np.log10(excess * p.baud / 2500) if excess > 0 else -np.inf
    return {"offsets": offsets, "profile": profile, "excess": excess, "snr_db": snr}


def combine_weight(z0, z1, tones, p, t_start, f0):
    """Maximal-ratio weight w for (z0 + w z1) / sqrt(1 + |w|^2), from the expected tone bins.

    With unit, independent noise in both streams, E[s0 conj(s1)] = h0 conj(h1) and E|s0|^2 - 1 = |h0|^2, so
    w = conj(h1) / conj(h0) = E[s0 conj(s1)] / (E|s0|^2 - 1).
    """
    s0 = dehopped(z0, tones, p, t_start, f0, [0])[:, 0]
    s1 = dehopped(z1, tones, p, t_start, f0, [0])[:, 0]
    p0, p1 = np.mean(np.abs(s0) ** 2) - 1, np.mean(np.abs(s1) ** 2) - 1
    cxy = np.mean(s0 * np.conj(s1))
    w = cxy / p0 if p0 > 0.05 else cxy / abs(cxy)
    return {"w": w, "phase_deg": np.degrees(np.angle(cxy)), "p0": p0, "p1": p1,
            "coherence": abs(cxy) / np.sqrt(max(p0, 1e-9) * max(p1, 1e-9))}


def combine(z0, z1, w):
    return (z0 + w * z1) / np.sqrt(1 + abs(w) ** 2)


# ---------------------------------------------------------------------------------------------------------
# WSJT-X
# ---------------------------------------------------------------------------------------------------------

def period_audio(z, t_period_start, tr_period, rms=300.0):
    """One T/R period of USB audio (real part) starting at file time t_period_start, as int16."""
    n = tr_period * RATE
    i0 = int(round(t_period_start * RATE))
    seg = np.zeros(n, dtype=complex)
    a, b = max(i0, 0), min(i0 + n, len(z))
    seg[a - i0:b - i0] = z[a:b]
    return np.clip(np.round(seg.real * rms * np.sqrt(2)), -32767, 32767).astype("<i2")


def noise_density(z, f_lo, f_hi):
    """Noise power per Hz between f_lo and f_hi (Welch, 1 Hz resolution, median over the band)."""
    f, P = scipy.signal.welch(z, fs=RATE, nperseg=RATE, return_onesided=False)
    return np.median(P[(f >= f_lo) & (f <= f_hi)])


def inject(z, tones, p, t_start, f0, snr_db, phase=0.0):
    """z plus a synthetic Q65 signal with these tones, sync tone at f0, S/N snr_db in 2500 Hz relative to the
    local noise density in the band of the injected signal."""
    pi = q65.Q65Params(submode=p.submode, tr_period=p.tr_period, sample_rate=RATE, audio_freq=f0)
    sig = q65.synthesize(tones, pi, dtype=np.complex128) * np.exp(1j * phase)
    n0 = noise_density(z, f0, pi.top_freq)
    out = z.copy()
    i0 = int(round(t_start * RATE))
    a, b = max(i0, 0), min(i0 + len(sig), len(z))
    out[a:b] += np.sqrt(10 ** (snr_db / 10) * 2500 * n0) * sig[a - i0:b - i0]
    return out
