# -*- coding: utf-8 -*-
"""
A subroutine to calculate duration of any magnetisation preparation block.
Requires the params structure as input.

@author: petrm
"""
#imports
from MRI_sequences.pypulseq.utilities.magn_prep.FS_CHESS_block import FS_CHESS_block
from MRI_sequences.pypulseq.utilities.magn_prep.SPAIR_block import SPAIR_block
from MRI_sequences.pypulseq.utilities.magn_prep.IR_block import IR_block
from MRI_sequences.pypulseq.calc_duration import calc_duration


def magn_prep_duration(params, scanner_parameters, gz90):

    params['FS'] = False #TODO: add parameters to GUI
    params['SPAIR'] = False #TODO: add parameters to GUI
    params['IR'] = False #TODO: add parameters to GUI

    magn_prep_dur = 0

    if params['FS']:
        rf_fs, gx_fs, gy_fs = FS_CHESS_block(params, scanner_parameters, gz90)
        magn_prep_dur += calc_duration(gx_fs)
        return magn_prep_dur

    elif params['SPAIR']:
        rf_SPAIR, gx_SPAIR, gy_SPAIR, delay_IR = SPAIR_block(params, scanner_parameters, gz90)
        magn_prep_dur += calc_duration(gx_SPAIR)
        magn_prep_dur += calc_duration(delay_IR)
        return magn_prep_dur

    elif params['IR']:
        rf_ir, gz_ir, delay_IR = IR_block(params, scanner_parameters)
        magn_prep_dur += max(calc_duration(rf_ir), calc_duration(gz_ir))
        magn_prep_dur += calc_duration(delay_IR)
        return magn_prep_dur

    else:
        return magn_prep_dur

def magn_prep_add_block(params, scanner_parameters, gz90, seq):

    params['FS'] = False  # TODO: add parameters to GUI
    params['SPAIR'] = False  # TODO: add parameters to GUI
    params['IR'] = False  # TODO: add parameters to GUI

    if params['FS']:
        rf_fs, gx_fs, gy_fs = FS_CHESS_block(params, scanner_parameters, gz90)
        seq.add_block(gx_fs, gy_fs, rf_fs)
        return seq

    elif params['SPAIR']:
        rf_SPAIR, gx_SPAIR, gy_SPAIR, delay_IR = SPAIR_block(params, scanner_parameters, gz90)
        seq.add_block(gx_SPAIR, gy_SPAIR, rf_SPAIR)
        seq.add_block(delay_IR)
        return seq

    elif params['IR']:
        # TODO add correct offset from correct slice
        rf_ir, gz_ir, delay_IR = IR_block(params, scanner_parameters)
        seq.add_block(gz_ir, rf_ir)
        seq.add_block(delay_IR)
        return seq

    else:
        return seq
