import keras
import pytest

from src.model_modules.advanced_paddings import *


class TestPadUtils:

    def test_get_padding_for_same_negative_kernel_size(self):
        print('In test_get_padding_for_same_negative_kernel_size')
        with pytest.raises(ValueError) as einfo:
            PadUtils.get_padding_for_same((-1, 2))
        assert 'All values of kernel_size must be > 0. Got: (-1, 2) ' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.get_padding_for_same((1, -2))
        assert 'All values of kernel_size must be > 0. Got: (1, -2) ' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.get_padding_for_same((-1, -2))
        assert 'All values of kernel_size must be > 0. Got: (-1, -2) ' in str(einfo.value)

    def test_get_padding_for_same_strides_greater_one(self):
        with pytest.raises(NotImplementedError) as einfo:
            PadUtils.get_padding_for_same((1, 1), strides=2)
        assert 'Strides other than 1 not implemented!' in str(einfo.value)

        with pytest.raises(NotImplementedError) as einfo:
            PadUtils.get_padding_for_same((1, 1), strides=-1)
        assert 'Strides other than 1 not implemented!' in str(einfo.value)

    def test_get_padding_for_same_non_int_kernel(self):
        with pytest.raises(ValueError) as einfo:
            PadUtils.get_padding_for_same((1., 1))
        assert "The `kernel_size` argument must have a tuple of integers. Got: (1.0, 1) " \
               "of type [<class 'float'>, <class 'int'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.get_padding_for_same((1, 1.))
        assert "The `kernel_size` argument must have a tuple of integers. Got: (1, 1.0) " \
               "of type [<class 'int'>, <class 'float'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.get_padding_for_same((1, '1.'))
        assert "The `kernel_size` argument must have a tuple of integers. Got: (1, '1.') " \
               "of type [<class 'int'>, <class 'str'>]" in str(einfo.value)

    def test_get_padding_for_same_stride_3d(self):
        kernel = (3, 3, 3)
        pad = PadUtils.get_padding_for_same(kernel)
        assert pad == (1, 1, 1)
        assert isinstance(pad, tuple)
        assert isinstance(pad[0], int) and isinstance(pad[1], int)
        assert not (isinstance(pad[0], np.int64) and isinstance(pad[1], np.int64) and isinstance(pad[2], np.int64))

    def test_get_padding_for_same_even_pad(self):
        with pytest.raises(NotImplementedError) as einfo:
            PadUtils.get_padding_for_same((2, 1))
        assert 'even kernel size not implemented. Got (2, 1)' in str(einfo.value)

        with pytest.raises(NotImplementedError) as einfo:
            PadUtils.get_padding_for_same((1, 4))
        assert 'even kernel size not implemented. Got (1, 4)' in str(einfo.value)

        with pytest.raises(NotImplementedError) as einfo:
            PadUtils.get_padding_for_same((2, 4))
        assert 'even kernel size not implemented. Got (2, 4)' in str(einfo.value)

    ##################################################################################

    def test_check_padding_format_negative_pads(self):

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((-2, 1))
        assert "The `1st entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((1, -1))
        assert "The `2nd entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((-2, -1))
        assert "The `1st entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str(
            einfo.value)

    def test_check_padding_format_len_of_pad_tuple(self):
        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((1, 1, 2))
        assert "`padding` should have two elements. Found: (1, 1, 2)" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((1, 1, 2, 2))
        assert "`padding` should have two elements. Found: (1, 1, 2, 2)" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1, 1, 3), (2, 2, 4)))
        assert "`padding[0]` should have one or two elements. Found: (1, 1, 3)" in str(einfo.value)

        assert PadUtils.check_padding_format(((1, 1), (2, 2))) == ((1, 1), (2, 2))
        assert PadUtils.check_padding_format((1, 2)) == ((1, 1), (2, 2))
        assert PadUtils.check_padding_format(1) == ((1, 1), (1, 1))

    def test_check_padding_format_tupe_of_none_integer(self):
        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((1.2, 1))
        assert "The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.2" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((1, 1.))
        assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 1.0" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(1.2)
        assert "`padding` should be either an int, a tuple of 2 ints (symmetric_height_pad, symmetric_width_pad), " \
               "or a tuple of 2 tuples of 2 ints ((top_pad, bottom_pad), (left_pad, right_pad)). Found: 1.2 of type " \
               "<class 'float'>" in str(einfo.value)

    def test_check_padding_format_tuple_of_tuple_none_integer_first(self):
        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1., 2), (3, 4)))
        assert "`padding[0]` should have one or two elements of type int. Found:(1.0, 2) " \
               "of type [<class 'float'>, <class 'int'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1, 2.), (3, 4)))
        assert "`padding[0]` should have one or two elements of type int. Found:(1, 2.0) " \
               "of type [<class 'int'>, <class 'float'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1, '2'), (3, 4)))
        assert "`padding[0]` should have one or two elements of type int. Found:(1, '2') " \
               "of type [<class 'int'>, <class 'str'>]" in str(einfo.value)

    def test_check_padding_format_tuple_of_tuple_none_integer_second(self):
        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1, 2), (3., 4)))
        assert "`padding[1]` should have one or two elements of type int. Found:(3.0, 4) " \
               "of type [<class 'float'>, <class 'int'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1, 2), (3, 4.)))
        assert "`padding[1]` should have one or two elements of type int. Found:(3, 4.0) " \
               "of type [<class 'int'>, <class 'float'>]" in str(einfo.value)

    def test_check_padding_format_valid_mix_of_int_and_tuple(self):
        assert PadUtils.check_padding_format(((1, 2), 3)) == ((1, 2), (3, 3))
        assert PadUtils.check_padding_format((1, (2, 3))) == ((1, 1), (2, 3))

    def test_check_padding_format_invalid_mixed_tuple_and_int(self):
        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1., 2), 3))
        assert "`padding[0]` should have one or two elements of type int. Found:(1.0, 2) " \
               "of type [<class 'float'>, <class 'int'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format(((1, 2), 3.))
        assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 3.0" in str(einfo.value)

    def test_check_padding_format_invalid_mixed_int_and_tuple(self):
        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((1., (2, 3)))
        assert "The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            PadUtils.check_padding_format((1, (2., 3)))
        assert "`padding[1]` should have one or two elements of type int. Found:(2.0, 3) " \
               "of type [<class 'float'>, <class 'int'>]" in str(einfo.value)


