import os
import time
from django.conf import settings
from .exceptions import FolderNotFoundException, SemanticUIException
from .utils import Constant


class SemanticUI(object):
    """
    Main class used to install, gulp build and uninstall the Semantic UI Framework.
    """
    SECONDS_WAITING_ALLOWED = 5

    def __init__(self):
        """
        Build the default folder required by the DSU package.
        Define the default global variables required by the DSU package.
        """
        self.project_folder_path = os.path.dirname(__file__)
        try:
            self.jquery_url = settings.DSU_JQUERY_URL
        except:
            self.jquery_url = Constant.JQUERY_URL
        try:
            self.gulp_version = settings.GULP_VERSION
        except:
            self.gulp_version = Constant.GULP_VERSION
        try:
            self.semantic_ui_version = settings.SEMANTIC_UI_VERSION
        except:
            self.semantic_ui_version = Constant.SEMANTIC_UI_VERSION
        try:
            self.semantic_folder = settings.SEMANTIC_DIRNAME
        except:
            self.semantic_folder = Constant.SEMANTIC_DIRNAME

    def move_static_folder(self):
        try:
            if not os.path.exists(self.project_folder_path):
                raise FolderNotFoundException("[ERROR] Project folder not foound. PATH: {0}".format(
                    self.project_folder_path))
            else:
                self.static_folder_path = '{0}/static'.format(self.project_folder_path)
                if not os.path.exists(self.static_folder_path):
                    print("Creating the Static folder in the PATH: {0}".format(
                        self.static_folder_path))
                    os.mkdir(self.static_folder_path)
                try:
                    os.chdir(self.static_folder_path)
                    self.semantic_files_path = '{0}/dsu'.format(self.static_folder_path)
                    if not os.path.exists(self.semantic_files_path):
                        print("Creating the django_semantic_ui folder on PATH: {0}".format(
                            self.semantic_files_path))
                        os.mkdir(self.semantic_files_path)
                    print("Semantic UI Folder: {0}".format(self.semantic_files_path))
                except:
                    raise FolderNotFoundException("[ERROR] Static folder not found")
        except Exception as e:
            raise SemanticUIException(e)

    def install(self):
        """
        Install Gulp and Semantic UI Framework on a django project
        :return: None
        """
        print("Installing started...")
        self.move_static_folder()
        try:
            print("Moving to static folder...")
            os.chdir(self.semantic_files_path)
            package_json_path = '{0}/package.json'.format(self.static_folder_path)
            file_exists = os.path.isfile(package_json_path)
            if not file_exists:
                print("Generationg the package.json file...")
                os.system('npm init')
                print("Package.json file has been generated successfully!.")
            print("Installing gulp...")
            os.system('npm install gulp@{0} --save-dev'.format(self.gulp_version))
            print("Gulp module has been installed successfully (Version installed {0})".format(
                self.gulp_version))
            print("Installing semantic-ui module...")
            os.system('npm install semantic-ui@{0} --save'.format(self.semantic_ui_version))
            print("Semantic-ui module has been installed successfully (Version installed {0}).".format(
                self.semantic_ui_version))
            print('Indexing "semantic-ui" on your django project path: {0}'.format(settings.BASE_DIR))
            print("Indexing modules...")
            time.sleep(self.SECONDS_WAITING_ALLOWED)
            os.chdir(settings.BASE_DIR)
            time.sleep(5)
            print("Semantic UI  has been installed successfully...")
        except:
            raise SemanticUIException("[ERROR] Installing failed cannot move to static folder.")

    def uninstall(self):
        """
        Uninstall Gulp and Semantic UI framework from the django project.
        :return: None
        """
        print("Uninstalling started...")
        self.move_static_folder()
        try:
            print("Moving to Static folder...")
            os.chdir(self.semantic_files_path)
            print("Uninstalling Semantic UI module...")
            os.system('npm uninstall semantic-ui --save')
            print("Semantic UI module has been removed successfully!.")
            print("Uninstalling Gulp module...")
            os.system('npm uninstall gulp --save-dev')
            print("Gulp module has been removed successfully!.")
            print("Deleting semantic folder...")
            try:
                os.system('rm -rf {0}'.format(self.semantic_folder))
                print("Semantic folder has been removed successfully.")
                print("Deleting semantic.json file...")
                try:
                    os.system('rm -f semantic.json')
                    print("semantic.json file has been removed...")
                    print("Deleting package.json file...")
                    try:
                        os.system("rm -rf package*")
                        print("package.json file has been removed.")
                        node_modules_path = '{0}/node_modules'.format(self.semantic_files_path)
                        if not os.path.exists(node_modules_path):
                            raise FolderNotFoundException("[ERROR] node_modules folder not found. PATH: {0}".format(
                                node_modules_path))
                        else:
                            os.system("rm -rf node_modules")
                        print("Removing semantic folder...")
                        os.rmdir(self.semantic_files_path)
                        print("Semantic folder removed.")
                        print("Removing static folder...")
                        os.rmdir(self.static_folder_path)
                        print("Static folder removed.")
                        print('Indexing "semantic-ui" on your django project path: {0}'.format(settings.BASE_DIR))
                        print("Indexing modules...")
                        time.sleep(self.SECONDS_WAITING_ALLOWED)
                        os.chdir(settings.BASE_DIR)
                        time.sleep(5)
                        print("Semantic UI has been removed successfully!")
                    except:
                        raise SemanticUIException("[ERROR] Semantic JSON file not exists.")
                except:
                    raise SemanticUIException("[ERROR] Semantic JSON file not exists.")
            except:
                raise SemanticUIException("[ERROR] Semantic folder not exists.")
        except:
            raise SemanticUIException("[ERROR] Uninstalling failed cannot move to static folder.")

    def build(self):
        """
        Running Gulp build command inside semantic folder.
        :return: None
        """
        print("Gulp Build command started...")
        self.move_static_folder()
        print("Moving to Static folder...")
        os.chdir(self.static_folder_path)
        semantic_path = '{0}/{1}'.format(self.semantic_files_path, self.semantic_folder)
        try:
            print("Moving to Semantic folder...")
            os.chdir(semantic_path)
            print("Gulp build command executing...")
            os.system('gulp build')
            print('Indexing "semantic-ui" on your django project path: {0}'.format(settings.BASE_DIR))
            print("Indexing modules...")
            time.sleep(self.SECONDS_WAITING_ALLOWED)
            os.chdir(settings.BASE_DIR)
            time.sleep(5)
            print("Gulp build command completed successfully.")
        except:
            raise SemanticUIException(
                "[ERROR] Gulp build command failed, the semantic folder not exists or the name is incorrect.")
