import torch


def if_def_when(lossyear, year, cutoffs=[2, 5, 8]):
    """
    Creates categorical variables for deforestration event given cutoffs.
    Values in cutoffs define the time bins
    Returns len(cutoffs) + 1 categorical layers:
    Example: cutoffs = [2,5,8], num of layers = 4 , considered year = year
    Categories:
    0) if year - lossyear is in [0,2)
    1) if year - lossyear is in [2,5)
    2) if year - lossyear is in [5,8)
    3) 8 years ago or more
    No prior knowledge:
        if loss event is in year > considered year or pixel is non deforested up to 2018+, all categories have value 0
    """
    cutoffs.append(year)
    cutoffs.insert(0, 0)
    lossyear[(lossyear > year)] = 0
    losses = []
    for idx in range(0, len(cutoffs) - 1):
        deff = torch.zeros(lossyear.size())
        deff[
            (cutoffs[idx] <= (year - lossyear)) & ((year - lossyear) < cutoffs[idx + 1])
        ] = 1
        losses.append(deff.float())
    losses = torch.stack(losses)
    # Return Nan values encoded as needed:
    losses[:, (lossyear == -1)] = -1
    return losses