load("//aos:config.bzl", "aos_config")
load("//aos/util:config_validator_macro.bzl", "config_validator_test")
load("//frc971:downloader.bzl", "robot_downloader")

config_validator_test(
    name = "config_validator_test",
    config = "//y2024:aos_config",
)

robot_downloader(
    binaries = [
        "//aos/network:web_proxy_main",
        "//aos/events/logging:log_cat",
        "//y2024/constants:constants_sender",
        "//aos/events:aos_timing_report_streamer",
    ],
    data = [
        ":aos_config",
        "//aos/starter:roborio_irq_config.json",
        "//y2024/constants:constants.json",
        "@ctre_phoenix6_api_cpp_athena//:shared_libraries",
        "@ctre_phoenix6_tools_athena//:shared_libraries",
        "@ctre_phoenix_api_cpp_athena//:shared_libraries",
        "@ctre_phoenix_cci_athena//:shared_libraries",
    ],
    dirs = [
        "//y2024/www:www_files",
        "//y2024/autonomous:splines",
    ],
    start_binaries = [
        "//aos/events/logging:logger_main",
        "//aos/network:web_proxy_main",
        "//aos/starter:irq_affinity",
        "//y2024/autonomous:binaries",
        ":joystick_reader",
        ":wpilib_interface",
        "//frc971/can_logger",
        "//aos/network:message_bridge_client",
        "//aos/network:message_bridge_server",
        "//y2024/control_loops/drivetrain:drivetrain",
        "//y2024/control_loops/drivetrain:trajectory_generator",
        "//y2024/control_loops/superstructure:superstructure",
    ],
    target_compatible_with = ["@platforms//os:linux"],
)

robot_downloader(
    name = "orin_download",
    binaries = [
        "//frc971/wpilib:joystick_republish",
        "//aos/events:aos_timing_report_streamer",
        "//aos/events/logging:log_cat",
        "//aos:aos_jitter",
        "//aos/network:web_proxy_main",
        "//aos/starter:irq_affinity",
        "//aos/util:foxglove_websocket",
        "//frc971/image_streamer:image_streamer",
        "//frc971/orin:hardware_monitor",
        "//frc971/orin:argus_monitor",
        "//frc971/vision:intrinsics_calibration",
        "//aos/util:filesystem_monitor",
        "//y2024/vision:viewer",
        "//y2024/constants:constants_sender",
        "//y2024/localizer:localizer_main",
        "//frc971/orin:localizer_logger",
        "//frc971/vision:foxglove_image_converter",
    ],
    data = [
        ":aos_config",
        "//frc971/orin:orin_irq_config.json",
        "//y2024/constants:constants.json",
        "//y2024/vision:image_streamer_start",
        "//y2024/www:www_files",
    ],
    dirs = [
        "//y2024/www:www_files",
        "//frc971/image_streamer/www:www_files",
    ],
    start_binaries = [
        "//aos/events/logging:logger_main",
        "//frc971/imu_fdcan:can_translator",
        "//frc971/imu_fdcan:dual_imu_blender",
        "//aos/network:message_bridge_client",
        "//aos/network:message_bridge_server",
        "//aos/network:web_proxy_main",
        "//aos/starter:irq_affinity",
        "//frc971/orin:argus_camera",
        "//y2024/orin:can_logger",
        "//y2024/vision:apriltag_detector",
        "//frc971/vision:image_logger",
    ],
    target_compatible_with = ["//tools/platforms/hardware:raspberry_pi"],
    target_type = "pi",
)

aos_config(
    name = "aos_config",
    src = "y2024.json",
    flatbuffers = [
        "//aos/network:message_bridge_client_fbs",
        "//aos/network:message_bridge_server_fbs",
        "//aos/network:timestamp_fbs",
        "//frc971/input:robot_state_fbs",
        "//frc971/vision:vision_fbs",
        "//frc971/vision:target_map_fbs",
    ],
    target_compatible_with = ["@platforms//os:linux"],
    visibility = ["//visibility:public"],
    deps = [
        ":config_imu",
        ":config_orin1",
        ":config_roborio",
    ],
)

