#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, 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.api import HSplitLayout, InsertItem
from enaml.widgets.api import Container, DockArea, DockItem, Html
from enaml.workbench.api import Extension, PluginManifest
from enaml.workbench.core.api import Command
from enaml.workbench.ui.api import ActionItem, MenuItem


print('Imported Persistent View!')


class PickableDockItem(DockItem):
    """ A custom pickable dock item class.

    """
    def __getstate__(self):
        """ Get the pickle state for the dock item.

        This method saves the necessary state for the dock items used
        in this example. Different applications will have different
        state saving requirements.

        The default __setstate__ method provided on the Atom base class
        provides sufficient unpickling behavior.

        """
        return {'name': self.name, 'title': self.title}


class PickableDockArea(DockArea):
    """ A custom pickable dock area class.

    """
    def get_save_items(self):
        """ Get the list of dock items to save with this dock area.

        """
        return [c for c in self.children if isinstance(c, PickableDockItem)]

    def __getstate__(self):
        """ Get the pickle state for the dock area.

        This method saves the necessary state for the dock area used
        in this example. Different applications will have different
        state saving requirements.

        """
        state = {
            'name': self.name,
            'layout': self.save_layout(),
            'items': self.get_save_items(),
        }
        return state

    def __setstate__(self, state):
        """ Restore the state of the dock area.

        """
        self.name = state['name']
        self.layout = state['layout']
        self.insert_children(None, state['items'])


enamldef Pane(PickableDockItem):
    """ A pickable dock item with default Html content.

    """
    Container:
        Html:
            source = '<h1><center>%s</center></h1>' % title


def create_new_area():
    """ Create a new pickable dock area with two child panes.

    """
    area = PickableDockArea(name='the_dock_area')
    Pane(area, name='first', title='Pane 0')
    Pane(area, name='second', title='Pane 1')
    area.layout = HSplitLayout('first', 'second')
    return area


def new_item(event):
    """ The handler for the 'sample.persistent.new_item' command.

    """
    ui = event.workbench.get_plugin('enaml.workbench.ui')
    area = ui.workspace.content.find('the_dock_area')
    count = len(area.dock_items())
    name = '_pane_%d' % count
    title = 'Pane %d' % count
    item = Pane(area, name=name, title=title)
    area.update_layout(InsertItem(item=item.name))


def destroy_items(event):
    """ The handler for the 'sample.persistent.destroy_items' command.

    """
    ui = event.workbench.get_plugin('enaml.workbench.ui')
    area = ui.workspace.content.find('the_dock_area')
    for item in area.dock_items():
        item.destroy()


enamldef PersistentManifest(PluginManifest):
    """ The manifest which is registered when the view is loaded.

    This manifest contributes extra menu items to the menu bar and
    new commands for manipulating the dock area items.

    """
    id = 'sample.persistent'
    Extension:
        id = 'commands'
        point = 'enaml.workbench.core.commands'
        Command:
            id = 'sample.persistent.new_item'
            handler = new_item
        Command:
            id = 'sample.persistent.destroy_items'
            handler = destroy_items
    Extension:
        id = 'actions'
        point = 'enaml.workbench.ui.actions'
        MenuItem:
            path = '/debug'
            label = 'Debug'
            after = 'file'
        MenuItem:
            path = '/options'
            label = 'Options'
        MenuItem:
            path = '/tools'
            label = 'Tools'
            before = 'workspace'
        ActionItem:
            path = '/debug/something'
            label = 'Something'
        ActionItem:
            path = '/options/something_else'
            label = 'Something Else'
        ActionItem:
            path = '/tools/destroy_items'
            label = 'Destroy Items'
            shortcut = 'Ctrl+X'
            command = 'sample.persistent.destroy_items'
        ActionItem:
            path = '/file/new'
            label = 'New'
            shortcut = 'Ctrl+N'
            group = 'user'
            command = 'sample.persistent.new_item'
