# from libs import osaf
import os
import shutil

from libs.osaf import osaf_core as osaf
from libs.osaf.integrations.nt.NT_Detect import NT_Detect
from libs.osaf.osaf_core import OSResources
from libs.spider_logger import logger


class NT_HostResources(OSResources):
    def __init__(self, core):
        super().__init__(core)


    # get dirty disks list
    def getMountedStoragesRoots(self):
        import os.path

        dl = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        drives = []
        for dc in dl:
            dp = f'{dc}:\\'
            if os.path.exists(dp):
                drives.append(dp)

        return drives


class NT_OSAF(osaf.OSAF_Core):

    def __init__(self, logger: logger.SpiderLog, master_workdir, conf):
        super().__init__(logger, master_workdir, conf)
        self.files = NT_Files(self)
        self.processes = NT_Process(self)
        self.resources = NT_HostResources(self)
        self.platform_info = NT_Detect(self, f"{conf['osa_assets_path']}/data/")

        self.log.d(f"NT_Detect: {self.platform_info.profile}" )


#
# def init(loader : MorzheML):
#     if loader.syspack.platform_info is not None and loader.syspack.platform_info.basic_platform == 'Windows':
#         loader.syspack.log.d('Apply Windows OSAF Plugin')
#         loader.syspack.set_os_abstraction(WindowsNT_OSAF(logger=loader.syspack.log, master_workdir=loader.syspack.operate_dir))

class NT_Process(osaf.OSProcess):
    def __init__(self, osaf_core):
        super().__init__(osaf_core)

    def kill(self, pid):
        self.exec([f'taskkill', '/PID', pid, '/F'])

class NT_Files(osaf.OSBasicFiles):
    def __init__(self, osaf_core):
        super().__init__(osaf_core)

    def touch(self, file_path):
        from pathlib import Path
        Path(file_path).touch()

    def mkdir(self, dir_path, mode=None, parents=True):
        d = self.p(dir_path)
        if self.is_dir(d):
            self.log.d(f'dir is exists {d}, skip mkdir')
        else:
            self.log.d(f'mkdir {d}')
            os.makedirs(d)


    def mv(self, src, dest):
        return shutil.move(src, dest)

    def cp(self, src, dest):
        if self.is_dir(src):
            from distutils.dir_util import copy_tree
            return copy_tree(src, dest)
        else:
            return shutil.copy(src, dest)

    def chmod(self, file, mode="0700", recurcive=False):
        return None
        # if recurcive:
        #     return self.proc.exec(['chmod', mode, file,'-R'])
        # else:
        #     return self.proc.exec(['chmod', mode, file])

    def chown(self, file, new_own, new_grp=None, recursive=False):
        pass
        # ow = new_own
        # if new_grp:
        #     ow = f'{ow}:{new_grp}'
        #
        # mm = ['chown', ow, file]
        #
        # if recursive:
        #     mm.append('-R')
        # mm.append('-v')
        # return self.proc.exec(mm)

    def list(self, dir_path=None):
        if dir_path is None:
            return os.listdir()
        else:
            return os.listdir(dir_path)
