import base64
import json
import unittest

from parameterized import parameterized

from lib.noindent import CustomEncoder
from lib.rawdata import character, foliage_model_instance, group


class TestRawData(unittest.TestCase):
    @parameterized.expand(
        [
            (
                "player data",
                "[encoded data, 5784 characters]",
            ),
            (
                "character data",
                "[encoded data, 7024 characters]",
            ),
        ]
    )
    def test_character(self, name, test_base64):
        test_data = base64.b64decode(test_base64)
        properties = character.decode_bytes(test_data)
        json_str = json.dumps(
            properties, cls=CustomEncoder, ensure_ascii=False, indent=2
        )
        reparsed_properties = json.loads(json_str)
        reconverted_data = character.encode_bytes(reparsed_properties)
        self.assertEqual(test_data, reconverted_data)

    @parameterized.expand(
        [
            ("EPalGroupType::Neutral", "VjIcY1hKCdXAmtWebNlJCwAAAAAAAAAA"),
            (
                "EPalGroupType::Guild",
                "[encoded data, 31880 characters]",
            ),
            (
                "EPalGroupType::Organization",
                "hJYrguAqq0C1R1LYdTOU+AAAAAAhAAAAAAAAAAAAAAAAAAAAAQAAAJP/oPkwde5Mu9LEZz4Ao7kAAAAAAAAAAAAAAAABAAAAS9gbh2lg/EK4JaUv6ZLvpAAAAAAAAAAAAAAAAAEAAABClNaHEgrLQbry8En6Ps5CAAAAAAAAAAAAAAAAAQAAAGi+Y6rkWoJNvNYiWy0wP2oAAAAAAAAAAAAAAAABAAAAyavtNRisZ0GMrffZPRwUUQAAAAAAAAAAAAAAAAEAAABaFkBiBdzaQI561vQWfEVlAAAAAAAAAAAAAAAAAQAAAEHsz/5oAthAqfTDrSU0iTQAAAAAAAAAAAAAAAABAAAAhpYm+oXOHEe0xluVcJae4QAAAAAAAAAAAAAAAAEAAABvbkhSUW08RrJ+2zUmlQosAAAAAAAAAAAAAAAAAQAAADHIBCjyiV5MvbwVQ4WutD8AAAAAAAAAAAAAAAABAAAAkCa0JufIAE6HDwfMXbIoXgAAAAAAAAAAAAAAAAEAAACSJCUqIPgzQ7C6QhHwFmDGAAAAAAAAAAAAAAAAAQAAANITOu234UJAtzHuFdmIf6oAAAAAAAAAAAAAAAABAAAAGf7viWmjZUaZ1YDZY68vpQAAAAAAAAAAAAAAAAEAAADzLXMPNgZxRaZEKrJAsFDRAAAAAAAAAAAAAAAAAQAAACTLT6S0hXBMiWhNCt1Q1CYAAAAAAAAAAAAAAAABAAAA8FIVKqLG+k6oDXo+08QXowAAAAAAAAAAAAAAAAEAAABtbXHJ1oETRaWNTBKI1YGvAAAAAAAAAAAAAAAAAQAAAMMbAwyhoONMqpUcq0MqPAkAAAAAAAAAAAAAAAABAAAAypjWd7uTjU6MdKuetLdZUAAAAAAAAAAAAAAAAAEAAAAyYKiVbDM+QaTY8Hv6ZObLAAAAAAAAAAAAAAAAAQAAAJAFrzxLoPFLl8DrAv8Gd3YAAAAAAAAAAAAAAAABAAAAKIACqxZ7p0GiR0TmrHacnwAAAAAAAAAAAAAAAAEAAADrw5+vNs5US7pOHBAvVR3tAAAAAAAAAAAAAAAAAQAAAF/uko9GJFFHqnPaQJbISvAAAAAAAAAAAAAAAAABAAAABUZ8Ixbmz0615pXXmYkR5AAAAAAAAAAAAAAAAAEAAACYDOqTTI28Qpw5lTLZSrBuAAAAAAAAAAAAAAAAAQAAAPLlWwh4i7JNn9QJ8BQlcS8AAAAAAAAAAAAAAAABAAAAHZMbTe8rTkeVXRN6twsB7QAAAAAAAAAAAAAAAAEAAAAwbMTEkrfWS4tBQ9T8pfmzAAAAAAAAAAAAAAAAAQAAANwynIWIZEpFsUrnDlenJaoAAAAAAAAAAAAAAAABAAAA38dfKnHdkk6/jK09vZCqywAAAAAAAAAAAAAAAAEAAACSTla8evFdSYUiwbuzgJrPAgAAAAA=",
            ),
        ]
    )
    def test_group(self, group_type, test_base64):
        test_data = base64.b64decode(test_base64)
        properties = group.decode_bytes(test_data, group_type)
        json_str = json.dumps(
            properties, cls=CustomEncoder, ensure_ascii=False, indent=2
        )
        reparsed_properties = json.loads(json_str)
        reconverted_data = group.encode_bytes(reparsed_properties)
        self.assertEqual(test_data, reconverted_data)

    @parameterized.expand(
        [
            ("MU3+sPafPE2sV9dUbyBGsgH7/gE2hQGr+lQAAABE7v+bJga7OQAAAIA/6AMAAA=="),
            ("iaUtjCpXgUK+rzl270FLJgABfjQAVAAAACny/wkpBqw5AKCANj/oAwAA"),
            ("LnyQHlPRt0apn4KAPLlMwwEvBgHzmgGK+lQAAAAlzv8IHwaTOwAAAIA/MgAAAA=="),
            ("hBhgfhCg8UObz9lwvgNdvwEgBgGa1wGr/FQAAABj6/ioDv+D+f9of4A/MgAAAA=="),
            ("DofBUL77wk2bQOkJ3Ayh/wABEXkATwAAAGfjOjleGHWqgD/oAwAA"),
        ]
    )
    def test_foliage_model_instance(self, test_base64):
        test_data = base64.b64decode(test_base64)
        properties = foliage_model_instance.decode_bytes(test_data)
        json_str = json.dumps(
            properties, cls=CustomEncoder, ensure_ascii=False, indent=2
        )
        reparsed_properties = json.loads(json_str)
        reconverted_data = foliage_model_instance.encode_bytes(reparsed_properties)
        self.assertEqual(test_data, reconverted_data)
