# -*- coding: utf-8 -*-
"""
A subroutine to add IR block.
Requires the params structure as input.

Need to think of the reequired output (pulse sequence variable itself?)

@author: petrm
"""

#imports
from math import pi
import numpy as np

from LF_scanner.pypulseq.make_sinc_pulse import make_sinc_pulse
from LF_scanner.pypulseq.make_delay import make_delay


def IR_block(params, scanner_parameters):
    #function creates inversion recovery block with delay
    #params['IR_time'] = 0.140  # STIR # TODO add to GUI
    #params['IR_time'] = 2.250  # FLAIR # TODO add to GUI
    flip_ir = round(180 * pi / 180) # TODO add to GUI
    rf_ir, gz_ir, _ = make_sinc_pulse(flip_angle=flip_ir, system=scanner_parameters, duration=params['t_ref'],
                                      slice_thickness=params['sl_thkn'], apodization=0.5,
                                      time_bw_product=round(params['t_BW_product_ref'], 8), phase_offset=90 * pi / 180,
                                      return_gz=True)
    delay_IR = np.ceil(params['TI'] / scanner_parameters.grad_raster_time) * scanner_parameters.grad_raster_time
    delay_IR = make_delay(delay_IR)

    return rf_ir, gz_ir, delay_IR