class TestReflectionPadding2D:

    @pytest.fixture
    def input_x(self):
        return keras.Input(shape=(10, 10, 3))

    def test_init_tuple_of_valid_int(self):
        pad = (1, 3)
        layer_name = "RefPAD"
        ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)
        assert ref_pad.padding == ((1, 1), (3, 3))
        assert ref_pad.name == 'RefPAD'
        assert ref_pad.data_format == 'channels_last'
        assert ref_pad.rank == 2

        pad = (0, 1)
        ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)
        assert ref_pad.padding == ((0, 0), (1, 1))
        assert ref_pad.name == 'RefPAD'
        assert ref_pad.data_format == 'channels_last'
        assert ref_pad.rank == 2

        pad = (5, 3)
        layer_name = "RefPAD_5x3"
        ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)
        assert ref_pad.padding == ((5, 5), (3, 3))

    def test_init_tuple_of_negative_int(self):
        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(-1, 1))
        assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(1, -2))
        assert "The `2nd entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(-1, -2))
        assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(einfo.value)

    def test_init_tuple_of_invalid_format_float(self):
        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(1., 1))
        assert 'The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(1, 1.2))
        assert 'The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 1.2' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(1., 1.2))
        assert 'The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0' in str(einfo.value)

    def test_init_tuple_of_invalid_format_string(self):
        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=('1', 2))
        # This error message is not the best as it is missing the type information.
        # But it is raised by keras.utils.conv_utils which I will not touch.
        assert "`padding[0]` is str but must be int" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(1, '2'))
        assert '`padding[1]` is str but must be int' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=('1', '2'))
        assert '`padding[0]` is str but must be int' in str(einfo.value)

    def test_init_int(self):
        layer_name = "RefPAD"
        ref_pad = ReflectionPadding2D(padding=1, name=layer_name)
        assert ref_pad.padding == ((1, 1), (1, 1))
        assert ref_pad.name == "RefPAD"

    def test_init_tuple_of_tuple_of_valid_int(self):
        ref_pad = ReflectionPadding2D(padding=((0, 1), (2, 3)), name="RefPAD")
        assert ref_pad.padding == ((0, 1), (2, 3))
        assert ref_pad.name == "RefPAD"

    def test_init_tuple_of_tuple_of_invalid_int(self):
        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=((-4, 1), (2, 3)), name="RefPAD")
        assert "The `1st entry of padding` argument must be >= 0. Received: (-4, 1) of type <class 'tuple'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=((4, -1), (2, 3)), name="RefPAD")
        assert "The `1st entry of padding` argument must be >= 0. Received: (4, -1) of type <class 'tuple'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=((4, 1), (-2, 3)), name="RefPAD")
        assert "The `2nd entry of padding` argument must be >= 0. Received: (-2, 3) of type <class 'tuple'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=((4, 1), (2, -3)), name="RefPAD")
        assert "The `2nd entry of padding` argument must be >= 0. Received: (2, -3) of type <class 'tuple'>" in str(
            einfo.value)

    def test_init_tuple_of_tuple_of_invalid_format(self):
        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=((0.1, 1), (2, 3)), name="RefPAD")
        assert "`padding[0]` should have one or two elements of type int. Found:(0.1, 1) " \
               "of type [<class 'float'>, <class 'int'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=(1, 2.2))
        assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 2.2" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            ReflectionPadding2D(padding=((0, 1), ('2', 3)), name="RefPAD")
        assert "`padding[1]` should have one or two elements of type int. Found:('2', 3) " \
               "of type [<class 'str'>, <class 'int'>]" in str(einfo.value)

    def test_call(self, input_x):
        # here it behaves like a "normal" keras layer, I don't know how to test those
        pad = (1, 0)
        layer_name = "RefPad_3x1"
        ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)(input_x)
        assert ref_pad.get_shape().as_list() == [None, 12, 10, 3]
        assert ref_pad.name == 'RefPad_3x1/MirrorPad:0'


