# -*- coding: utf-8 -*-
#!/usr/bin/env python
from gi.repository import Gtk
import bluetooth

#insert here the address of the BT module that you noted earlier
bd_addr = "00:XX:XX:XX:12:80"
#port must be consistent with server
port = 1

class AppWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Availability Indicator")
        self.set_border_width(10)

        hbox = Gtk.Box(spacing=6)
        self.add(hbox)

        button1 = Gtk.RadioButton.new_with_label_from_widget(None, "Free")
        button1.connect("toggled", self.on_button_toggled, "1")
        hbox.pack_start(button1, False, False, 0)

        button2 = Gtk.RadioButton.new_from_widget(button1)
        button2.set_label("Work")
        button2.connect("toggled", self.on_button_toggled, "2")
        hbox.pack_start(button2, False, False, 0)

        button3 = Gtk.RadioButton.new_from_widget(button1)
        button3.set_label("Phone")
        button3.connect("toggled", self.on_button_toggled, "3")
        hbox.pack_start(button3, False, False, 0)
        
        self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
        self.sock.connect((bd_addr,port))        
        self.send_command("130")

    def on_button_toggled(self, button, name):
        
        if button.get_active():
          if name == "1":
            value = "130"
          if name == "2":
            value = "70"
          if name == "3":
            value = "25"
          self.send_command(value)
          state = "on"
        else:
            state = "off"
        #print("Button", name, "was turned", state, " value : ", value)
        
    def send_command(self, val):
        self.sock.send(val)
 

win = AppWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()