aos_config(
    name = "config_imu",
    src = "y2024_imu.json",
    flatbuffers = [
        "//aos/network:message_bridge_client_fbs",
        "//aos/network:message_bridge_server_fbs",
        "//frc971/imu_fdcan:dual_imu_fbs",
        "//frc971/imu_fdcan:can_translator_status_fbs",
        "//frc971/imu_fdcan:dual_imu_blender_status_fbs",
        "//y2024/constants:constants_fbs",
        "//frc971/control_loops/drivetrain/localization:localizer_output_fbs",
        "//frc971/can_logger:can_logging_fbs",
        "//frc971/orin:hardware_stats_fbs",
        "//y2024/localizer:status_fbs",
        "//y2024/localizer:visualization_fbs",
        "//aos/network:timestamp_fbs",
        "//aos/util:filesystem_fbs",
        "//aos/network:remote_message_fbs",
        "//frc971/vision:calibration_fbs",
        "//frc971/vision:target_map_fbs",
        "//frc971/vision:vision_fbs",
        "@com_github_foxglove_schemas//:schemas",
    ],
    target_compatible_with = ["@platforms//os:linux"],
    visibility = ["//visibility:public"],
    deps = [
        "//aos/events:aos_config",
        "//frc971/control_loops/drivetrain:aos_config",
    ],
)

aos_config(
    name = "config_roborio",
    src = "y2024_roborio.json",
    flatbuffers = [
        "//frc971:can_configuration_fbs",
        "//aos/network:remote_message_fbs",
        "//aos/network:message_bridge_client_fbs",
        "//aos/network:message_bridge_server_fbs",
        "//frc971/wpilib:pdp_values_fbs",
        # y2019 stuff shouldn't be here (e.g. target selector)
        "//y2024/constants:constants_fbs",
        "//aos/network:timestamp_fbs",
        "//y2024/control_loops/superstructure:superstructure_goal_fbs",
        "//frc971/control_loops/drivetrain:drivetrain_can_position_fbs",
        "//y2024/control_loops/superstructure:superstructure_can_position_fbs",
        "//y2024/control_loops/superstructure:superstructure_output_fbs",
        "//frc971/control_loops/drivetrain:rio_localizer_inputs_fbs",
        "//y2024/control_loops/superstructure:superstructure_position_fbs",
        "//frc971/control_loops/drivetrain/localization:localizer_output_fbs",
        "//y2024/control_loops/superstructure:superstructure_status_fbs",
        "//frc971/can_logger:can_logging_fbs",
    ],
    target_compatible_with = ["@platforms//os:linux"],
    deps = [
        "//aos/events:aos_config",
        "//frc971/autonomous:aos_config",
        "//frc971/control_loops/drivetrain:aos_config",
        "//frc971/input:aos_config",
    ],
)

aos_config(
    name = "config_orin1",
    src = "y2024_orin1.json",
    flatbuffers = [
        "//aos/network:message_bridge_client_fbs",
        "//aos/network:message_bridge_server_fbs",
        "//aos/network:timestamp_fbs",
        "//aos/network:remote_message_fbs",
        "//y2024/constants:constants_fbs",
        "//frc971/control_loops/drivetrain/localization:localizer_output_fbs",
        "//frc971/orin:hardware_stats_fbs",
        "//frc971/vision:calibration_fbs",
        "//y2024/localizer:visualization_fbs",
        "//frc971/vision:target_map_fbs",
        "//frc971/vision:vision_fbs",
        "//aos/util:filesystem_fbs",
        "@com_github_foxglove_schemas//:schemas",
    ],
    target_compatible_with = ["@platforms//os:linux"],
    visibility = ["//visibility:public"],
    deps = [
        "//aos/events:aos_config",
        "//frc971/control_loops/drivetrain:aos_config",
        "//frc971/input:aos_config",
    ],
)

cc_library(
    name = "constants",
    srcs = [
        "constants.cc",
    ],
    hdrs = [
        "constants.h",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//aos/mutex",
        "//aos/network:team_number",
        "//frc971:constants",
        "//frc971/control_loops:pose",
        "//frc971/control_loops:static_zeroing_single_dof_profiled_subsystem",
        "//frc971/shooter_interpolation:interpolation",
        "//frc971/zeroing:absolute_encoder",
        "//frc971/zeroing:pot_and_absolute_encoder",
        "//y2024/constants:constants_fbs",
        "//y2024/control_loops/drivetrain:polydrivetrain_plants",
        "//y2024/control_loops/superstructure/altitude:altitude_plants",
        "//y2024/control_loops/superstructure/catapult:catapult_plants",
        "//y2024/control_loops/superstructure/climber:climber_plants",
        "//y2024/control_loops/superstructure/extend:extend_plants",
        "//y2024/control_loops/superstructure/intake_pivot:intake_pivot_plants",
        "//y2024/control_loops/superstructure/turret:turret_plants",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
    ],
)

