import logging
import os
import shutil
import threading
import concurrent.futures

from src.engines.blender.blender_engine import Blender
from src.engines.ffmpeg.ffmpeg_engine import FFMPEG
from src.utilities.misc_helper import system_safe_path, current_system_os, current_system_cpu

logger = logging.getLogger()


class EngineManager:
    """Class that manages different versions of installed render engines and handles fetching and downloading new versions,
    if possible.
    """

    engines_path = None
    download_tasks = []

    @staticmethod
    def supported_engines():
        return [Blender, FFMPEG]

    @classmethod
    def downloadable_engines(cls):
        return [engine for engine in cls.supported_engines() if hasattr(engine, "downloader") and engine.downloader()]

    @classmethod
    def active_downloads(cls) -> list:
        return [x for x in cls.download_tasks if x.is_alive()]

    @classmethod
    def engine_with_name(cls, engine_name):
        for obj in cls.supported_engines():
            if obj.name().lower() == engine_name.lower():
                return obj

    @classmethod
    def update_all_engines(cls):
        for engine in cls.downloadable_engines():
            update_available = cls.is_engine_update_available(engine)
            if update_available:
                update_available['name'] = engine.name()
                cls.download_engine(engine.name(), update_available['version'], background=True)

    @classmethod
    def get_engines(cls, filter_name=None, include_corrupt=False, ignore_system=False):

        if not cls.engines_path:
            raise FileNotFoundError("Engine path is not set")

        # Parse downloaded engine directory
        results = []
        try:
            all_items = os.listdir(cls.engines_path)
            all_directories = [item for item in all_items if os.path.isdir(os.path.join(cls.engines_path, item))]
            keys = ["engine", "version", "system_os", "cpu"]  # Define keys for result dictionary

            for directory in all_directories:
                # Split directory name into segments
                segments = directory.split('-')
                # Create a dictionary mapping keys to corresponding segments
                result_dict = {keys[i]: segments[i] for i in range(min(len(keys), len(segments)))}
                result_dict['type'] = 'managed'

                # Initialize binary_name with engine name
                binary_name = result_dict['engine'].lower()
                # Determine the correct binary name based on the engine and system_os
                eng = cls.engine_with_name(result_dict['engine'])
                binary_name = eng.binary_names.get(result_dict['system_os'], binary_name)

                # Find the path to the binary file
                path = next(
                    (os.path.join(root, binary_name) for root, _, files in
                     os.walk(system_safe_path(os.path.join(cls.engines_path, directory))) if binary_name in files),
                    None
                )
                result_dict['path'] = path

                # fetch version number from binary - helps detect corrupted downloads - disabled due to perf issues
                # binary_version = eng(path).version()
                # if not binary_version:
                #     logger.warning(f"Possible corrupt {eng.name()} {result_dict['version']} install detected: {path}")
                #     if not include_corrupt:
                #         continue
                # result_dict['version'] = binary_version or 'error'

                # Add the result dictionary to results if it matches the filter_name or if no filter is applied
                if not filter_name or filter_name == result_dict['engine']:
                    results.append(result_dict)
        except FileNotFoundError as e:
            logger.warning(f"Cannot find local engines download directory: {e}")

        # add system installs to this list - use bg thread because it can be slow
        def fetch_engine_details(eng, include_corrupt=False):
            version = eng().version()
            if not version and not include_corrupt:
                return

            return {
                'engine': eng.name(),
                'version': version or 'error',
                'system_os': current_system_os(),
                'cpu': current_system_cpu(),
                'path': eng.default_engine_path(),
                'type': 'system'
            }

        if not ignore_system:
            with concurrent.futures.ThreadPoolExecutor() as executor:
                futures = {
                    executor.submit(fetch_engine_details, eng, include_corrupt): eng.name()
                    for eng in cls.supported_engines()
                    if eng.default_engine_path() and (not filter_name or filter_name == eng.name())
                }

                for future in concurrent.futures.as_completed(futures):
                    result = future.result()
                    if result:
                        results.append(result)

        return results

    @classmethod
    def all_versions_for_engine(cls, engine_name, include_corrupt=False, ignore_system=False):
        versions = cls.get_engines(filter_name=engine_name, include_corrupt=include_corrupt, ignore_system=ignore_system)
        sorted_versions = sorted(versions, key=lambda x: x['version'], reverse=True)
        return sorted_versions

    @classmethod
    def newest_engine_version(cls, engine, system_os=None, cpu=None, ignore_system=None):
        system_os = system_os or current_system_os()
        cpu = cpu or current_system_cpu()

        try:
            filtered = [x for x in cls.all_versions_for_engine(engine, ignore_system=ignore_system)
                        if x['system_os'] == system_os and x['cpu'] == cpu]
            return filtered[0]
        except IndexError:
            logger.error(f"Cannot find newest engine version for {engine}-{system_os}-{cpu}")
        return None

    @classmethod
    def is_version_downloaded(cls, engine, version, system_os=None, cpu=None, ignore_system=False):
        system_os = system_os or current_system_os()
        cpu = cpu or current_system_cpu()

        filtered = [x for x in cls.get_engines(filter_name=engine, ignore_system=ignore_system) if
                    x['system_os'] == system_os and x['cpu'] == cpu and x['version'] == version]
        return filtered[0] if filtered else False

    @classmethod
    def version_is_available_to_download(cls, engine, version, system_os=None, cpu=None):
        try:
            downloader = cls.engine_with_name(engine).downloader()
            return downloader.version_is_available_to_download(version=version, system_os=system_os, cpu=cpu)
        except Exception as e:
            logger.debug(f"Exception in version_is_available_to_download: {e}")
            return None

    @classmethod
    def find_most_recent_version(cls, engine=None, system_os=None, cpu=None, lts_only=False):
        try:
            downloader = cls.engine_with_name(engine).downloader()
            return downloader.find_most_recent_version(system_os=system_os, cpu=cpu)
        except Exception as e:
            logger.debug(f"Exception in find_most_recent_version: {e}")
            return None

    @classmethod
    def get_existing_download_task(cls, engine, version, system_os=None, cpu=None):
        for task in cls.download_tasks:
            task_parts = task.name.split('-')
            task_engine, task_version, task_system_os, task_cpu = task_parts[:4]

            if engine == task_engine and version == task_version:
                if system_os in (task_system_os, None) and cpu in (task_cpu, None):
                    return task
        return None

    @classmethod
    def download_engine(cls, engine, version, system_os=None, cpu=None, background=False, ignore_system=False):

        engine_to_download = cls.engine_with_name(engine)
        existing_task = cls.get_existing_download_task(engine, version, system_os, cpu)
        if existing_task:
            logger.debug(f"Already downloading {engine} {version}")
            if not background:
                existing_task.join()  # If download task exists, wait until it's done downloading
            return None
        elif not engine_to_download.downloader():
            logger.warning("No valid downloader for this engine. Please update this software manually.")
            return None
        elif not cls.engines_path:
            raise FileNotFoundError("Engines path must be set before requesting downloads")

        thread = EngineDownloadWorker(engine, version, system_os, cpu)
        cls.download_tasks.append(thread)
        thread.start()

        if background:
            return thread

        thread.join()
        found_engine = cls.is_version_downloaded(engine, version, system_os, cpu, ignore_system)  # Check that engine downloaded
        if not found_engine:
            logger.error(f"Error downloading {engine}")
        return found_engine

    @classmethod
    def delete_engine_download(cls, engine, version, system_os=None, cpu=None):
        logger.info(f"Requested deletion of engine: {engine}-{version}")

        found = cls.is_version_downloaded(engine, version, system_os, cpu)
        if found and found['type'] == 'managed':  # don't delete system installs
            # find the root directory of the engine executable
            root_dir_name = '-'.join([engine, version, found['system_os'], found['cpu']])
            remove_path = os.path.join(found['path'].split(root_dir_name)[0], root_dir_name)
            # delete the file path
            logger.info(f"Deleting engine at path: {remove_path}")
            shutil.rmtree(remove_path, ignore_errors=False)
            logger.info(f"Engine {engine}-{version}-{found['system_os']}-{found['cpu']} successfully deleted")
            return True
        elif found:  # these are managed by the system / user. Don't delete these.
            logger.error(f'Cannot delete requested {engine} {version}. Managed externally.')
        else:
            logger.error(f"Cannot find engine: {engine}-{version}")
        return False

    @classmethod
    def is_engine_update_available(cls, engine_class, ignore_system_installs=False):
        logger.debug(f"Checking for updates to {engine_class.name()}")
        latest_version = engine_class.downloader().find_most_recent_version()

        if not latest_version:
            logger.warning(f"Could not find most recent version of {engine_class.name()} to download")
            return None

        version_num = latest_version.get('version')
        if cls.is_version_downloaded(engine_class.name(), version_num, ignore_system=ignore_system_installs):
            logger.debug(f"Latest version of {engine_class.name()} ({version_num}) already downloaded")
            return None

        return latest_version


    @classmethod
    def create_worker(cls, engine_name, input_path, output_path, engine_version=None, args=None, parent=None, name=None):

        worker_class = cls.engine_with_name(engine_name).worker_class()

        # check to make sure we have versions installed
        all_versions = cls.all_versions_for_engine(engine_name)
        if not all_versions:
            raise FileNotFoundError(f"Cannot find any installed '{engine_name}' engines")

        # Find the path to the requested engine version or use default
        engine_path = None
        if engine_version and engine_version != 'latest':
            for ver in all_versions:
                if ver['version'] == engine_version:
                    engine_path = ver['path']
                    break

            # Download the required engine if not found locally
            if not engine_path:
                download_result = cls.download_engine(engine_name, engine_version)
                if not download_result:
                    raise FileNotFoundError(f"Cannot download requested version: {engine_name} {engine_version}")
                engine_path = download_result['path']
                logger.info("Engine downloaded. Creating worker.")
        else:
            logger.debug(f"Using latest engine version ({all_versions[0]['version']})")
            engine_path = all_versions[0]['path']

        if not engine_path:
            raise FileNotFoundError(f"Cannot find requested engine version {engine_version}")

        return worker_class(input_path=input_path, output_path=output_path, engine_path=engine_path, args=args,
                            parent=parent, name=name)

    @classmethod
    def engine_for_project_path(cls, path):
        _, extension = os.path.splitext(path)
        extension = extension.lower().strip('.')
        for engine in cls.supported_engines():
            if extension in engine().supported_extensions():
                return engine
        undefined_renderer_support = [x for x in cls.supported_engines() if not x().supported_extensions()]
        return undefined_renderer_support[0]


