import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio, Gtk, Gdk

class AddFriendWidget(Gtk.Box):

    def __init__(self, main_window, fchat_prv, friend_list):
        Gtk.Box.__init__(self, spacing=7, orientation = Gtk.Orientation.VERTICAL)

        self.fchat_prv = fchat_prv
        self.main_window = main_window
        self.friend_list = friend_list

        self.fchat_prv.add_friend_gui = self

        self.generate_keys_bt = Gtk.Button('Generate Key')
        self.generate_keys_bt.connect('clicked', self.on_generate_keys)

        self.save_bt = Gtk.Button('Save')
        self.save_bt.connect('clicked', self.on_save)

        self.cancel_bt = Gtk.Button('Cancel')
        self.cancel_bt.connect('clicked', self.on_cancel)

        self.close_bt = Gtk.Button('Close')
        self.close_bt.connect('clicked', self.on_close)

        self.owner_info  = Gtk.Entry()
        self.owner_info.set_sensitive(False)
        
        self.copy_clipboard_bt = Gtk.Button(label='Copy to clipboard')
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        self.copy_clipboard_bt.connect('clicked', self.on_copy_clipboard)
        
        h_owner = Gtk.Box(spacing=5)
        h_owner.pack_start(self.owner_info, True, True, 1)
        h_owner.pack_start(self.copy_clipboard_bt, False, False, 1)

        self.friend_info  = Gtk.Entry()
        self.friend_info.set_placeholder_text('Key of your friend')
        self.spinner = Gtk.Spinner()

        self.pack_start(h_owner, True, False, 7)
        self.pack_start(self.friend_info, True, False, 7)
        self.pack_start(self.spinner, True, False, 7)
        h_bt = Gtk.Box()
        h_bt.pack_start(self.generate_keys_bt, True, False, 7)
        h_bt.pack_start(self.save_bt, True, False, 7)
        h_bt.pack_start(self.cancel_bt, True, False, 7)
        h_bt.pack_start(self.close_bt, True, False, 7)
        self.pack_start(h_bt, True, False, 7)
        
        self.job = None

    def on_generate_keys(self, button):
        self.pub, self.prv, self.pub_info_key, self.job = self.fchat_prv.generate_key_for_friend()
        self.owner_info.set_text(self.pub_info_key)
        self.on_generate_keys_start()

    def on_generate_keys_start(self):
        self.spinner.show()
        self.spinner.start()
        self.friend_info.set_sensitive(False)
        self.save_bt.set_sensitive(False)
        self.close_bt.set_sensitive(False)
        self.generate_keys_bt.set_sensitive(False)
        self.copy_clipboard_bt.set_sensitive(False)

    def on_generate_keys_ok(self):
        self.spinner.hide()
        self.spinner.stop()
        self.friend_info.set_sensitive(True)
        self.save_bt.set_sensitive(True)
        self.close_bt.set_sensitive(True)
        self.generate_keys_bt.set_sensitive(True)
        self.copy_clipboard_bt.set_sensitive(True)

    def on_generate_keys_faild(self, text):

        self.spinner.hide()
        self.spinner.stop()
        self.friend_info.set_sensitive(True)
        self.save_bt.set_sensitive(True)
        self.close_bt.set_sensitive(True)
        self.generate_keys_bt.set_sensitive(True)
        self.copy_clipboard_bt.set_sensitive(True)

    def on_cancel(self, button):
        if self.job:
            self.job.remove_from_queue_when_finish()


    def on_close(self, button):
        self.main_window.application.back_main_window_or_friend_list()

    def on_save(self, button):
        if self.owner_info.get_text() == '':
            self.msg_info('You should generate a key that contains your info')
            return
        
        if self.friend_info.get_text() == '':
            self.msg_info('Friend info is required')
            return
        
        self.fchat_prv.add_friend(self.pub, self.prv, self.friend_info.get_text())
        
        self.on_save_start()

    def on_save_start(self):
        self.spinner.show()
        self.spinner.start()
        self.friend_info.set_sensitive(False)
        self.save_bt.set_sensitive(False)
        self.close_bt.set_sensitive(False)
        self.generate_keys_bt.set_sensitive(False)
        self.copy_clipboard_bt.set_sensitive(False)

    def on_save_start_ok(self):
        self.spinner.hide()
        self.spinner.stop()
        self.friend_info.set_sensitive(True)
        self.save_bt.set_sensitive(True)
        self.close_bt.set_sensitive(True)
        self.generate_keys_bt.set_sensitive(True)
        self.copy_clipboard_bt.set_sensitive(True)
        self.friend_list.sync_friends_list()

    def on_save_start_duplicate(self, text):
        self.msg_info(text)

    def on_save_start_faild(self):
        dialog = Gtk.MessageDialog(self.main_window, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "ERROR")
        dialog.format_secondary_text("Error adding friend please try later")
        dialog.run()
        dialog.destroy()

        self.spinner.hide()
        self.spinner.stop()
        self.friend_info.set_sensitive(True)
        self.save_bt.set_sensitive(True)
        self.close_bt.set_sensitive(True)
        self.generate_keys_bt.set_sensitive(True)
        self.copy_clipboard_bt.set_sensitive(True)

    def on_copy_clipboard(self, button):
        self.clipboard.set_text(self.owner_info.get_text(), -1)

    def msg_info(self, text):
        dialog = Gtk.MessageDialog(self.main_window, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Info")
        dialog.format_secondary_text(text)
        dialog.run()
        dialog.destroy()

[((2, 0, 2, 32), 'gi.require_version', 'gi.require_version', ({(2, 19, 2, 24): '"""Gtk"""', (2, 26, 2, 31): '"""3.0"""'}, {}), "('Gtk', '3.0')", False, 'import gi\n'), ((8, 8, 8, 81), 'gi.repository.Gtk.Box.__init__', 'Gtk.Box.__init__', (), '', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((16, 32, 16, 58), 'gi.repository.Gtk.Button', 'Gtk.Button', ({(16, 43, 16, 57): '"""Generate Key"""'}, {}), "('Generate Key')", False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((19, 23, 19, 41), 'gi.repository.Gtk.Button', 'Gtk.Button', ({(19, 34, 19, 40): '"""Save"""'}, {}), "('Save')", False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((22, 25, 22, 45), 'gi.repository.Gtk.Button', 'Gtk.Button', ({(22, 36, 22, 44): '"""Cancel"""'}, {}), "('Cancel')", False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((25, 24, 25, 43), 'gi.repository.Gtk.Button', 'Gtk.Button', ({(25, 35, 25, 42): '"""Close"""'}, {}), "('Close')", False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((28, 27, 28, 38), 'gi.repository.Gtk.Entry', 'Gtk.Entry', ({}, {}), '()', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((31, 33, 31, 70), 'gi.repository.Gtk.Button', 'Gtk.Button', (), '', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((32, 25, 32, 67), 'gi.repository.Gtk.Clipboard.get', 'Gtk.Clipboard.get', ({(32, 43, 32, 66): 'Gdk.SELECTION_CLIPBOARD'}, {}), '(Gdk.SELECTION_CLIPBOARD)', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((35, 18, 35, 36), 'gi.repository.Gtk.Box', 'Gtk.Box', (), '', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((39, 28, 39, 39), 'gi.repository.Gtk.Entry', 'Gtk.Entry', ({}, {}), '()', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((41, 23, 41, 36), 'gi.repository.Gtk.Spinner', 'Gtk.Spinner', ({}, {}), '()', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((46, 15, 46, 24), 'gi.repository.Gtk.Box', 'Gtk.Box', ({}, {}), '()', False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((132, 17, 132, 107), 'gi.repository.Gtk.MessageDialog', 'Gtk.MessageDialog', ({(132, 35, 132, 51): 'self.main_window', (132, 53, 132, 54): '0', (132, 56, 132, 77): 'Gtk.MessageType.ERROR', (132, 79, 132, 97): 'Gtk.ButtonsType.OK', (132, 99, 132, 106): '"""ERROR"""'}, {}), "(self.main_window, 0, Gtk.MessageType.ERROR, Gtk.\n    ButtonsType.OK, 'ERROR')", False, 'from gi.repository import Gio, Gtk, Gdk\n'), ((149, 17, 149, 105), 'gi.repository.Gtk.MessageDialog', 'Gtk.MessageDialog', ({(149, 35, 149, 51): 'self.main_window', (149, 53, 149, 54): '0', (149, 56, 149, 76): 'Gtk.MessageType.INFO', (149, 78, 149, 96): 'Gtk.ButtonsType.OK', (149, 98, 149, 104): '"""Info"""'}, {}), "(self.main_window, 0, Gtk.MessageType.INFO, Gtk.\n    ButtonsType.OK, 'Info')", False, 'from gi.repository import Gio, Gtk, Gdk\n')]