import os

from kivy.app import App
from kivy.logger import Logger
from kivy.lang import Builder

from carbonkivy.theme import CarbonTheme


class CarbonApp(App, CarbonTheme):
    def __init__(self, **kwargs):
        super(CarbonApp, self).__init__(**kwargs)

    def load_all_kv_files(self, directory: str, *args) -> None:
        """
        Recursively load all kv files from a given directory.
        """

        for root, dirs, files in os.walk(directory):
            if "carbonkivy" in directory:
                Logger.critical(
                    "CarbonKivy: "
                    "Do not use the word 'carbonkivy' in the name of the "
                    "directory from where you download KV files"
                )
            if "venv" in root or ".buildozer" in root or os.path.join("carbonkivy") in root:
                continue
            for file in files:
                if (
                    os.path.splitext(file)[1] == ".kv"
                    and file != "style.kv"  # if use PyInstaller
                    and "__MACOS" not in root  # if use Mac OS
                ):
                    path_to_kv_file = os.path.join(root, file)
                    Builder.load_file(path_to_kv_file)
