import os
import shutil
from datetime import datetime
from distutils.dir_util import copy_tree


class LoggerMananger(object):
        def __init__(self):
            self.workspace = "/tmp"

        @classmethod
        def set_workspace(cls, task="Fake_SN", workspace="/tmp/mProDebugger", timestamp=True):
            if timestamp:
                cls.workspace = os.path.join(workspace, "{}_{}".format(task, datetime.now().strftime('%Y%m%d')))
            else:
                cls.workspace = os.path.join(workspace, "{}".format(task))
            if not os.path.exists(cls.workspace):
                os.makedirs(cls.workspace)

        @classmethod
        def get_workspace(cls):
            return cls.workspace

        @staticmethod
        def get_logger_path(name):
                base, ext = os.path.splitext(name)
                if not ext:
                        ext = ".log"

                timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
                return os.path.join(LoggerMananger.workspace, "{}_{}{}".format(timestamp, base, ext))

        @staticmethod
        def get_daily_logger_path(name):
            base, ext = os.path.splitext(name)
            if not ext:
                ext = ".log"

            timestamp = datetime.now().strftime('%Y%m%d')
            return os.path.join(LoggerMananger.workspace, "{}_{}{}".format(timestamp, base, ext))

        @staticmethod
        def write_log(path, str):
                f = open(path, 'ab')
                f.seek(0)
                f.truncate()
                f.write(str)
                f.close()

        @staticmethod
        def append_log(path, str):
                f = open(path, 'ab')
                f.write(str)
                f.close()


class FileMananger(object):
        def __init__(self):
            pass

        @staticmethod
        def list_file_in_folder(folder, ignore_hide=True, followlinks=False):
            ret = []
            for root, dirs, files in os.walk(folder, followlinks=followlinks):
                if ignore_hide:
                    files = [f for f in files if not f[0] == '.']
                    dirs[:] = [d for d in dirs if not (d[0] == '.' or d == '__MACOSX')]

                for f in files:
                    f_path = os.path.join(root, f)
                    if not os.path.exists(f_path):
                        continue

                    ret.append(f_path)

            return ret

        @staticmethod
        def copy_path_into_folder(src, dest_folder, remove=False):
            FileMananger.create_folder(dest_folder)
            if os.path.isdir(src):
                copy_tree(src, os.path.join(dest_folder, os.path.basename(src)))
                if remove:
                    shutil.rmtree(src)

            else:
                shutil.copy(src, dest_folder)
                if remove:
                    os.remove(src)

        @staticmethod
        def remove_path(path):
            if os.path.exists(path):
                if os.path.isdir(path):
                    shutil.rmtree(path)
                else:
                    os.remove(path)
            if os.path.islink(path):
                os.unlink(path)

        @staticmethod
        def create_empty_folder(path):
            FileMananger.remove_path(path)
            FileMananger.create_folder(path)

        @staticmethod
        def create_folder(path):
            if not os.path.exists(path):
                os.makedirs(path)