class TestSymmerticPadding2D:

    @pytest.fixture
    def input_x(self):
        return keras.Input(shape=(10, 10, 3))

    def test_init_tuple_of_valid_int(self):
        pad = (1, 3)
        layer_name = "SymPad"
        sym_pad = SymmetricPadding2D(padding=pad, name=layer_name)
        assert sym_pad.padding == ((1, 1), (3, 3))
        assert sym_pad.name == 'SymPad'
        assert sym_pad.data_format == 'channels_last'
        assert sym_pad.rank == 2

        pad = (0, 1)
        sym_pad = SymmetricPadding2D(padding=pad, name=layer_name)
        assert sym_pad.padding == ((0, 0), (1, 1))
        assert sym_pad.name == 'SymPad'
        assert sym_pad.data_format == 'channels_last'
        assert sym_pad.rank == 2

        pad = (5, 3)
        layer_name = "SymPad_5x3"
        sym_pad = SymmetricPadding2D(padding=pad, name=layer_name)
        assert sym_pad.padding == ((5, 5), (3, 3))

    def test_init_tuple_of_negative_int(self):
        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(-1, 1))
        assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(1, -2))
        assert "The `2nd entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(-1, -2))
        assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(
            einfo.value)

    def test_init_tuple_of_invalid_format_float(self):
        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(1., 1))
        assert 'The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(1, 1.2))
        assert 'The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 1.2' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(1., 1.2))
        assert 'The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0' in str(einfo.value)

    def test_init_tuple_of_invalid_format_string(self):
        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=('1', 2))
        # This error message is not the best as it is missing the type information.
        # But it is raised by keras.utils.conv_utils which I will not touch.
        assert "`padding[0]` is str but must be int" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(1, '2'))
        assert '`padding[1]` is str but must be int' in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=('1', '2'))
        assert '`padding[0]` is str but must be int' in str(einfo.value)

    def test_init_int(self):
        layer_name = "SymPad"
        sym_pad = SymmetricPadding2D(padding=1, name=layer_name)
        assert sym_pad.padding == ((1, 1), (1, 1))
        assert sym_pad.name == "SymPad"

    def test_init_tuple_of_tuple_of_valid_int(self):
        sym_pad = SymmetricPadding2D(padding=((0, 1), (2, 3)), name="SymPad")
        assert sym_pad.padding == ((0, 1), (2, 3))
        assert sym_pad.name == "SymPad"

    def test_init_tuple_of_tuple_of_invalid_int(self):
        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=((-4, 1), (2, 3)), name="SymPad")
        assert "The `1st entry of padding` argument must be >= 0. Received: (-4, 1) of type <class 'tuple'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=((4, -1), (2, 3)), name="SymPad")
        assert "The `1st entry of padding` argument must be >= 0. Received: (4, -1) of type <class 'tuple'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=((4, 1), (-2, 3)), name="SymPad")
        assert "The `2nd entry of padding` argument must be >= 0. Received: (-2, 3) of type <class 'tuple'>" in str(
            einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=((4, 1), (2, -3)), name="SymPad")
        assert "The `2nd entry of padding` argument must be >= 0. Received: (2, -3) of type <class 'tuple'>" in str(
            einfo.value)

    def test_init_tuple_of_tuple_of_invalid_format(self):
        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=((0.1, 1), (2, 3)), name="SymPad")
        assert "`padding[0]` should have one or two elements of type int. Found:(0.1, 1) " \
               "of type [<class 'float'>, <class 'int'>]" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=(1, 2.2))
        assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 2.2" in str(einfo.value)

        with pytest.raises(ValueError) as einfo:
            SymmetricPadding2D(padding=((0, 1), ('2', 3)), name="SymPad")
        assert "`padding[1]` should have one or two elements of type int. Found:('2', 3) " \
               "of type [<class 'str'>, <class 'int'>]" in str(einfo.value)

    def test_call(self, input_x):
        # here it behaves like a "normal" keras layer, I don't know how to test those
        pad = (1, 0)
        layer_name = "SymPad_3x1"
        sym_pad = SymmetricPadding2D(padding=pad, name=layer_name)(input_x)
        assert sym_pad.get_shape().as_list() == [None, 12, 10, 3]
        assert sym_pad.name == 'SymPad_3x1/MirrorPad:0'