use std::{
    cell::{Cell, RefCell},
    rc::Rc,
};

use evdev::Device;
use gtk::{glib, pango, prelude::*};

use super::{
    devices::{CameraMode, DeviceCatalog},
    diagnostics::DiagnosticsLog,
    preview::{LauncherPreview, PreviewBinding, PreviewSurface},
    state::{
        BreakoutSizeChoice, BreakoutSizePreset, CaptureSizeChoice, CaptureSizePreset,
        FeedSourceChoice, FeedSourcePreset, LauncherState, UpstreamAudioTransport, WebcamTransport,
    },
};

include!("ui_components/types.rs");
include!("ui_components/build_contexts.rs");
include!("ui_components/style.rs");
include!("ui_components/panel_chips.rs");
include!("ui_components/combo_helpers.rs");
include!("ui_components/control_buttons.rs");
include!("ui_components/display_pane.rs");
include!("ui_components/scale_reset.rs");

pub fn build_launcher_view(
    app: &gtk::Application,
    server_addr: &str,
    catalog: &DeviceCatalog,
    state: &LauncherState,
) -> LauncherView {
    let LauncherShellContext {
        window,
        root,
        staging_row,
        operations,
        left_pane,
        right_pane,
        relay_light,
        relay_value,
        routing_light,
        routing_value,
        gpio_light,
        gpio_value,
        left_eye_light,
        left_eye_value,
        right_eye_light,
        right_eye_value,
        usb_light,
        usb_value,
        uac_light,
        uac_value,
        uvc_light,
        uvc_value,
        upstream_lag_light,
        upstream_lag_value,
        shortcut_value,
    } = include!("ui_components/build_shell.rs");

    let DeviceControlsContext {
        device_refresh_button,
        camera_combo,
        camera_quality_combo,
        microphone_combo,
        speaker_combo,
        keyboard_combo,
        mouse_combo,
        camera_channel_toggle,
        microphone_channel_toggle,
        noise_suppression_toggle,
        audio_channel_toggle,
        audio_gain_scale,
        audio_gain_value,
        mic_gain_scale,
        mic_gain_value,
        audio_check_detail,
        audio_check_meter,
        devices_panel,
        preview_panel,
        camera_preview_stack,
        camera_preview_frame,
        camera_preview,
        webcam_transport_combo,
        upstream_audio_transport_combo,
        camera_mirror_button,
        camera_mirror_revealer,
        camera_status,
        camera_test_button,
        microphone_test_button,
        microphone_replay_button,
        speaker_test_button,
    } = include!("ui_components/build_device_controls.rs");

    let OperationsRailContext {
        server_entry,
        start_button,
        certs_button,
        clipboard_button,
        wake_combo,
        usb_recover_button,
        uac_recover_button,
        uvc_recover_button,
        calibration_default_button,
        calibration_factory_button,
        calibration_blind_button,
        calibration_minus_button,
        calibration_plus_button,
        calibration_rig_button,
        power_auto_button,
        power_on_button,
        power_off_button,
        power_detail,
        input_toggle_button,
        swap_key_button,
        diagnostics_copy_button,
        diagnostics_popout_button,
        diagnostics_log,
        diagnostics_label,
        diagnostics_scroll,
        console_copy_button,
        console_popout_button,
        console_level_combo,
        session_log_level,
        status_label,
        session_log_buffer,
        session_log_view,
        preview,
    } = include!("ui_components/build_operations_rail.rs");

    include!("ui_components/assemble_view.rs")
}

#[cfg(test)]
#[path = "tests/ui_components_scale.rs"]
mod tests;
