import unittest
from parameterized import parameterized
from iqrfpy.enums.commands import CoordinatorResponseCommands
from iqrfpy.enums.message_types import CoordinatorMessages
from iqrfpy.enums.peripherals import EmbedPeripherals
from iqrfpy.peripherals.coordinator.responses.set_hops import SetHopsResponse
from tests.helpers.json import generate_json_response

data_ok: dict = {
    'mtype': CoordinatorMessages.SET_HOPS,
    'msgid': 'setHopsTest',
    'nadr': 0,
    'hwpid': 0,
    'rcode': 0,
    'dpa_value': 64,
    'result': {
        'requestHops': 255,
        'responseHops': 255,
    },
    'dpa': b'\x00\x00\x00\x89\x00\x00\x00\x40\xff\xff'
}

data_ok_1: dict = {
    'mtype': CoordinatorMessages.SET_HOPS,
    'msgid': 'setHopsTest',
    'nadr': 0,
    'hwpid': 1026,
    'rcode': 0,
    'dpa_value': 35,
    'result': {
        'requestHops': 0,
        'responseHops': 239,
    },
    'dpa': b'\x00\x00\x00\x89\x02\x04\x00\x23\x00\xef'
}

data_error: dict = {
    'mtype': CoordinatorMessages.SET_HOPS,
    'msgid': 'setHopsTest',
    'nadr': 0,
    'hwpid': 1028,
    'rcode': 1,
    'dpa_value': 35,
    'dpa': b'\x00\x00\x00\x89\x04\x04\x01\x23'
}


class SetHopsResponseTestCase(unittest.TestCase):

    @parameterized.expand([
        ['from_dpa', data_ok, SetHopsResponse.from_dpa(data_ok['dpa']), False],
        ['from_dpa', data_ok_1, SetHopsResponse.from_dpa(data_ok_1['dpa']), False],
        ['from_json', data_ok, SetHopsResponse.from_json(generate_json_response(data_ok)), True],
        ['from_json', data_ok_1, SetHopsResponse.from_json(generate_json_response(data_ok_1)), True],
        ['from_dpa_error', data_error, SetHopsResponse.from_dpa(data_error['dpa']), False],
        ['from_json_error', data_error, SetHopsResponse.from_json(generate_json_response(data_error)), True]
    ])
    def test_factory_methods_ok(self, _, response_data, response, json):
        with self.subTest():
            self.assertEqual(response.nadr, response_data['nadr'])
        with self.subTest():
            self.assertEqual(response.pnum, EmbedPeripherals.COORDINATOR)
        with self.subTest():
            self.assertEqual(response.pcmd, CoordinatorResponseCommands.SET_HOPS)
        with self.subTest():
            self.assertEqual(response.hwpid, response_data['hwpid'])
        with self.subTest():
            self.assertEqual(response.rcode, response_data['rcode'])
        if json:
            with self.subTest():
                self.assertEqual(response.mtype, CoordinatorMessages.SET_HOPS)
            with self.subTest():
                self.assertEqual(response.msgid, response_data['msgid'])

    def test_from_dpa_invalid(self):
        with self.assertRaises(ValueError):
            SetHopsResponse.from_dpa(b'\x00\x00\x00\x89\x00\x00\x00\x40')

    @parameterized.expand([
        ['from_dpa', data_ok['result']['requestHops'], SetHopsResponse.from_dpa(data_ok['dpa'])],
        ['from_dpa', data_ok_1['result']['requestHops'], SetHopsResponse.from_dpa(data_ok_1['dpa'])],
        ['from_json', data_ok['result']['requestHops'], SetHopsResponse.from_json(generate_json_response(data_ok))],
        ['from_json', data_ok_1['result']['requestHops'], SetHopsResponse.from_json(generate_json_response(data_ok_1))]
    ])
    def test_get_request_hops(self, _, request_hops, response: SetHopsResponse):
        self.assertEqual(response.request_hops, request_hops)

    @parameterized.expand([
        ['from_dpa_error', SetHopsResponse.from_dpa(data_error['dpa'])],
        ['from_json_error', SetHopsResponse.from_json(generate_json_response(data_error))]
    ])
    def test_get_request_hops_error(self, _, response: SetHopsResponse):
        self.assertIsNone(response.request_hops)

    @parameterized.expand([
        ['from_dpa', data_ok['result']['responseHops'], SetHopsResponse.from_dpa(data_ok['dpa'])],
        ['from_dpa', data_ok_1['result']['responseHops'], SetHopsResponse.from_dpa(data_ok_1['dpa'])],
        ['from_json', data_ok['result']['responseHops'], SetHopsResponse.from_json(generate_json_response(data_ok))],
        ['from_json', data_ok_1['result']['responseHops'], SetHopsResponse.from_json(generate_json_response(data_ok_1))]
    ])
    def test_get_response_hops(self, _, response_hops, response: SetHopsResponse):
        self.assertEqual(response.response_hops, response_hops)

    @parameterized.expand([
        ['from_dpa_error', SetHopsResponse.from_dpa(data_error['dpa'])],
        ['from_json_error', SetHopsResponse.from_json(generate_json_response(data_error))]
    ])
    def test_get_response_hops_error(self, _, response: SetHopsResponse):
        self.assertIsNone(response.response_hops)
