from ..generated.alpinebits import OtaPingRq, OtaPingRs
from xsdata_pydantic.bindings import XmlParser


def main():
    # test parsing a ping request sample

    path = (
        "AlpineBits-HotelData-2024-10/files/samples/Handshake/Handshake-OTA_PingRS.xml"
    )

    with open(path, "r", encoding="utf-8") as f:
        xml = f.read()

    # Parse the XML into the request object

    # Test parsing back

    parser = XmlParser()

    parsed_result = parser.from_string(xml, OtaPingRs)

    print(parsed_result.echo_data)

    warning = parsed_result.warnings.warning[0]

    print(warning.type_value)

    print(type(warning.content))

    print(warning.content[0])

    # save json in echo_data to file with indents
    output_path = "echo_data_response.json"
    with open(output_path, "w", encoding="utf-8") as out_f:
        import json

        json.dump(json.loads(parsed_result.echo_data), out_f, indent=4)
    print(f"Saved echo_data json to {output_path}")


if __name__ == "__main__":
    main()
