from nautilus_trader.common.component import MessageBus
from nautilus_trader.common.component import LiveClock, TimeEvent
from nautilus_trader.model.identifiers import TraderId
from nautilus_trader.core.uuid import UUID4

from nautilus_trader.core import nautilus_pyo3  # noqa
from nautilus_trader.core.nautilus_pyo3 import HttpClient  # noqa
from nautilus_trader.core.nautilus_pyo3 import HttpMethod  # noqa
from nautilus_trader.core.nautilus_pyo3 import HttpResponse  # noqa

# from nautilus_trader.core.nautilus_pyo3 import MessageBus  # noqa
from nautilus_trader.core.nautilus_pyo3 import WebSocketClient  # noqa
from nautilus_trader.core.nautilus_pyo3 import WebSocketClientError  # noqa
from nautilus_trader.core.nautilus_pyo3 import WebSocketConfig  # noqa
from nautilus_trader.core.nautilus_pyo3 import (
    hmac_signature,  # noqa
    rsa_signature,  # noqa
    ed25519_signature,  # noqa
)
from nautilus_trader.common.component import Logger, set_logging_pyo3  # noqa


def setup_nautilus_core(
    trader_id: str,
    level_stdout: str,
    level_file: str | None = None,
    component_levels: dict[str, str] | None = None,
    directory: str | None = None,
    file_name: str | None = None,
    file_format: str | None = None,
    file_rotate: tuple[int, int] | None = None,
    is_colored: bool | None = None,
    is_bypassed: bool | None = None,
    print_config: bool | None = None,
    log_components_only: bool | None = None,
):
    """
    Setup logging for the application.
    """
    clock = LiveClock()
    msgbus = MessageBus(
        trader_id=TraderId(trader_id),
        clock=clock,
    )
    set_logging_pyo3(True)

    instance_id = nautilus_pyo3.UUID4().value
    log_guard = nautilus_pyo3.init_logging(
        trader_id=nautilus_pyo3.TraderId(trader_id),
        instance_id=nautilus_pyo3.UUID4.from_str(instance_id),
        level_stdout=nautilus_pyo3.LogLevel(level_stdout),
        level_file=nautilus_pyo3.LogLevel(level_file) if level_file else None,
        directory=directory,
        file_name=file_name,
        file_format=file_format,
        is_colored=is_colored,
        print_config=print_config,
        component_levels=component_levels,
        file_rotate=file_rotate,
        is_bypassed=is_bypassed,
        log_components_only=log_components_only,
    )

    return log_guard, msgbus, clock


def usage():
    import time

    print(UUID4().value)
    print(UUID4().value)
    print(UUID4().value)

    uuid_to_order_id = {}

    uuid = UUID4()

    order_id = "123456"

    uuid_to_order_id[uuid] = order_id

    print(uuid_to_order_id)

    clock = LiveClock()
    print(clock.timestamp())
    print(type(clock.timestamp_ms()))

    print(clock.utc_now().isoformat(timespec="milliseconds").replace("+00:00", "Z"))

    def handler1(msg):
        print(f"[{clock.timestamp_ns()}] Received message: {msg} - handler1")

    def handler2(msg):
        print(f"[{clock.timestamp_ns()}] Received message: {msg} - handler2")

    def handler3(msg):
        print(f"[{clock.timestamp_ns()}] Received message: {msg} - handler3")

    log_guard, msgbus, clock = setup_nautilus_core(
        trader_id="TESTER-001",
        level_stdout="DEBUG",
        component_levels={
            "logger1": "DEBUG",
            "logger2": "INFO",
        },
        log_components_only=True,
    )

    log1 = Logger("logger1")
    log2 = Logger("logger2")
    log1.debug("This is a debug msg")
    log1.info("This is a info msg")
    log2.debug("This is a debug msg")
    log2.info("This is a info msg")

    # msgbus.subscribe(topic="order", handler=handler1)
    # msgbus.subscribe(topic="order", handler=handler2)
    # msgbus.subscribe(topic="order", handler=handler3)

    # try:
    #     while True:
    #         msgbus.publish(topic="order", msg="hello")
    #         time.sleep(1)
    # except KeyboardInterrupt:
    #     print("Exiting...")

    # print("done")
    # from datetime import timedelta, datetime, timezone

    # count = 0
    # name = "TEST_TIMER 111"

    # def count_handler(event: TimeEvent):
    #     nonlocal count
    #     count += 1

    #     print(
    #         f"[{clock.utc_now()}] {event.ts_event} {event.ts_init} {clock.timestamp_ns() - clock.next_time_ns(name)} {event.ts_event - clock.next_time_ns(name)}"
    #     )

    # # clock.register_default_handler(count_handler)

    # interval = timedelta(milliseconds=1000)
    # start_time = (datetime.now(tz=timezone.utc) + timedelta(seconds=1)).replace(
    #     microsecond=0
    # )
    # clock.set_timer(
    #     name=name,
    #     interval=interval,
    #     start_time=start_time,
    #     stop_time=None,
    #     callback=count_handler,
    # )

    # time.sleep(10000)


if __name__ == "__main__":
    usage()
