from __future__ import annotations

from .. import get_hw_model_as_number
from ..stm32f4_common import stm32f4_common_files


def configure(
    env: dict,
    features_wanted: list[str],
    defines: list[str | tuple[str, str]],
    sources: list[str],
    paths: list[str],
) -> list[str]:
    features_available: list[str] = []
    board = "D001/boards/stm32f429i-disc1.h"
    hw_model = get_hw_model_as_number("D001")
    hw_revision = 0

    mcu = "STM32F429xx"

    features_available += stm32f4_common_files(
        env, features_wanted, defines, sources, paths
    )

    ENV = env.get("ENV")
    assert ENV

    ENV["CPU_ASFLAGS"] = "-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16"
    ENV["CPU_CCFLAGS"] = (
        "-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mtune=cortex-m4 "
    )
    ENV["RUST_TARGET"] = "thumbv7em-none-eabihf"

    defines += [
        mcu,
        ("TREZOR_BOARD", f'"{board}"'),
        ("HW_MODEL", str(hw_model)),
        ("HW_REVISION", str(hw_revision)),
        ("HSE_VALUE", "8000000"),
        ("USE_HSE", "1"),
    ]

    if "display" in features_wanted:
        sources += [
            "embed/io/display/stm32f429i-disc1/display_driver.c",
            "embed/io/display/stm32f429i-disc1/display_ltdc.c",
            "embed/io/display/stm32f429i-disc1/ili9341_spi.c",
        ]
        paths += ["embed/io/display/inc"]
        defines += [("USE_DISPLAY", "1")]

        sources += ["embed/io/gfx/bitblt/stm32/dma2d_bitblt.c"]

        sources += [
            "vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c"
        ]
        sources += [
            "vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c"
        ]
        defines += ["USE_DMA2D"]
        features_available.append("dma2d")

        defines += [
            "FRAMEBUFFER",
            "DISPLAY_RGB565",
            ("USE_RGB_COLORS", "1"),
            ("DISPLAY_RESX", "240"),
            ("DISPLAY_RESY", "320"),
        ]
        features_available.append("framebuffer")
        features_available.append("display_rgb565")

    sources += ["embed/sys/sdram/stm32f429i-disc1/sdram_bsp.c"]
    paths += ["embed/sys/sdram/inc"]
    defines += [("USE_SDRAM", "1")]

    if "input" in features_wanted:
        sources += ["embed/sys/i2c_bus/stm32f4/i2c_bus.c"]
        sources += ["embed/io/touch/stmpe811/stmpe811.c"]
        sources += ["embed/io/touch/stmpe811/touch.c"]
        sources += ["embed/io/touch/touch_poll.c"]
        paths += ["embed/sys/i2c_bus/inc"]
        paths += ["embed/io/touch/inc"]
        features_available.append("touch")
        defines += [
            ("USE_TOUCH", "1"),
            ("USE_I2C", "1"),
        ]

    defines += [("USE_PVD", "1")]

    return features_available
