import platform

import esphome.codegen as cg
import esphome.config_validation as cv

DEPENDENCIES = ["rp2"]


PIOASM_REPO_VERSION = "1.5.0-b"
PIOASM_REPO_BASE = f"https://github.com/earlephilhower/pico-quick-toolchain/releases/download/{PIOASM_REPO_VERSION}"
PIOASM_VERSION = "pioasm-2e6142b.230216"
PIOASM_DOWNLOADS = {
    "linux": {
        "aarch64": f"aarch64-linux-gnu.{PIOASM_VERSION}.tar.gz",
        "armv7l": f"arm-linux-gnueabihf.{PIOASM_VERSION}.tar.gz",
        "x86_64": f"x86_64-linux-gnu.{PIOASM_VERSION}.tar.gz",
    },
    "windows": {
        "amd64": f"x86_64-w64-mingw32.{PIOASM_VERSION}.zip",
    },
    "darwin": {
        "x86_64": f"x86_64-apple-darwin14.{PIOASM_VERSION}.tar.gz",
        "arm64": f"x86_64-apple-darwin14.{PIOASM_VERSION}.tar.gz",
    },
}


async def to_code(config):
    # cg.add_platformio_option(
    #     "platform_packages",
    #     [
    #         "earlephilhower/tool-pioasm-rp2040-earlephilhower",
    #     ],
    # )
    os_name = platform.system().lower()
    arch = platform.machine().lower()
    if os_name not in PIOASM_DOWNLOADS or arch not in PIOASM_DOWNLOADS[os_name]:
        raise cv.Invalid(
            f"pioasm is not available for {platform.system()} {platform.machine()}"
        )
    file = PIOASM_DOWNLOADS[os_name][arch]
    cg.add_platformio_option(
        "platform_packages",
        [f"earlephilhower/tool-pioasm-rp2040-earlephilhower@{PIOASM_REPO_BASE}/{file}"],
    )
    cg.add_platformio_option("extra_scripts", ["pre:build_pio.py"])