class EngineDownloadWorker(threading.Thread):
    """A thread worker for downloading a specific version of a rendering engine.

    This class handles the process of downloading a rendering engine in a separate thread,
    ensuring that the download process does not block the main application.

    Attributes:
        engine (str): The name of the rendering engine to download.
        version (str): The version of the rendering engine to download.
        system_os (str, optional): The operating system for which to download the engine. Defaults to current OS type.
        cpu (str, optional): Requested CPU architecture. Defaults to system CPU type.
    """
    def __init__(self, engine, version, system_os=None, cpu=None):
        super().__init__()
        self.engine = engine
        self.version = version
        self.system_os = system_os
        self.cpu = cpu
        self.percent_complete = 0

    def _update_progress(self, current_progress):
        self.percent_complete = current_progress

    def run(self):
        try:
            existing_download = EngineManager.is_version_downloaded(self.engine, self.version, self.system_os, self.cpu,
                                                                    ignore_system=True)
            if existing_download:
                logger.info(f"Requested download of {self.engine} {self.version}, but local copy already exists")
                return existing_download

            # Get the appropriate downloader class based on the engine type
            downloader = EngineManager.engine_with_name(self.engine).downloader()
            downloader.download_engine( self.version, download_location=EngineManager.engines_path,
                                        system_os=self.system_os, cpu=self.cpu, timeout=300, progress_callback=self._update_progress)
        except Exception as e:
            logger.error(f"Error in download worker: {e}")
        finally:
            # remove itself from the downloader list
            EngineManager.download_tasks.remove(self)


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # print(EngineManager.newest_engine_version('blender', 'macos', 'arm64'))
    # EngineManager.delete_engine_download('blender', '3.2.1', 'macos', 'a')
    EngineManager.engines_path = "/Users/brettwilliams/zordon-uploads/engines"
    # print(EngineManager.is_version_downloaded("ffmpeg", "6.0"))
    print(EngineManager.get_engines())
