"""rule.py — FoBiS.py ``rule`` subcommand."""

import typer

from ._app import _ns, app
from ._constants import __extensions_inc__, __extensions_parsed__
from ._options import *


@app.command("rule")
def cmd_rule(
    ctx: typer.Context,
    # fobos group
    fobos: FobosOpt = None,
    fobos_case_insensitive: FciOpt = False,
    mode: ModeOpt = None,
    lmodes: LmodesOpt = False,
    print_fobos_template: PrintFobosTemplateOpt = False,
    # compiler group
    compiler: CompilerOpt = "gnu",
    fc: FcOpt = None,
    cflags: CflagsOpt = None,
    lflags: LflagsOpt = None,
    modsw: ModswOpt = None,
    mpi: MpiOpt = False,
    openmp: OpenmpOpt = False,
    openmp_offload: OpenmpOffloadOpt = False,
    coarray: CoarrayOpt = False,
    coverage: CoverageOpt = False,
    profile: ProfileOpt = False,
    mklib: MklibOpt = None,
    ar: ArOpt = "ar",
    arflags: ArflagsOpt = "-rcs",
    ranlib: RanlibOpt = "ranlib",
    cflags_heritage: CflagsHeritageOpt = False,
    track_build: TrackBuildOpt = False,
    # directories group
    src: SrcOpt = None,
    build_dir: BuildDirOpt = "./",
    obj_dir: ObjDirOpt = "./obj/",
    mod_dir: ModDirOpt = "./mod/",
    lib_dir: LibDirOpt = None,
    include: IncludeListOpt = None,
    exclude_dirs: ExcludeDirsOpt = None,
    disable_recursive_search: DisableRecursiveSearchOpt = False,
    # files group
    target: TargetOpt = None,
    output: OutputOpt = None,
    exclude: ExcludeOpt = None,
    libs: LibsOpt = None,
    vlibs: VlibsOpt = None,
    ext_libs: ExtLibsOpt = None,
    ext_vlibs: ExtVlibsOpt = None,
    dependon: DependonOpt = None,
    inc: IncOpt = None,
    extensions: ExtensionsOpt = None,
    build_all: BuildAllOpt = False,
    # preprocessor group
    preprocessor: PreprocessorOpt = None,
    preproc: PreprocOpt = None,
    preprocessor_args: PreprocessorArgsOpt = "",
    preprocessor_no_o: PreprocessorNoOOpt = False,
    preprocessor_dir: PreprocessorDirOpt = None,
    preprocessor_ext: PreprocessorExtOpt = None,
    # rules group
    execute: ExecuteOpt = None,
    list_rules: ListRulesOpt = False,
    # intrinsic rules group
    get: GetOpt = None,
    get_output_name: GetOutputNameOpt = False,
    ford: FordOpt = None,
    gcov_analyzer: GcovAnalyzerOpt = None,
    is_ascii_kind_supported: IsAsciiKindSupportedOpt = False,
    is_ucs4_kind_supported: IsUcs4KindSupportedOpt = False,
    is_float128_kind_supported: IsFloat128KindSupportedOpt = False,
    # fancy group
    force_compile: ForceCompileOpt = False,
    colors: ColorsOpt = False,
    log: LogOpt = False,
    graph: GraphOpt = False,
    quiet: QuietOpt = False,
    verbose: VerboseOpt = False,
    jobs: JobsOpt = 1,
    makefile: MakefileOpt = None,
):
    """Execute special rules or user-defined ones from a fobos file."""
    ctx.ensure_object(dict)
    ctx.obj["cliargs"] = _ns(
        which="rule",
        fobos=fobos,
        fobos_case_insensitive=fobos_case_insensitive,
        mode=mode,
        lmodes=lmodes,
        print_fobos_template=print_fobos_template,
        compiler=compiler.lower(),
        fc=fc,
        cflags=cflags,
        lflags=lflags,
        modsw=modsw,
        mpi=mpi,
        openmp=openmp,
        openmp_offload=openmp_offload,
        coarray=coarray,
        coverage=coverage,
        profile=profile,
        mklib=mklib,
        ar=ar,
        arflags=arflags,
        ranlib=ranlib,
        cflags_heritage=cflags_heritage,
        track_build=track_build,
        src=src or ["./"],
        build_dir=build_dir,
        obj_dir=obj_dir,
        mod_dir=mod_dir,
        lib_dir=lib_dir or [],
        include=include or [],
        exclude_dirs=exclude_dirs or [],
        disable_recursive_search=disable_recursive_search,
        target=target,
        output=output,
        exclude=exclude or [],
        libs=libs or [],
        vlibs=vlibs or [],
        ext_libs=ext_libs or [],
        ext_vlibs=ext_vlibs or [],
        dependon=dependon or [],
        inc=inc or list(__extensions_inc__),
        extensions=extensions or list(__extensions_parsed__),
        build_all=build_all,
        preprocessor=preprocessor,
        preproc=preproc,
        preprocessor_args=preprocessor_args,
        preprocessor_no_o=preprocessor_no_o,
        preprocessor_dir=preprocessor_dir,
        preprocessor_ext=preprocessor_ext or [],
        execute=execute,
        list=list_rules,
        get=get,
        get_output_name=get_output_name,
        ford=ford,
        gcov_analyzer=gcov_analyzer,
        is_ascii_kind_supported=is_ascii_kind_supported,
        is_ucs4_kind_supported=is_ucs4_kind_supported,
        is_float128_kind_supported=is_float128_kind_supported,
        force_compile=force_compile,
        colors=colors,
        log=log,
        graph=graph,
        quiet=quiet,
        verbose=verbose,
        jobs=jobs,
        makefile=makefile,
    )
