def bsa_buff_dest_init(bsa_buff, inclusion, dest_num):
    """
    Need to write to pvs to configure the bsa buffer
    """
    # reset all destination masks
    for index in range(1, 5):
        caput(
            f"BSA:{args.SYS}:{args.UNIT}:{bsa_buff.number}:DST{index}", DEST_UNCHECKED
        )

    # write to destination mode pv, want inclusion
    caput(f"BSA:{args.SYS}:{args.UNIT}:{bsa_buff.number}:DESTMODE", inclusion)

    # write to all aplicable dest mask pvs
    caput(f"BSA:{args.SYS}:{args.UNIT}:{bsa_buff.number}:DST{dest_num}", DEST_CHECKED)
    time.sleep(
        1
    )  # need this or else the buffer will start before the inclusion mask is set


def bsa_buff_ac_init(bsa_buff):
    """
    Extra initialization for AC buffers
    Set time slot to TS4 and rate to 60Hz
    """
    caput(f"BSA:{args.SYS}:{args.UNIT}:{bsa_buff.number}:RATEMODE", 1)
    caput(f"BSA:{args.SYS}:{args.UNIT}:{bsa_buff.number}:ACRATE", "60Hz")

    for time_slot in TIME_SLOTS:
        if time_slot != 4 and time_slot != 1:
            caput(f"BSA:{args.SYS}:{args.UNIT}:{bsa_buff.number}:TS{time_slot}", 0)
        else:
            caput(f"BSA:{args.SYS}:{args.UNIT}:{bsa_buff.number}:TS{time_slot}", 1)


def run_AC_buffer(dest_num, n_measurements):
    """
    input
    --------
    strin representation of destination number
    Runs an AC buffer based on the dest_num

    output
    --------
    AC Rate BSA Buffer
    """
    epics_ctrls.break_point(f"initializing AC buffer for dest{dest_num}")
    ac_buffer = BSABuffer(f"ACpattVerify dest{dest_num}", user="timing team")
    bsa_buff_dest_init(ac_buffer, INCLUSION, dest_num)
    bsa_buff_ac_init(ac_buffer)
    ac_buffer.n_measurements = n_measurements
    return ac_buffer


def run_FR_buffer(dest_num, n_measurements):
    """
    input
    --------
    strin representation of destination number
    Runs an FR buffer based on the dest_num

    output
    --------
    Fixed Rate BSA Buffer
    """
    epics_ctrls.break_point(f"initializing FR buffer for dest{dest_num}")
    fr_buffer = BSABuffer(f"FRpattVerify dest{dest_num}", user="timing team")
    bsa_buff_dest_init(fr_buffer, INCLUSION, dest_num)
    fr_buffer.n_measurements = n_measurements
    return fr_buffer


def get_actual_rates():
    """
    Get the actual rates
    """
    rate_pvs = []
    rates = []
    actual_rates = {}
    for dest in DEST_NUMS:
        if dest < 10:
            dest = "0" + str(dest)
        else:
            dest = str(dest)
        rate_pvs.append(f"{TPG_BASE_PV}:DST{dest}:RATE")


