import functools

import aiobtclientapi

from . import constants, trackers, utils

defaults = {
    'config': {
        'main': {
            'cache_directory': utils.configfiles.config_value(
                value=constants.DEFAULT_CACHE_DIRECTORY,
                description='Where to store generated files.',
            ),
            'max_cache_size': utils.configfiles.config_value(
                value=utils.types.Bytes.from_string('20 MB'),
                description=(
                    'Maximum size of cache directory. '
                    'Units like "kB" and "MiB" are interpreted.'
                ),
            ),
        },
        'torrent-create': {
            'reuse_torrent_paths': utils.configfiles.config_value(
                value=[],
                description=(
                    'List of directories to search for a *.torrent file '
                    'to get piece hashes from instead of generating the '
                    'pieces hashes from file contents.\n'
                ),
            ),
        },
    },

    'trackers': {
        tracker.name: tracker.TrackerConfig()
        for tracker in trackers.trackers()
    },

    'imghosts': {
        imghost.name: imghost.default_config
        for imghost in utils.imghosts.imghosts()
    },

    'clients': {
        client.name: {
            'url': client.URL.default,
            'username': '',
            'password': '',
            'check_after_add': utils.types.Bool('no'),
        }
        for client in aiobtclientapi.api_classes()
    },
}
"""Defaults for configuration options"""

# Transmission cannot add torrent without verifying it
defaults['clients']['transmission']['check_after_add'] = utils.types.Bool('yes')


@functools.lru_cache(maxsize=None)
def option_paths():
    """Tuple of configuration option paths (`<section>.<subsection>.<option>`)"""
    return utils.configfiles.ConfigFiles(defaults).paths


def option_type(option_path):
    filename, section, option = option_path.split('.', maxsplit=2)
    return type(defaults[filename][section][option])
