#!/usr/bin/env python3
# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
#
# SPDX-License-Identifier: MIT

"""A demo that opens a Kivy window and exposes it as a text display via CoAP

Running this example requires a version of Kivy that has asyncio support merged
(see <https://github.com/kivy/kivy/pull/5241>).
"""

import asyncio

from aiocoap import Context, Message, CHANGED
from aiocoap.resource import Site, Resource, ObservableResource, WKCResource

from kivy.app import App
from kivy.lang.builder import Builder

from widgets_common.kivy_resource import *

kv = '''
BoxLayout:
    orientation: 'vertical'
    Button:
        id: btn
        text: 'Use CoAP discovery to write here or receive notifications \
of clicks.'
        background_normal: ''
        color: (0, 0, 0, 1)
'''

class CoAPDisplay(App):
    def build(self):
        return Builder.load_string(kv)

    async def main(self):
        site = Site()
        site.add_resource(['text'], Text(self.root.ids.btn))
        site.add_resource(['pressed'], PressState(self.root.ids.btn))
        site.add_resource(['.well-known', 'core'],
                WKCResource(site.get_resources_as_linkheader))

        self.context = await Context.create_server_context(site)

    def on_start(self):
        asyncio.get_event_loop().create_task(self.main())

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(CoAPDisplay().async_run())
