# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

__all__ = ('get_tasks_itf',)

from ulib.base.uio import _u, _s
from ulib.tasks import TasksCtl
from taskscli import TasksCLI

def get_cmd_name(args, required=False):
    cmd_name = args[0:1] and args[0] or None
    if required and cmd_name is None:
        raise ValueError("You must specify a command")
    return cmd_name, args[1:]

def get_httpd():
    import httpd.server
    return httpd.server.Server

def get_httpstore():
    import httpstore.server
    return httpstore.server.Server

DEFAULT_CLASS_GETTER = lambda: TasksCLI
CLASSES_GETTERS = {'httpd': get_httpd,
                   'httpstore': get_httpstore,
                   }
def get_tasks_cls(args):
    cmd_name, args = get_cmd_name(args)
    if cmd_name is None: cls_getter = None
    else: cls_getter = CLASSES_GETTERS.get(cmd_name, DEFAULT_CLASS_GETTER)
    if cls_getter is None: cls = None
    else: cls = cls_getter()
    return cls, cmd_name, args

def get_tasks_itf(args, ctl=None):
    cls, cmd_name, args = get_tasks_cls(args)
    if cls is None: itf = None
    else: itf = cls(ctl=ctl)
    return itf, cmd_name, args
