import math
import numpy as np
from numbers import Number
import random
from typing import List
from pojagi_dsp.channel.ecg import Segments
from pojagi_dsp.channel.ecg.generator.wavetable import ECGWaveTable

# NOTE: larger table required for avoiding aliasing at different srates than 125Hz
sinus_data = np.array([
    # R-S: 0
    2000,
    1822,
    374,
    # S-Jp: 3
    -474,
    -271,
    -28,
    18,
    66,
    # Jp-T: 9
    63,
    73,
    91,
    101,
    101,
    101,
    116,
    124,
    124,
    # T: 17
    141,
    171,
    186,
    196,
    229,
    265,
    297,
    327,
    363,
    406,
    446,
    475,
    493,
    508,
    526,
    533,
    518,
    475,
    403,
    327,
    272,
    222,
    174,
    138,
    109,
    88,
    73,
    66,
    69,
    69,
    66,
    73,
    81,
    76,
    73,
    76,
    76,
    66,
    58,
    58,
    63,
    63,
    41,
    26,
    26,
    18,
    8,
    8,
    8,
    # U: 66 -- not found
    # T-P: 66
    2,
    3,
    2,
    2,
    2,
    -1,
    2,
    2,
    2,
    -1,
    0,
    -1,
    -1,
    3,
    2,
    1,
    3,
    2,
    1,
    0,
    1,
    # P: 87
    0,
    3,
    11,
    11,
    0,
    8,
    18,
    18,
    18,
    15,
    8,
    18,
    26,
    26,
    26,
    8,
    32,
    61,
    116,
    164,
    182,
    159,
    131,
    116,
    116,
    109,
    91,
    73,
    58,
    55,
    58,
    63,
    69,
    # P-R: 120
    48,
    -14,
    # Q-R: 122
    -40,
    131,
    931,
])  # len == 125


pr_idx = 120
t_idx = 18
t_pr_idx_diff = pr_idx - t_idx
t_pr = np.arange(-math.floor(t_pr_idx_diff / 2), math.ceil(t_pr_idx_diff / 2))
t_pr_curve: np.ndarray = t_pr**2 * -0.2
t_pr_curve = (t_pr_curve - t_pr_curve[0]) + 141

tachycardia = np.array(
    [  # 58-107 flat
        # R-S: 0
        2000,
        1822,
        374,
        # S-Jp: 3
        -474,
        -271,
        -28,
        18,
        66,
        # Jp-T: 8
        63,
        73,
        91,
        101,
        101,
        101,
        116,
        124,
        124,
        *t_pr_curve,
        # P-R: 119
        124,
        48,
        -14,
        # Q-R: 122
        -40,
        731,
        1231,
    ]
)  # len == 125


def SinusWaveTable():
    return ECGWaveTable(
        data=sinus_data,
        segments=Segments(
            S=3,
            S_T=9,
            T=17,
            T_P=66,
            P=87,
            P_R=120,
            Q=122,
        ),
    )


def TachycardiaWaveTable():
    return ECGWaveTable(
        data=tachycardia,
        segments=Segments(
            S=3,
            S_T=8,
            T=17,
            T_P=66,
            P=87,
            P_R=119,
            Q=122,
        ),
        # Tachy is weaker than sinus, so we inflate the range here, which
        # effectively attenuates the signal by 1/3 (i.e., it is 2/3 the
        # original).
        top=2000 * (3 / 2),
        bottom=0,
    )

impulse_data = np.array([1, *([0] * 124)])
def ImpulseWaveTable():
    return ECGWaveTable(
        data=impulse_data,
        segments=Segments(
            S=3,
            S_T=9,
            T=17,
            T_P=66,
            P=87,
            P_R=119,
            Q=122,
        ),
        top=1,
        bottom=0,
    )


if __name__ == "__main__":
    from matplotlib import pyplot as plt
    
    samples = np.tile(SinusWaveTable().data, 3)

    plt.plot(range(len(samples)), samples)
    plt.show()