def organize_patt_data(
    pattData, throughputs: dict, pid_spacings: dict, slow_dest_trigs: dict
) -> dict:
    """
    I wanted to make the structure of this a lot easier to follow and access
    Takes in a pattData from the sc timing patts NTTable and formats it
    into a dictionary named pattDataOrg
    see the following example:

    pattData['PATTERN_NAME'] = SC_DIAG0_STD_AC_30_Hz_off_3_SC_BSYD_STD_FR_102_Hz_SC_SXR_EXP_FR_1.0_kHz_off_7

    pattDataOrg['Data'] = {
                    0: {'Name': 'LASER', 'Rate': 0, 'Active': False, 'Timing Source': 'FR'},
                    1: {'Name': 'SC_DIAG0', 'Rate': 30.0, 'Active': True, 'Timing Source': 'AC'},
                    2: {'Name': 'SC_BSYD', 'Rate': 102.0, 'Active': True, 'Timing Source': 'FR'},
                    3: {'Name': 'SC_HXR', 'Rate': 0, 'Active': False, 'Timing Source': 'FR'},
                    4: {'Name': 'SC_SXR', 'Rate': 1000.0, 'Active': True, 'Timing Source': 'FR'}
    }
    """
    pattDataOrg = {}
    pattDataOrg["PATTERN_NAME"] = pattData["PATTERN_NAME"]
    pattDataOrg["Data"] = {}
    for dest_num in DEST_NUMS:

        pattDataOrg["Data"][dest_num] = {}

        pattDataOrg["Data"][dest_num]["Name"] = patt_sel.globals.DEST_NAMES[dest_num]

        pattDataOrg["Data"][dest_num]["Rate"] = pattData[
            f"{patt_sel.globals.DEST_NAMES[dest_num]}_RATE_Hz"
        ]

        patt_time_src = pattData[
            f"{patt_sel.globals.DEST_NAMES[dest_num]}_TIMING_SOURCE"
        ]

        print(f"patt_time_src {patt_time_src}")

        if patt_time_src != "None":
            pattDataOrg["Data"][dest_num]["Active"] = True
        else:
            pattDataOrg["Data"][dest_num]["Active"] = False

        if pattData[
            f"{patt_sel.globals.DEST_NAMES[dest_num]}_TIMING_SOURCE"
        ].__contains__("AC"):
            pattDataOrg["Data"][dest_num]["Timing Source"] = "AC"
        else:
            pattDataOrg["Data"][dest_num]["Timing Source"] = "FR"

        pattDataOrg = add_patt_data(
            pattDataOrg, dest_num, throughputs, "throughput", None
        )

        pattDataOrg = add_patt_data(
            pattDataOrg, dest_num, pid_spacings, "pid_spacings", None
        )

        pattDataOrg = add_patt_data(
            pattDataOrg, dest_num, slow_dest_trigs, "slow_dest_tirgs", None
        )

    return pattDataOrg


def add_patt_data(pattData: dict, dest_num: int, new_data: dict, new_key, default):
    """ """
    if new_data is None:
        pattData["Data"][dest_num][new_key] = default
        return pattData
    if dest_num in new_data.keys():
        pattData["Data"][dest_num][new_key] = new_data[dest_num]
    else:
        pattData["Data"][dest_num][new_key] = default
    return pattData


def get_PIDLHST(buff_num):
    """
    Returns the PIDLHST for the given buff_num
    """
    return caget(f"{TPG_BASE_PV}:PIDLHST{buff_num}")


def init_bpm_dict(patt_data):
    """
    init's the bpm dict with bpm names depending on active dest and dev/prod
    """

    bpm_dict = {}
    host = socket.gethostbyaddr(socket.gethostname())[0]
    print(f"*****host: {host}")

    print(patt_data)

    if host.__contains__("lcls-dev3"):
        bpm_dict = init_dev_bpms(bpm_dict, patt_data)
    else:
        bpm_dict = init_prod_bpms(bpm_dict, patt_data)

    return bpm_dict


def init_prod_bpms(bpm_dict, patt_data):
    """
    inits prod bpms
    """
    # check DIAG0 BPMS
    # BPMS:HTR:960
    if patt_data["Data"][1]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:HTR:960", patt_data["Data"][1]["Buffer"].number, True
        )

    # check BSYD BPMS
    # check BMPS:SLTD:895
    if patt_data["Data"][2]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:SLTD:895", patt_data["Data"][2]["Buffer"].number, True
        )

    # check HXR BPMS
    # kicker: BPMS:SPD:255
    # inline: BPMS:SPH:322
    if patt_data["Data"][3]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:SPD:255", patt_data["Data"][3]["Buffer"].number, False
        )
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:SPD:135", patt_data["Data"][3]["Buffer"].number, True
        )
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:SPH:322", patt_data["Data"][3]["Buffer"].number, True
        )

    # check SXR BPMS
    # kicker: BPMS:SPD:525
    # inline: BPMS:SPS:572
    if patt_data["Data"][4]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:SPD:525", patt_data["Data"][4]["Buffer"].number, False
        )
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:SPD:420", patt_data["Data"][4]["Buffer"].number, True
        )
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:SPS:572", patt_data["Data"][4]["Buffer"].number, True
        )

    if 5 in patt_data["Data"]:
        if patt_data["Data"][5]["Active"]:
            bpm_dict = check_bpm.add_bpm(
                bpm_dict,
                "BPMS:SPD:700",
                patt_data["Data"][4]["Buffer"].number,
                False,
            )
            bpm_dict = check_bpm.add_bpm(
                bpm_dict, "BPMS:SPD:570", patt_data["Data"][4]["Buffer"].number, True
            )
            bpm_dict = check_bpm.add_bpm(
                bpm_dict,
                "BPMS:DASEL:300",
                patt_data["Data"][4]["Buffer"].number,
                True,
            )

    return bpm_dict