cc_binary(
    name = "wpilib_interface",
    srcs = [
        "wpilib_interface.cc",
    ],
    target_compatible_with = ["//tools/platforms/hardware:roborio"],
    deps = [
        ":constants",
        "//aos:init",
        "//aos:math",
        "//aos/containers:sized_array",
        "//aos/events:shm_event_loop",
        "//aos/logging",
        "//aos/stl_mutex",
        "//aos/time",
        "//aos/util:log_interval",
        "//aos/util:phased_loop",
        "//aos/util:wrapping_counter",
        "//frc971:can_configuration_fbs",
        "//frc971/autonomous:auto_mode_fbs",
        "//frc971/constants:constants_sender_lib",
        "//frc971/control_loops:control_loop",
        "//frc971/control_loops:control_loops_fbs",
        "//frc971/control_loops/drivetrain:drivetrain_can_position_fbs",
        "//frc971/control_loops/drivetrain:drivetrain_position_fbs",
        "//frc971/input:robot_state_fbs",
        "//frc971/queues:gyro_fbs",
        "//frc971/wpilib:ADIS16448",
        "//frc971/wpilib:buffered_pcm",
        "//frc971/wpilib:can_drivetrain_writer",
        "//frc971/wpilib:can_sensor_reader",
        "//frc971/wpilib:dma",
        "//frc971/wpilib:drivetrain_writer",
        "//frc971/wpilib:encoder_and_potentiometer",
        "//frc971/wpilib:generic_can_writer",
        "//frc971/wpilib:joystick_sender",
        "//frc971/wpilib:logging_fbs",
        "//frc971/wpilib:pdp_fetcher",
        "//frc971/wpilib:sensor_reader",
        "//frc971/wpilib:talonfx",
        "//frc971/wpilib:wpilib_robot_base",
        "//third_party:phoenix",
        "//third_party:phoenix6",
        "//third_party:wpilib",
        "//y2024/constants:constants_fbs",
        "//y2024/control_loops/superstructure:led_indicator_lib",
        "//y2024/control_loops/superstructure:superstructure_can_position_fbs",
        "//y2024/control_loops/superstructure:superstructure_output_fbs",
        "//y2024/control_loops/superstructure:superstructure_position_fbs",
    ],
)

cc_binary(
    name = "joystick_reader",
    srcs = [
        ":joystick_reader.cc",
    ],
    deps = [
        ":constants",
        "//aos:init",
        "//aos/actions:action_lib",
        "//aos/logging",
        "//frc971/autonomous:auto_fbs",
        "//frc971/autonomous:base_autonomous_actor",
        "//frc971/control_loops:profiled_subsystem_fbs",
        "//frc971/control_loops:static_zeroing_single_dof_profiled_subsystem",
        "//frc971/input:action_joystick_input",
        "//frc971/input:drivetrain_input",
        "//frc971/input:joystick_input",
        "//frc971/input:redundant_joystick_data",
        "//y2024/control_loops/drivetrain:drivetrain_base",
        "//y2024/control_loops/superstructure:superstructure_goal_fbs",
        "//y2024/control_loops/superstructure:superstructure_status_fbs",
    ],
)

py_library(
    name = "python_init",
    srcs = ["__init__.py"],
    target_compatible_with = ["@platforms//os:linux"],
    visibility = ["//visibility:public"],
)

sh_binary(
    name = "log_web_proxy",
    srcs = ["log_web_proxy.sh"],
    data = [
        ":aos_config",
        "//aos/network:log_web_proxy_main",
        "//frc971/www:starter_main_bundle.min.js",
        "//y2024/www:field_main_bundle.min.js",
        "//y2024/www:files",
    ],
    target_compatible_with = ["@platforms//os:linux"],
)
