# Path: sen/tui/widgets/list/util.py
# class RowWidget(UnselectableRowWidget):
#     def selectable(self):
#         return True
# 
# Path: sen/tui/widgets/util.py
# class SelectableText(ColorTextMixin, AdHocAttrMap):
#     def __init__(self, text, maps=None):
#         maps = maps or get_map()
#         super().__init__(urwid.Text(text, align="left", wrap="clip"), maps)
# 
# Path: sen/tui/chunks/image.py
# def get_basic_image_markup(docker_image, with_size=False):
#     if isinstance(docker_image, RootImage):
#         return [str(docker_image)]
# 
#     text_markup = [docker_image.short_id]
# 
#     if docker_image.names:
#         text_markup.append(" ")
#         text_markup.append(("main_list_lg", docker_image.names[0].to_str()))
# 
#     c = docker_image.container_command or docker_image.comment
#     if c:
#         text_markup.append(" ")
#         text_markup.append(("main_list_ddg", c))
# 
#     if with_size:
#         text_markup.append(" ")
#         text_markup.append(("main_list_ddg", "(%s)" % humanize_bytes(docker_image.total_size)))
# 
#     return text_markup


import logging
import urwidtrees
from sen.tui.widgets.list.util import RowWidget
from sen.tui.widgets.util import SelectableText
from sen.tui.chunks.image import get_basic_image_markup
logger = logging.getLogger(__name__)


class TreeNodeWidget(SelectableText):
    def __init__(self, ui, docker_image):
        self.ui = ui
        self.docker_image = docker_image
        super().__init__(get_basic_image_markup(docker_image, with_size=True))


class TreeBackend(urwidtrees.Tree):
    # FIXME: rewrite to use SimpleTree instead (for sake of docker-1.10 changes: how does one
    #        index an image with id <missing>
    def __init__(self, docker_backend, ui):
        super().__init__()
        self.ui = ui
        self.root = docker_backend.scratch_image

    def __getitem__(self, pos):
        return RowWidget([TreeNodeWidget(self.ui, pos)])

    # Tree API
    def parent_position(self, pos):
        return pos.parent_image

    def first_child_position(self, pos):
        ch = pos.children
        if ch:
            return pos.children[0]
        else:

# Path: sen/tui/widgets/list/util.py
# class RowWidget(UnselectableRowWidget):
#     def selectable(self):
#         return True
# 
# Path: sen/tui/widgets/util.py
# class SelectableText(ColorTextMixin, AdHocAttrMap):
#     def __init__(self, text, maps=None):
#         maps = maps or get_map()
#         super().__init__(urwid.Text(text, align="left", wrap="clip"), maps)
# 
# Path: sen/tui/chunks/image.py
# def get_basic_image_markup(docker_image, with_size=False):
#     if isinstance(docker_image, RootImage):
#         return [str(docker_image)]
# 
#     text_markup = [docker_image.short_id]
# 
#     if docker_image.names:
#         text_markup.append(" ")
#         text_markup.append(("main_list_lg", docker_image.names[0].to_str()))
# 
#     c = docker_image.container_command or docker_image.comment
#     if c:
#         text_markup.append(" ")
#         text_markup.append(("main_list_ddg", c))
# 
#     if with_size:
#         text_markup.append(" ")
#         text_markup.append(("main_list_ddg", "(%s)" % humanize_bytes(docker_image.total_size)))
# 
#     return text_markup


# Path: sen/tui/widgets/tree.py
import logging
import urwidtrees
from sen.tui.widgets.list.util import RowWidget
from sen.tui.widgets.util import SelectableText
from sen.tui.chunks.image import get_basic_image_markup

logger = logging.getLogger(__name__)


class TreeNodeWidget(SelectableText):
    def __init__(self, ui, docker_image):
        self.ui = ui
        self.docker_image = docker_image
        super().__init__(get_basic_image_markup(docker_image, with_size=True))


class TreeBackend(urwidtrees.Tree):
    # FIXME: rewrite to use SimpleTree instead (for sake of docker-1.10 changes: how does one
    #        index an image with id <missing>
    def __init__(self, docker_backend, ui):
        super().__init__()
        self.ui = ui
        self.root = docker_backend.scratch_image

    def __getitem__(self, pos):
        return RowWidget([TreeNodeWidget(self.ui, pos)])

    # Tree API
    def parent_position(self, pos):
        return pos.parent_image

    def first_child_position(self, pos):
        ch = pos.children
        if ch:
            return pos.children[0]
        else:

            return None