#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os, sys
import shutil
import re
import importlib
import importlib.util
from kconfiglib import Kconfig
from menuconfig import menuconfig

def mconf_set_env(argv):
    """
    Set Kconfig Env
    """
    os.environ["MENUCONFIG_STYLE"] = "default selection=fg:white,bg:blue"
    os.environ["KCONFIG_CONFIG"] = os.path.join(".bconfig")
    os.environ["KCONFIG_CONFIG_HEADER"] = "# Generated by BabyOS Configuration"
    os.environ["KCONFIG_AUTOHEADER"] = os.path.join("b_config.h")
    os.environ["CONFIG_"] = ""

def mconfig(path):
    print(path)
    
    kconf_fd = open("Kconfig", 'w+')
    kconf_list = "mainmenu " + "\"BabyOS Configuration\" \n"
    kconf_list += "source " + "\"" + path + "/core/Kconfig" + "\"" + "\n" 
    kconf_list += "source " + "\"" + path + "/hal/Kconfig" + "\"" + "\n"
    kconf_list += "source " + "\"" + path + "/mcu/Kconfig" + "\"" + "\n"
    kconf_list += "source " + "\"" + path + "/algorithm/Kconfig" + "\"" + "\n"
    kconf_list += "source " + "\"" + path + "/drivers/Kconfig" + "\"" + "\n"
    kconf_list += "source " + "\"" + path + "/modules/Kconfig" + "\"" + "\n"
    kconf_list += "source " + "\"" + path + "/thirdparty/Kconfig" + "\"" + "\n"
    kconf_list += "source " + "\"" + path + "/utils/Kconfig" + "\"" + "\n"
    kconf_list += "source " + "\"" + path + "/services/Kconfig" + "\"" + "\n"
    print(kconf_list)
    kconf_fd.write(kconf_list)
    kconf_fd.close()
    
    mconf_set_env(path)
    kconf = Kconfig(filename="./Kconfig")
    menuconfig(kconf)
    kconf.write_autoconf()
    bconf_fd = open("b_config.h", 'r')
    bconf_data = bconf_fd.read()
    bconf_fd.close()
    print(bconf_data)

    bconf_new_data = "#ifndef __B_CONFIG_H__ \n"
    bconf_new_data += "#define __B_CONFIG_H__ \n\n\n"
    bconf_new_data += bconf_data
    bconf_new_data += "\n\n#include \"b_type.h\" \n\n"
    bconf_new_data += "#endif \n\n"
    bconf_fd = open("b_config.h", 'w')
    bconf_fd.write(bconf_new_data)
    bconf_fd.close()
    
    os.remove("Kconfig")

def check_path_exists(path):
    filepath = os.path.join(path, "b_os.h")
    if not os.path.isfile(filepath):
        print(path + " is invalid; Please enter the correct bos path:")
        path_a = input(">> ")
        path_a = check_path_exists(path_a)
    else:
        path_a = path
    return path_a

def replace_string_in_file(filename, old_string, new_string):
    with open(filename, 'r') as file:
        content = file.read()
        file.close()
    old_string = old_string.strip()
    new_string = new_string.strip()
    if old_string != new_string:
        content = content.replace(old_string, new_string)
        with open(filename, 'w') as file:
            file.write(content)
            file.close()

def call_function(file_path, function_name, arg):
    # 加载文件
    spec = importlib.util.spec_from_file_location("module_name", file_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    # 获取函数
    if hasattr(module, function_name):
        function = getattr(module, function_name)
        # 调用函数
        function(arg)
    else:
        print(f"在文件 {file_path} 中找不到函数 {function_name}。")


if __name__ == "__main__":
    bos_path = check_path_exists(sys.argv[1])
    replace_string_in_file("b_config.bat", sys.argv[1], bos_path)
    replace_string_in_file("b_config.sh", sys.argv[1], bos_path)
    mconfig(bos_path)
    call_function(bos_path + "/thirdparty/thirdparty.py", "cp_thirdparty_file", bos_path)