# ruff: noqa: D100, D101, D102, D103, D104, D107
from __future__ import annotations

from typing import TYPE_CHECKING, Callable

from headless_kivy_pi import HeadlessWidget
from kivy.base import Clock
from redux import (
    BaseCombineReducerState,
    CombineReducerAction,
    CreateStoreOptions,
    combine_reducers,
    create_store,
)

from ubo_app.logging import logger
from ubo_app.store.camera import CameraAction, CameraEvent
from ubo_app.store.keypad import KeypadEvent
from ubo_app.store.main import MainAction, MainState, main_reducer
from ubo_app.store.sound import SoundAction, SoundState
from ubo_app.store.wifi import WiFiAction, WiFiEvent, WiFiState

from .status_icons import reducer as status_icons_reducer

if TYPE_CHECKING:
    from .status_icons import StatusIconsState


class RootState(BaseCombineReducerState):
    main: MainState
    sound: SoundState
    status_icons: StatusIconsState
    wifi: WiFiState


ActionType = MainAction | SoundAction | CombineReducerAction | WiFiAction | CameraAction
EventType = KeypadEvent | CameraEvent | WiFiEvent

root_reducer, root_reducer_id = combine_reducers(
    state_type=RootState,
    action_type=ActionType,
    event_type=EventType,
    main=main_reducer,
    status_icons=status_icons_reducer,
)


def scheduler(main_loop_callback: Callable[[], None]) -> None:
    Clock.create_trigger(
        lambda _: main_loop_callback(),
        0,
        interval=True,
    )()


store = create_store(
    root_reducer,
    CreateStoreOptions(scheduler=scheduler),
)


def dispatch(items: ActionType | EventType | list[ActionType | EventType]) -> None:
    logger.debug('Dispatch', extra={'items': items})
    store.dispatch(items)


autorun = store.autorun

subscribe = store.subscribe
subscribe_event = store.subscribe_event

subscribe(lambda _: HeadlessWidget.render())

__ALL__ = (autorun, dispatch, subscribe)
