from builtins import map
from logging import getLogger

import pandas as pd

from dtale.cli.clickutils import get_loader_options

logger = getLogger(__name__)

'''
  IMPORTANT!!! This global variable is required for building any customized CLI loader.
  When find loaders on startup it will search for any modules containing the global variable LOADER_KEY.
'''
NOW = pd.Timestamp('now').strftime('%Y%m%d')
LOADER_KEY = 'arctic'
LOADER_PROPS = [
    dict(name='host', help='arctic hostname'),
    dict(name='library', help='library within --arctic-host'),
    dict(name='node', help='node within --arctic-library'),
    dict(name='start', help='start-date of range to load if reading from ChunkStore (EX: {})'.format(NOW)),
    dict(name='end', help='end-date of range to load if reading from ChunkStore (EX: {})'.format(NOW)),
]


# IMPORTANT!!! This function is required for building any customized CLI loader.
def find_loader(kwargs):
    """
    Arctic implementation of data loader which will return a function if any of the
    `click` options based on LOADER_KEY & LOADER_PROPS have been used, otherwise return None

    :param kwargs: Optional keyword arguments to be passed from `click`
    :return: data loader function for arctic implementation
    """
    arctic_opts = get_loader_options(LOADER_KEY, kwargs)
    if len([f for f in arctic_opts.values() if f]):
        try:
            from arctic import Arctic
            from arctic.store.versioned_item import VersionedItem
        except ImportError:
            raise ImportError('In order to use the --arctic loader you must install arctic!')

        def _arctic_loader():
            host = Arctic(arctic_opts['host'])
            lib = host.get_library(arctic_opts['library'])
            read_kwargs = {}
            start, end = map(arctic_opts.get, ['start', 'end'])
            if start and end:
                read_kwargs['chunk_range'] = pd.date_range(start, end)
            data = lib.read(arctic_opts['node'], **read_kwargs)
            if isinstance(data, VersionedItem):
                data = data.data
            return data

        return _arctic_loader
    return None
