#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from enaml.layout.layout_helpers import align, spacer, vbox, vertical
from enaml.styling import StyleSheet, Style, Setter
from enaml.widgets.container import Container
from enaml.widgets.separator import Separator


enamldef TaskDialogIconArea(Container):
    """ A custom container for use in a task dialog.

    User code can declare and instance of this element in the body of a
    TaskDialogBody in order to provide icon area content.

    """
    #: The default padding is set appropriate for common use cases.
    padding = (10, 0, 15, 10)

    #: The icon area resists clipping by default, since it is typically
    #: taller than the instruction and could otherwise lead to a system
    #: where the the instruction and icon compete to set the min height.
    resist_height = 'strong'

    #: Generate the layout constraints for the icon area. The icon area
    #: gets a default spacer so that it can expand to accomodate larger
    #: content areas.
    layout_constraints => ():
        if self.constraints:
            return self.constraints
        items = self.visible_widgets()
        items.append(spacer(0))
        return [vbox(*items)]


enamldef TaskDialogInstructionArea(Container):
    """ A custom container for use in a task dialog.

    User code can declare and instance of this element in the body of a
    TaskDialogBody in order to provide instruction area content.

    """
    #: The default padding is set appropriate for common use cases.
    padding = (10, 10, 15, 10)


enamldef TaskDialogContentArea(Container):
    """ A custom container for use in a task dialog.

    User code can declare and instance of this element in the body of a
    TaskDialogBody in order to provide content area content.

    """
    #: The default padding is set appropriate for common use cases.
    padding = (0, 10, 15, 10)


enamldef TaskDialogCommandArea(Container):
    """ A custom container for use in a task dialog.

    User code can declare and instance of this element in the body of a
    TaskDialogBody in order to provide command area content.

    """
    pass


enamldef TaskDialogDetailsArea(Container):
    """ A custom container for use in a task dialog.

    User code can declare and instance of this element in the body of a
    TaskDialogBody in order to provide details area content.

    """
    #: The default padding is set appropriate for common use cases.
    padding = (0, 10, 15, 10)

    # XXX ideally, this shouldn't be needed.
    visible :: parent.request_relayout()


enamldef TaskDialogFootnoteArea(Container):
    """ A custom container for use in a task dialog.

    User code can declare and instance of this element in the body of a
    TaskDialogBody in order to provide footnote area content.

    """
    pass


enamldef TaskDialogBody(Container):
    """ A custom container used to create a task dialog body.

    A TaskDialogBody element should be declared as the central widget
    of a Dialog widget. The element layout logic automatically arranges
    the dialog area containers declared as children.

    The dialog body supports the following area children, all of which
    are optional. User code should declare at most one of each type
    of child:

    - TaskDialogIconArea
    - TaskDialogInstructionArea
    - TaskDialogContentArea
    - TaskDialogDetailsArea
    - TaskDialogCommandArea
    - TaskDialogFootnoteArea

    No other widget types should be used as children of the dialog body.

    """
    #: The dialog body has no padding by default.
    padding = 0

    #: The dialog footnote separator. This is only made visible when
    #: using a dialog footnote area.
    Separator: separator:
        visible = False

    #: Generate the layout constraints for the dialog body. This method
    #: will setup a layout which is appropriate for the task dialog area
    #: children defined for the dialog body. The 'constraints' attribute
    #: is ignored entirely.
    layout_constraints => ():
        icon = inst = ctnt = dtls = cmnd = ftnt = None
        for item in self.visible_widgets():
            if isinstance(item, TaskDialogIconArea):
                icon = item
            elif isinstance(item, TaskDialogInstructionArea):
                inst = item
            elif isinstance(item, TaskDialogContentArea):
                ctnt = item
            elif isinstance(item, TaskDialogCommandArea):
                cmnd = item
            elif isinstance(item, TaskDialogDetailsArea):
                dtls = item
            elif isinstance(item, TaskDialogFootnoteArea):
                ftnt = item

        if ftnt is not None:
            separator.visible = True
            sepr = separator
        else:
            separator.visible = False
            sepr = None

        t = self.contents_top
        r = self.contents_right
        b = self.contents_bottom
        l = self.contents_left
        cns = [
            vertical(t, inst, ctnt, dtls, cmnd, sepr, ftnt, b, spacing=0),
            align('right', r, inst, ctnt, dtls, cmnd, sepr, ftnt),
            align('left', l, cmnd, sepr, ftnt),
            align('left', inst, ctnt, dtls),
        ]

        if icon is None:
            cns.append(align('left', l, inst or ctnt or dtls))
        else:
            cns.extend([
                vertical(t, icon, cmnd, spacing=0),
                align('left', l, icon),
                align('left', icon.right, inst or ctnt or dtls),
            ])

        return cns


enamldef TaskDialogStyleSheet(StyleSheet):
    """ A style sheet which provides basic styling for a task dialog.

    This stylesheet can be declared, and optionally extended, in the
    body of a Dialog which contains a TaskDialogBody as its central
    widget. It provides a styling theme which is consistent with the
    look and feel of Windows 7/8 task dialogs.

    """
    Style:
        element = ('TaskDialogIconArea, TaskDialogInstructionArea, '
                   'TaskDialogContentArea, TaskDialogDetailsArea')
        Setter:
            field = 'background'
            value = 'white'
    Style:
        element = 'TaskDialogCommandArea'
        Setter:
            field = 'border-top'
            value = '1px solid #D9D9D9'
    Style:
        element = 'Label'
        style_class = 'task-dialog-instructions'
        Setter:
            field = 'font'
            value = '12pt "Segoe UI"'
        Setter:
            field = 'color'
            value = '#003399'
    Style:
        element = 'Label'
        style_class = 'task-dialog-content'
        Setter:
            field = 'font'
            value = '9pt "Segoe UI"'