def init_dev_bpms(bpm_dict, patt_data):
    """
    inits dev bpms
    """
    if patt_data["Data"][1]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:BSYH:445", patt_data["Data"][1]["Buffer"].number, True
        )
    if patt_data["Data"][2]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:BSYH:465", patt_data["Data"][2]["Buffer"].number, True
        )
    if patt_data["Data"][3]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:UNDH:3190", patt_data["Data"][3]["Buffer"].number, True
        )
    if patt_data["Data"][4]["Active"]:
        bpm_dict = check_bpm.add_bpm(
            bpm_dict, "BPMS:UNDH:3290", patt_data["Data"][4]["Buffer"].number, False
        )

    return bpm_dict


def save_report(test_patt_report, run_date):
    """
    Saves the report of the passed or failed tests
    """

    epics_ctrls.break_point(f"saving reports to: {get_report_file(run_date)}")

    if not os.path.exists(get_report_file(run_date)):
        make_file(get_report_file(run_date))
        report = {"0": test_patt_report}
        test_patt_report_json = json.dumps(report, indent=4)
        with open(get_report_file(run_date), "w") as report_file:
            report_file.write(test_patt_report_json)
        return

    with open(get_report_file(run_date), "r") as report_file:
        old_reports = dict(json.load(report_file))

    report_num = str(len(old_reports))
    report = {report_num: test_patt_report}

    old_reports.update(report)

    all_reports_json = json.dumps(old_reports, indent=4)

    with open(get_report_file(run_date), "w") as report_file:
        report_file.write(all_reports_json)


def save_data(data, var_name):
    """
    Saves the raw data from the test sweet
    input
    --------
    data:
        (dict) data to be saved to data_file
    var_name:
        variable name of data, needed for error outputs

    output
    --------
    Saves data to data_file
    """
    data = helpers.convert_numpy_types(data)

    try:
        json_data = json.dumps(data, indent=4)
    except:
        print(var_name)
        print(data)
        inspect_dict(data)
        pass

    try:
        with open(data_file, "a") as data_file:
            data_file.write(json_data)

    except:
        epics_ctrls.break_point(f"unable to save data for {var_name}")


def inspect_dict(d, i=0):
    """
    very verbose printing of a nested dictionary for troubuleshooting only
    """
    i = i + 1
    for key, val in d.items():
        txt = f"Key '{key}' has value '{val}' of type '{type(val)}'"
        txt_len = len(txt)
        txt_len = txt_len + i * 4
        output = txt.rjust(txt_len)
        print(output)
        if type(val) is dict:
            inspect_dict(val, i)


