# -*- coding: utf-8 -*-

from riot.app import quit_app, run_tag
from riot.tags.style import parse_style
from riot.tags.tags import parse_tag_from_node
from riot.tags.utils import convert_string_to_node
from riot.virtual_dom import define_tag, mount

todo = define_tag('todo', '''<todo>
  <pile>
    <text>{ title }</text>
    <edit name="input" />
    <button label="Add #{ next_count() }" onclick="{ add }" />
    <button label="X" onclick="{ remove_all_done }" />
    <button label="Exit" onclick="{ exit }">
  </pile>
  <script>
    def init(self, opts):
        self.items = opts['items']
        self.title = opts['title']

    def edit(self, e):
        self.text = e.target.value

    def add(self, e):
        if self.text:
            self.items.append(dict(title=self.text, done=False, hidden=False))
            self.text = self.input.value = ''

    def remove_all_done(self, e):
        self.items = [item for item in self.items if not item.get('done')]

    def not_hidden_items(self):
        return [item for item in self.items if not item.get('hidden')]

    def count(self):
        return len(self.not_hidden_items())

    def next_count(self):
        return self.count() + 1

    def toggle(self, e):
        e.item['done'] = not e.item.get('done')
        return True
  </script>
</todo>''')


root = convert_string_to_node('<filler><todo /></filler>')

mount(root, 'todo', 'todo', {
    'title': 'I want to behave!',
    'items': [
        {'title': 'Avoid excessive coffeine', 'done': True, 'hidden': False},
        {'title': 'Hidden item', 'done': True, 'hidden': True},
        {'title': 'Be less provocative', 'done': False, 'hidden': False},
        {'title': 'Be nice to people', 'done': False, 'hidden': False}
    ]
})

app = parse_tag_from_node(root)

run_tag(app)

@@ -282,20 +282,23 @@
 odo%3E%0A  %3C
-pile
+listbox
 %3E%0A    %3Ct
@@ -318,16 +318,85 @@
 %3C/text%3E%0A
+    %3Ccheckbox label=%22%7B title %7D%22 state=%22%7B done %7D%22 each=%22%7B items %7D%22 /%3E%0A
     %3Cedi
@@ -584,12 +584,15 @@
   %3C/
-pile
+listbox
 %3E%0A  

Add checkbox each item.