#
# Copyright (C) 2019-Present A-Team Digital Solutions
#
"""
Project Loader

Creates and populates a Project object.
"""

from pathlib import Path
from .project import Project
from .config_loader import load_config

def load_project(
    rom_path,
    device_name
):
    project = Project()
    project.set_rom_path(
        rom_path
    )
    project.set_device(
        device_name
    )
    #
    # Device Configuration
    #
    project.device_vars = load_config(
        f"configs/devices/{device_name.lower()}.conf"
    )
    #
    # ROM Configuration
    #
    # If the selected ROM root contains its own rom.conf,
    # use that instead of the builder's default rom.conf.
    #
    local_rom_config = (
        Path(rom_path) / "rom.conf"
    )
    if local_rom_config.is_file():
        print()
        print("================================")
        print("Using ROM Local Configuration")
        print("================================")
        print()
        print(f"ROM Config: {local_rom_config}")
        print()
        project.rom_vars = load_config(
            local_rom_config
        )
        project.rom_config_path = str(
            local_rom_config
        )
    else:
        print()
        print("================================")
        print("Using Builder ROM Configuration")
        print("================================")
        print()
        print("ROM Config: configs/rom.conf")
        print()
        project.rom_vars = load_config(
            "configs/rom.conf"
        )
        project.rom_config_path = (
            "configs/rom.conf"
        )
    #
    # Upload Services Configuration
    #
    project.upload_vars = load_config(
        "configs/upload_services.conf"
    )
    #
    # A-Team Setup Configuration
    #
    project.setup_vars = load_config(
        "configs/a_team_setup.conf"
    )
    return project
