from kivy.lang import Builder
from kivy.factory import Factory
from kivy.clock import Clock
import electrum.lightning as lightning

Builder.load_string('''
<LightningChannelItem@CardItem>
    channelId: '<channelId not set>'
    Label:
        text: root.channelId

<LightningChannelsDialog@Popup>:
    name: 'lightning_channels'
    BoxLayout:
        orientation: 'vertical'
        spacing: '1dp'
        ScrollView:
            GridLayout:
                cols: 1
                id: lightning_channels_container
                size_hint: 1, None
                height: self.minimum_height
                spacing: '2dp'
                padding: '12dp'
''')

class LightningChannelsDialog(Factory.Popup):
    def __init__(self, app):
        super(LightningChannelsDialog, self).__init__()
        self.clocks = []
        self.app = app
    def open(self, *args, **kwargs):
        super(LightningChannelsDialog, self).open(*args, **kwargs)
        for i in self.clocks: i.cancel()
        self.clocks.append(Clock.schedule_interval(self.fetch_channels, 10))
        self.app.wallet.network.lightningrpc.subscribe(self.rpc_result_handler)
    def dismiss(self, *args, **kwargs):
        super(LightningChannelsDialog, self).dismiss(*args, **kwargs)
        self.app.wallet.network.lightningrpc.clearSubscribers()
    def fetch_channels(self, dw):
        lightning.lightningCall(self.app.wallet.network.lightningrpc, "listchannels")()
    def rpc_result_handler(self, methodName, res):
        print("got result", methodName)
        if isinstance(res, Exception):
            raise res
        channel_cards = self.ids.lightning_channels_container
        channel_cards.clear_widgets()
        for i in res["channels"]:
            item = Factory.LightningChannelItem()
            item.screen = self
            print(i)
            item.channelId = i["chan_id"]
            channel_cards.add_widget(item)

@@ -1,16 +1,32 @@
+import binascii%0A
 from kivy.lang i
@@ -139,16 +139,75 @@
 ightning
+%0Afrom electrum_gui.kivy.uix.context_menu import ContextMenu
 %0A%0ABuilde
@@ -256,16 +256,34 @@
 rdItem%3E%0A
+    active: False%0A
     chan
@@ -285,18 +285,21 @@
  channel
-Id
+Point
 : '%3Cchan
@@ -301,18 +301,21 @@
 %3Cchannel
-Id
+Point
  not set
@@ -354,18 +354,21 @@
 .channel
-Id
+Point
 %0A%0A%3CLight
@@ -432,32 +432,48 @@
 %0A    BoxLayout:%0A
+        id: box%0A
         orientat
@@ -955,16 +955,636 @@
 p = app%0A
+        self.context_menu = None%0A%0A    def close_channel(self, obj):%0A        print(%22asked to close channel%22, obj.channelPoint)%0A        lightning.lightningCall(self.app.wallet.network.lightningrpc, %22closechannel%22)(obj.channelPoint + (%22 --force%22 if not obj.active else %22%22))%0A%0A    def show_menu(self, obj):%0A        self.hide_menu()%0A        self.context_menu = ContextMenu(obj, %5B(%22Close%22, self.close_channel)%5D)%0A        self.ids.box.add_widget(self.context_menu)%0A%0A    def hide_menu(self):%0A        if self.context_menu is not None:%0A            self.ids.box.remove_widget(self.context_menu)%0A            self.context_menu = None%0A%0A
     def 
@@ -1877,16 +1877,17 @@
 andler)%0A
+%0A
     def 
@@ -1910,32 +1910,57 @@
 rgs, **kwargs):%0A
+        self.hide_menu()%0A
         super(Li
@@ -2073,24 +2073,25 @@
 bscribers()%0A
+%0A
     def fetc
@@ -2200,16 +2200,17 @@
 els%22)()%0A
+%0A
     def 
@@ -2617,23 +2617,161 @@
 nnel
-Id = i%5B%22chan_id
+Point = binascii.hexlify(bytes(reversed(bytes(bytearray.fromhex(i%5B%22channel_point%22%5D.split(%22:%22)%5B0%5D))))).decode(%22ascii%22)%0A            item.active = i%5B%22active
 %22%5D%0A 

fix kivy channel close