# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
# >-|===|>                             Imports                             <|===|-<
# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
from kbasic import cyan, yellow, red, File, TOML, Folder, ensure_path, could_be_path
from os.path import abspath
from os import system
from argparse import ArgumentParser, Namespace

# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
# >-|===|>                           Definitions                           <|===|-<
# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
commands = ['init']
parser = ArgumentParser(
    prog='kpaper', 
    usage='create a kpaper project from a template...',
    description="",
    epilog=""
)
parser.add_argument(
    'command', 
    action='store', nargs='?', help=f"the action you want kpaper to take (one of {",".join(commands)})"
)
parser.add_argument(
    'arguments', 
    action='store', default='./', nargs='?', help="The path to the kpaper directory"
)
parser.add_argument(
    '-b', '--basic', 
    action='store_true', help="use a minimal template paper"
)

# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
# >-|===|>                      Command Line Interface                     <|===|-<
# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
def kpaper():
    args = parser.parse_args()
    match args:
        case Namespace(command=None): print(yellow(f'no commands given...\nplease choose use one of the following:\n\t* {'\n\t* '.join(commands)}'))
        case Namespace(command='init', arguments=path, basic=False): copy_template('default', abspath(path))
        case Namespace(command='init', arguments=path, basic=True): copy_template('minimal', abspath(path))
        case Namespace(command='add', arguments=x): print(x)

# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
# >-|===|>                            Functions                            <|===|-<
# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
def copy_template(template_name: str, destination) -> None:
    ensure_path(destination)
    print(cyan(f'initializing kpaper project in {abspath(destination)}'))
    kpaperDir = File(__file__).parent
    template: Folder = kpaperDir + f'/templates/{template_name}'
    config_file = File(kpaperDir.path + '/templates/config-template')
    config_file.copy(destination+'/.kpaper')
    for c in template.children: 
        name = c.split('/')[-1]
        system(f"cp -r {c} {destination+'/'+name}")    

# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
# >-|===|>                             Classes                             <|===|-<
# !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
class KPaperConfig(TOML):
    def __init__(self, ): pass 

class KPaper(Folder):
    def __init__(self, path: str|Folder): pass