#----------------------------------------
#--------- Resnet Imports ---------------
#----------------------------------------
from resnets.cifar_resnet import ResNet, BasicBlock, Bottleneck

def _resnet(arch, block, layers, **kwargs):
    """
    This is a good level for unpacking keworded arguments
    """
    config = kwargs['config']

    # Number of classes for the classifier layer
    num_classes = config.MAIN.NUM_CLASS if ('MAIN' in config and 'NUM_CLASS' in config.MAIN) else 100
    training_strategy = config.TRAINING_STRATEGY if 'TRAINING_STRATEGY' in config else 'standard'
    num_additional_heads = config.NUM_ADDITIONAL_HEADS if 'NUM_ADDITIONAL_HEADS' in config else 1
    additional_mask_functions = config.ADDITIONAL_MASK_FUNCTIONS if 'ADDITIONAL_MASK_FUNCTIONS' in config else None

    model = ResNet(block, layers, num_classes=num_classes, training_strategy=training_strategy, num_additional_heads=num_additional_heads, additional_mask_functions=additional_mask_functions)
    return model


def cifar_resnet18(**kwargs):
    r"""ResNet-18 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], **kwargs)


def cifar_resnet34(**kwargs):
    r"""ResNet-34 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    return _resnet('resnet34', BasicBlock, [3, 4, 6, 3], **kwargs)

def cifar_resnet24(**kwargs):
    """
    Special network to experiment with knowledge distillation ideas
    """

    return _resnet('resnet24', Bottleneck, [2,2,2,2], **kwargs)

def cifar_resnet44(**kwargs):
    """
    Special network to experiment with knowledge distillation ideas
    """

    return _resnet('resnet44', Bottleneck, [3,4,4,3], **kwargs)

def cifar_resnet47(**kwargs):
    """
    Special network to experiment with knowledge distillation ideas
    """

    return _resnet('resnet47', Bottleneck, [3,4,5,3], **kwargs)

def cifar_resnet50(**kwargs):
    r"""ResNet-50 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    return _resnet('resnet50', Bottleneck, [3, 4, 6, 3], **kwargs)

def cifar_resnet53(**kwargs):
    """
    Special network to experiment with knowledge distillation ideas
    """

    return _resnet('resnet53', Bottleneck, [3,4,7,3], **kwargs)

def cifar_resnet56(**kwargs):
    """
    Special network to experiment with knowledge distillation ideas
    """

    return _resnet('resnet54', Bottleneck, [3,4,8,3], **kwargs)

def cifar_resnet59(**kwargs):
    """
    Special network to experiment with knowledge distillation ideas
    """

    return _resnet('resnet56', Bottleneck, [3,4,9,3], **kwargs)

def cifar_resnet101(**kwargs):
    r"""ResNet-101 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    return _resnet('resnet101', Bottleneck, [3, 4, 23, 3], **kwargs)


def cifar_resnet152(**kwargs):
    r"""ResNet-152 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    return _resnet('resnet152', Bottleneck, [3, 8, 36, 3], **kwargs)


def cifar_resnext50_32x4d(**kwargs):
    r"""ResNeXt-50 32x4d model from
    `"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    kwargs['groups'] = 32
    kwargs['width_per_group'] = 4
    return _resnet('resnext50_32x4d', Bottleneck, [3, 4, 6, 3], **kwargs)


def cifar_resnext101_32x8d(**kwargs):
    r"""ResNeXt-101 32x8d model from
    `"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    kwargs['groups'] = 32
    kwargs['width_per_group'] = 8
    return _resnet('resnext101_32x8d', Bottleneck, [3, 4, 23, 3], **kwargs)


def cifar_wide_resnet50_2(**kwargs):
    r"""Wide ResNet-50-2 model from
    `"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_
    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
    channels, and in Wide ResNet-50-2 has 2048-1024-2048.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    kwargs['width_per_group'] = 64 * 2
    return _resnet('wide_resnet50_2', Bottleneck, [3, 4, 6, 3], **kwargs)


def cifar_wide_resnet101_2(**kwargs):
    r"""Wide ResNet-101-2 model from
    `"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_
    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
    channels, and in Wide ResNet-50-2 has 2048-1024-2048.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    kwargs['width_per_group'] = 64 * 2
    return _resnet('wide_resnet101_2', Bottleneck, [3, 4, 23, 3], **kwargs)
