# Path: alvi/client/scenes/base.py
# class Scene(api.BaseScene):
#     """base class for container based scenes"""
#     class Form(forms.Form):
#         pass
# 
#     @classmethod
#     def run_wrapper(cls, scene, pipe, **kwargs):
#         container = cls.container_class()(pipe)
#         kwargs['container'] = container
#         scene.run(**kwargs)
# 
#     @classmethod
#     def container_name(cls):
#         return cls.container_class().name()
# 
#     @classmethod
#     @abc.abstractmethod
#     def container_class(cls):
#         raise NotImplementedError
# 
#     def test(self, container, test_case):
#         logger.warning("skipping test for %s" % self.__class__.__name__)
# 
# Path: alvi/client/containers/tree.py
# class Tree(base.Container):
#     @property
#     def root(self):
#         try:
#             return self._root
#         except AttributeError:
#             return None
# 
#     def create_root(self, value):
#         if self.root:
#             raise RuntimeError("Cannot set root more that once")
#         self._root = Node(self, None, value)
#         return self._root


from alvi.client.scenes.base import Scene
from alvi.client.containers.tree import Tree
from django import forms


class TreeCreateNode(Scene):
    class Form(Scene.Form):
        parents = forms.CharField(initial="0, 0, 1, 1, 4, 4")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.nodes = []

    def run(self, **kwargs):
        tree = kwargs['container']
        data_generator = kwargs['data_generator']
        options = kwargs['options']
        parents = [int(parent) for parent in options['parents'].split(',')]
        value = next(data_generator.values)
        node = tree.create_root(value=value)
        self.nodes.append(node)
        #parents = [0, 0, 1, 1, 4, 4]  # TODO this list could be passed from test case by custom form field
        for value, parent in zip(data_generator.values, parents):
            node = self.nodes[parent].children.create(value=value)
            self.nodes.append(node)
        tree.sync()

    @classmethod
    def container_class(cls):

# Path: alvi/client/scenes/base.py
# class Scene(api.BaseScene):
#     """base class for container based scenes"""
#     class Form(forms.Form):
#         pass
# 
#     @classmethod
#     def run_wrapper(cls, scene, pipe, **kwargs):
#         container = cls.container_class()(pipe)
#         kwargs['container'] = container
#         scene.run(**kwargs)
# 
#     @classmethod
#     def container_name(cls):
#         return cls.container_class().name()
# 
#     @classmethod
#     @abc.abstractmethod
#     def container_class(cls):
#         raise NotImplementedError
# 
#     def test(self, container, test_case):
#         logger.warning("skipping test for %s" % self.__class__.__name__)
# 
# Path: alvi/client/containers/tree.py
# class Tree(base.Container):
#     @property
#     def root(self):
#         try:
#             return self._root
#         except AttributeError:
#             return None
# 
#     def create_root(self, value):
#         if self.root:
#             raise RuntimeError("Cannot set root more that once")
#         self._root = Node(self, None, value)
#         return self._root


# Path: alvi/tests/resources/client/local_python_client/scenes/tree/create_node.py
from alvi.client.scenes.base import Scene
from alvi.client.containers.tree import Tree
from django import forms



class TreeCreateNode(Scene):
    class Form(Scene.Form):
        parents = forms.CharField(initial="0, 0, 1, 1, 4, 4")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.nodes = []

    def run(self, **kwargs):
        tree = kwargs['container']
        data_generator = kwargs['data_generator']
        options = kwargs['options']
        parents = [int(parent) for parent in options['parents'].split(',')]
        value = next(data_generator.values)
        node = tree.create_root(value=value)
        self.nodes.append(node)
        #parents = [0, 0, 1, 1, 4, 4]  # TODO this list could be passed from test case by custom form field
        for value, parent in zip(data_generator.values, parents):
            node = self.nodes[parent].children.create(value=value)
            self.nodes.append(node)
        tree.sync()

    @classmethod
    def container_class(cls):

        return Tree