#!/usr/bin/env python3

import sphinxify
from cxxheaderparser.simple import parse_string
from cxxheaderparser.tokfmt import tokfmt

if __name__ == "__main__":
    with open("ntcore/include/ntcore_cpp.h") as fp:
        data = parse_string(fp.read())

    for c in data.namespace.namespaces["nt"].classes:
        if str(c.class_decl.typename) == "struct PubSubOptions":

            params = []
            docs = []

            for f in c.fields:
                if f.static or f.name == "structSize":
                    continue

                if str(f.type) == "NT_Publisher":
                    params.append(
                        (
                            "std::optional<std::shared_ptr<nt::Publisher>>",
                            f.name,
                            f"{f.name}.has_value() ? {f.name}.value()->GetHandle() : {f.value}",
                            "std::nullopt",
                        )
                    )
                else:
                    v = str(f.value)
                    if v == "kDefaultPeriodic":
                        v = f"nt::PubSubOptions::{v}"
                    params.append((f.type, f.name, f.name, v))

                if f.doxygen:
                    docs.append(f"@param {f.name} {f.doxygen}")

            paramstr = ",\n        ".join(f"{t} {n}" for t, n, _, _ in params)
            args = ",\n        ".join(f'py::arg("{n}") = {v}' for _, n, _, v in params)
            options = ",\n          ".join(f".{fn} = {n}" for _, fn, n, _ in params)

            doc = "\n            ".join(
                sphinxify.process_raw("\n".join(docs)).splitlines()
            )

            print(
                f"""
      // autogenerated by gen-pubsub.py
      .def(py::init([](
        {paramstr}
      ) -> nt::PubSubOptions {{
        return nt::PubSubOptions{{
          {options}
        }};
      }}),
        py::kw_only(),
        {args},
        R"(
            {doc}
        )"
      )
        
        """
            )