def test_current_pattern(run_date, run_time):

    test_patt_report = init_test_patt_report(test_patt_name, run_date, run_time)

    testPattData = patt_sel.get_pattern_running_data()
    print("meta data for test patt:")
    print(testPattData)

    try:
        throughputs = patt_sel.get_throughputs(test_patt_name)
    except:
        throughputs = None

    try:
        pid_spacings = patt_sel.get_pid_spacings(test_patt_name)
    except:
        pid_spacings = {}

    try:
        slow_dest_trigs = patt_sel.get_slow_dest_trigger_rates(test_patt_name)
    except:
        slow_dest_trigs = None

    data_file = make_data_file(test_patt_name, run_date, run_time)
    make_file(data_file)

    testPattOrg = organize_patt_data(
        testPattData, throughputs, pid_spacings, slow_dest_trigs
    )

    for dest_num in testPattOrg["Data"]:
        # run for 5 sec if possible
        # cap rate at 20000
        meascnt = int(testPattOrg["Data"][dest_num]["Rate"])
        meascnt = meascnt * 5
        if meascnt > 20000:
            meascnt = 20000

        if not testPattOrg["Data"][dest_num]["Active"]:
            continue

        if testPattOrg["Data"][dest_num]["Timing Source"] == "AC":
            testPattOrg["Data"][dest_num]["Buffer"] = run_AC_buffer(dest_num, meascnt)

        elif testPattOrg["Data"][dest_num]["Timing Source"] == "FR":
            testPattOrg["Data"][dest_num]["Buffer"] = run_FR_buffer(dest_num, meascnt)

    bpm_dict = init_bpm_dict(testPattOrg)

    print(bpm_dict)

    is_bsss_tmit_available = monitor_bsss(bpm_dict, suffix="TMIT")
    if not is_bsss_tmit_available:
        test_patt_report["exit_message"] = "Could not connect to BSSS TMIT"
        # save_report(test_patt_report, run_date)
        # return

    is_bsss_tmit_pid_available = monitor_bsss(bpm_dict, suffix="TMITPID")
    if not is_bsss_tmit_pid_available:
        test_patt_report["exit_message"] = "Could not connect to BSSS TMIT PID"
        # save_report(test_patt_report, run_date)
        # return

    epics_ctrls.break_point("Configuring rate measures")

    rm_dest_time_sources = {}
    rm_dest_rates = {}
    dest_throughputs = helpers.to_str_dests(throughputs)
    for dest in config.RM_DESTS:
        rm_dest_time_sources[dest] = testPattData[f"{dest}_TIMING_SOURCE"]
        if dest_throughputs is not None:
            if dest in dest_throughputs:
                rm_dest_rates[dest] = dest_throughputs[dest]
            else:
                rm_dest_rates[dest] = testPattData[f"{dest}_RATE_Hz"]
        else:
            rm_dest_rates[dest] = testPattData[f"{dest}_RATE_Hz"]

    print(rm_dest_time_sources)

    RateMeasures = rm_checks.init_rms(rm_dest_time_sources, args.SYS, args.UNIT)

    print(bpm_dict)
    epics_ctrls.break_point("Starting BSA Buffers")
    # start the aquisitions
    for dest_num in testPattOrg["Data"]:
        if testPattOrg["Data"][dest_num]["Active"]:
            testPattOrg["Data"][dest_num]["Buffer"].start()

    print(f"slow_dest_trigs {slow_dest_trigs}")
    ctrl_rates = helpers.to_str_dests(slow_dest_trigs)
    print(f"ctrl_rates {ctrl_rates}")

    # Check the Rate Measures, these should finish before the bsa
    test_patt_report["Checks"]["rate_measures"], rm_info = rm_checks.check_all(
        RateMeasures, rm_dest_rates, rm_dest_time_sources, ctrl_rates
    )
    print(rm_info)
    epics_ctrls.break_point(
        f'rate measure check passed: {test_patt_report["Checks"]["rate_measures"]}'
    )

    # initialize bsa_ready flag to False before starting tests
    bsa_ready = False

    buffer_start = time.time()

    # Check each aquisition if it is ready, sleep for one second if not all aquisitions are ready
    # Don't need too wait for each to be ready, possible improvement in future if need speed
    while bsa_ready == False:
        time.sleep(1)
        bsa_ready = True

        for dest_num in testPattOrg["Data"]:
            if testPattOrg["Data"][dest_num]["Active"]:
                if (
                    caget(
                        f'BSA:{args.SYS}:{testPattOrg["Data"][dest_num]["Buffer"].number}:HST_READY'
                    )
                    == 0
                ):
                    bsa_ready = False
                    test_patt_report["Checks"]["bsa_buff_complete"] = True
                    break

        print(args.bsa_timeout)
        if time.time() - buffer_start >= int(args.bsa_timeout):
            bsa_ready = True
            test_patt_report["Checks"]["bsa_buff_complete"] = False

        epics_ctrls.break_point("Waiting for aquisitions to complete")

    # TODO: check that CNT pv is greater than zero.  If it is zero fail the patt

    monitor_bsss_clear(bpm_dict, suffix="TMIT")
    monitor_bsss_clear(bpm_dict, suffix="TMITPID")

    bpm_dict, is_tmit_pid_hst_available = check_bpm.get_bpm_bsa_data(
        bpm_dict, "TMITPIDHST"
    )

    bpm_dict, is_tmit_hst_available = check_bpm.get_bpm_bsa_data(bpm_dict, "TMITHST")

    bpm_dict = check_bpm.get_tpg_bsa_data(bpm_dict, TPG_BASE_PV, "PIDLHST")

    bpm_dict = check_bpm.get_tpg_bsa_data(bpm_dict, TPG_BASE_PV, "BEAMREQHST")

    if is_tmit_pid_hst_available:
        bpm_dict = check_bpm.get_spacing_count(bpm_dict, "TMITPIDHST")

    bpm_dict = check_bpm.get_spacing_count(bpm_dict, "PIDLHST")
    if is_bsss_tmit_pid_available:
        bpm_dict = check_bpm.get_spacing_count(bpm_dict, "BSSS_TMITPID")

    bpm_dict = check_bpm.get_count(bpm_dict, "BEAMREQHST")

    if is_tmit_hst_available:
        check_tmit_output = check_bpm.check_tmit(bpm_dict)
        bpm_dict = check_tmit_output[0]
        test_patt_report["Checks"]["has_good_tmit"] = check_tmit_output[1][0]
        print(f"has good tmit: {test_patt_report['Checks']['has_good_tmit']}")
    epics_ctrls.break_point(f"test_patt_report: {test_patt_report}")
    if (
        is_tmit_hst_available
        and is_tmit_pid_hst_available
        and is_bsss_tmit_pid_available
        and is_bsss_tmit_available
    ):
        bsss_bsa_result = match_bsss_and_bsa(bpm_dict)
        bpm_dict = bsss_bsa_result[0]
        test_patt_report["Checks"]["all_bsss_pid_in_bsa"] = bsss_bsa_result[1][0]
        test_patt_report["Checks"]["bsss_tmit_match_bsa_on_pid"] = bsss_bsa_result[1][1]

    print(f"\n****************************")
    print(f"Pattern Name: {test_patt_name}")
    print(f"Time: {run_time}")
    print("---------------")

    epics_ctrls.break_point("writing bpm info")
    bpm_dict = check_bpm.write_info(bpm_dict)
    epics_ctrls.break_point("bpm info written")
    bpm_info = check_bpm.get_info(bpm_dict)

    print(bpm_info)

    epics_ctrls.break_point("releasing buffers")
    # Release the buffers in use
    for dest_num in testPattOrg["Data"]:
        if not testPattOrg["Data"][dest_num]["Active"]:
            continue
        testPattOrg["Data"][dest_num]["Buffer"].release()

    for dest_num in testPattOrg["Data"]:
        if not testPattOrg["Data"][dest_num]["Active"]:
            continue
        del testPattOrg["Data"][dest_num]["Buffer"]

    # Save data

    epics_ctrls.break_point(f"saving data to: {data_file}")
    save_data(testPattOrg, "testPattOrg")
    save_data(bpm_dict, "bpm_dict")
    save_data(rm_info, "rm_info")

    rm_info = helpers.convert_numpy_types(rm_info)

    try:
        json_rm_info = json.dumps(rm_info, indent=4)
    except:
        print(f"undable to convert rm_info to json")

    # Save analysis of data
    epics_ctrls.break_point(f"saving info to: {get_info_file(run_date)}")
    try:
        with open(get_info_file(run_date), "a") as info_file:
            info_file.write(f"\n****************************")
            info_file.write(f"\nPattern Name: {test_patt_name}")
            info_file.write(f"\nTime: {run_time}")

            info_file.write(bpm_info)
            info_file.write("Rate Measure info:\n")
            info_file.write(json_rm_info)
    except:
        epics_ctrls.break_point("unable to save info")

    save_report(test_patt_report, run_date)

    make_file(get_passed_file(run_date))
    make_file(get_failed_file(run_date))

    all_checks_passed = True
    for check in test_patt_report["Checks"]:
        if not test_patt_report["Checks"][check]:
            all_checks_passed = False

    if all_checks_passed:
        result_file = get_passed_file(run_date)
    else:
        result_file = get_failed_file(run_date)

    try:
        with open(result_file, "a") as results:
            results.write(f"{test_patt_name}\n")
    except:
        epics_ctrls.break_point("unable to write to pass/fail file")

    total_time = time.time() - start
    epics_ctrls.break_point(
        f"End of Pattern Verification.  Time to complete: {total_time} seconds"
    )
