import pytest
import numpy as np
import time


@pytest.mark.parametrize('linac',
                         ['L0', 'L1', 'HL', 'L2', 'L3'],
                         ids=['L0B', 'L1B', 'HL', 'L2B', 'L3B'])
def test_cavs_connected(linac, request):
    linac = request.getfixturevalue(linac)
    assert all([c.connected for c in linac])


@pytest.mark.parametrize('linac',
                         ['L0', 'L1', 'HL', 'L2', 'L3'],
                         ids=['L0B', 'L1B', 'HL', 'L2B', 'L3B'])
def test_mean(linac, request):
    linac = request.getfixturevalue(linac)
    # assumes sim db init'd with 10MV amplitude, -20deg phase
    amean = np.mean([c.amplitude for c in linac])
    print(f'Mean amplitude = {amean} MV')
    assert np.abs(amean - 10.0) < 0.01
    pmean = np.mean([c.phase for c in linac])
    print(f'Mean phase = {pmean} deg')
    assert np.abs(pmean - -20.0) < 0.01


@pytest.mark.parametrize('linac',
                         ['L0', 'L1', 'HL', 'L2', 'L3'],
                         ids=['L0B', 'L1B', 'HL', 'L2B', 'L3B'])
def test_energy(linac, request):
    linac = request.getfixturevalue(linac)
    # assumes sim db init'd with 10MV amplitude, -20deg phase
    etotal =  np.sum([c.energy for c in linac])
    print(f'Total energy = {etotal} MV')
    assert etotal > 0
    assert np.abs((len(linac) * 10 * np.cos(np.deg2rad(-20))) - etotal) < 0.1


@pytest.mark.parametrize('linac',
                         ['L0', 'L1', 'HL', 'L2', 'L3'],
                         ids=['L0B', 'L1B', 'HL', 'L2B', 'L3B'])
def test_chirp(linac, request):
    linac = request.getfixturevalue(linac)
    # assumes sim db init'd with 10MV amplitude, -20deg phase
    ctotal =  np.sum([c.chirp for c in linac])
    print(f'Total chirp = {ctotal} MV')
    assert ctotal < 0
    assert np.abs((len(linac) * 10 * np.sin(np.deg2rad(-20))) - ctotal) < 0.1


@pytest.mark.parametrize('linac',
                         ['L0', 'L1', 'HL', 'L2', 'L3'],
                         ids=['L0B', 'L1B', 'HL', 'L2B', 'L3B'])
def test_phase(linac, request):
    linac = request.getfixturevalue(linac)
    # assumes sim db init'd with 10MV amplitude, -20deg phase
    for c in linac:
        c.phase = 0

    time.sleep(0.2)
    assert np.abs(np.sum([c.phase for c in linac])) < 0.05

    # reset phase to -20 deg
    for c in linac:
        c.phase = -20

    time.sleep(0.2)
    assert np.abs(20 + np.mean([c.phase for c in linac])) < 0.05


@pytest.mark.parametrize('linac',
                         ['L0', 'L1', 'HL', 'L2', 'L3'],
                         ids=['L0B', 'L1B', 'HL', 'L2B', 'L3B'])
def test_ptof(linac, request):
    linac = request.getfixturevalue(linac)

    # assumes sim db init'd with 10MV amplitude, -20deg phase, 0 deg ptof
    ptof_total = np.sum([c.ptof for c in linac])
    print(f'Total initial PTOF = {ptof_total}')
    assert ptof_total == 0.0

    for c in linac:
        c.ptof = 4

    time.sleep(0.2)
    assert np.abs(np.mean([c.ptof for c in linac]) - 4.0) < 0.001

    # reset ptof to 0 deg (a relative adjustment)
    for c in linac:
        c.ptof = -4

    time.sleep(0.2)
    assert np.abs(np.sum([c.ptof for c in linac])) < 0.001
