import os
import yaml
from src.utilities.misc_helper import current_system_os, copy_directory_contents


class Config:
    # Initialize class variables with default values
    upload_folder = "~/zordon-uploads/"
    update_engines_on_launch = True
    max_content_path = 100000000
    server_log_level = 'debug'
    log_buffer_length = 250
    worker_process_timeout = 120
    flask_log_level = 'error'
    flask_debug_enable = False
    queue_eval_seconds = 1
    port_number = 8080
    enable_split_jobs = True
    download_timeout_seconds = 120

    @classmethod
    def load_config(cls, config_path):
        with open(config_path, 'r') as ymlfile:
            cfg = yaml.safe_load(ymlfile)

        cls.upload_folder = os.path.expanduser(cfg.get('upload_folder', cls.upload_folder))
        cls.update_engines_on_launch = cfg.get('update_engines_on_launch', cls.update_engines_on_launch)
        cls.max_content_path = cfg.get('max_content_path', cls.max_content_path)
        cls.server_log_level = cfg.get('server_log_level', cls.server_log_level)
        cls.log_buffer_length = cfg.get('log_buffer_length', cls.log_buffer_length)
        cls.worker_process_timeout = cfg.get('worker_process_timeout', cls.worker_process_timeout)
        cls.flask_log_level = cfg.get('flask_log_level', cls.flask_log_level)
        cls.flask_debug_enable = cfg.get('flask_debug_enable', cls.flask_debug_enable)
        cls.queue_eval_seconds = cfg.get('queue_eval_seconds', cls.queue_eval_seconds)
        cls.port_number = cfg.get('port_number', cls.port_number)
        cls.enable_split_jobs = cfg.get('enable_split_jobs', cls.enable_split_jobs)
        cls.download_timeout_seconds = cfg.get('download_timeout_seconds', cls.download_timeout_seconds)

    @classmethod
    def config_dir(cls):
        # Set up the config path
        if current_system_os() == 'macos':
            local_config_path = os.path.expanduser('~/Library/Application Support/Zordon')
        elif current_system_os() == 'windows':
            local_config_path = os.path.join(os.environ['APPDATA'], 'Zordon')
        else:
            local_config_path = os.path.expanduser('~/.config/Zordon')
        return local_config_path

    @classmethod
    def setup_config_dir(cls):
        # Set up the config path
        local_config_dir = cls.config_dir()
        if os.path.exists(local_config_dir):
            return

        try:
            # Create the local configuration directory
            os.makedirs(local_config_dir)

            # Determine the template path
            resource_environment_path = os.environ.get('RESOURCEPATH')
            if resource_environment_path:
                template_path = os.path.join(resource_environment_path, 'config')
            else:
                template_path = os.path.join(
                    os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), 'config')

            # Copy contents from the template to the local configuration directory
            copy_directory_contents(template_path, local_config_dir)

        except Exception as e:
            print(f"An error occurred while setting up the config directory: {e}")
            raise
