class TrayUI(gtk.VBox):
    def __init__(self):
        gtk.VBox.__init__(self, spacing=0)
        self.init_ui()
        self.active_ap_index = []
        self.all_showed = False

    def init_ui(self):
        self.wire = Section(app_theme.get_pixbuf("network/cable.png"),
                            _("Wired"))
        self.wireless = Section(app_theme.get_pixbuf("network/wifi.png"),
                                _("Wireless"))
        self.mobile = Section(app_theme.get_pixbuf("network/3g.png"),
                              _("Mobile Network"))
        # vpn
        self.vpn = Section(app_theme.get_pixbuf("network/vpn.png"),
                           _("VPN Network"))
        self.dsl = Section(app_theme.get_pixbuf("network/dsl.png"), _("DSL"))

        self.ssid_list = []
        self.tree_box = gtk.VBox(spacing=0)
        self.button_more = SelectButton(_("Advanced..."),
                                        font_size=10,
                                        ali_padding=5)
        self.button_more.set_size_request(-1, 25)
        #self.pack_start(self.button_more, False, False)
        self.ap_tree = TreeView(mask_bound_height=0)
        self.ap_tree.set_expand_column(0)

        self.vpn_list = ConList()
        self.dsl_list = DSLConList()

        self.wire_box = self.section_box([self.wire])
        self.wireless_box = self.section_box([self.wireless, self.tree_box])
        self.mobile_box = self.section_box([self.mobile])
        self.vpn_box = self.section_box([self.vpn, self.vpn_list])
        self.dsl_box = self.section_box([self.dsl, self.dsl_list])
        self.wire_state = False
        self.wireless_state = False
        self.mobile_state = False
        self.vpn_state = False
        self.dsl_state = False

        self.device_tree = None

        self.pack_start(self.wire_box, False, False)
        self.pack_start(self.wireless_box, False, False)
        self.pack_start(self.mobile_box, False, False)
        self.pack_start(self.vpn_box, False, False)
        self.pack_start(self.dsl_box, False, False)
        self.pack_start(self.button_more, False, False)

    def get_widget_height(self):
        height = 0
        if self.wire_state:
            height += 35
        if self.wireless_state:
            height += 35
            if self.ap_tree.visible_items and self.wireless.get_active():
                height += self.ap_tree.get_size_request()[1]
            if self.device_tree:
                height += 22

        if self.mobile_state:
            height += 35

        if self.vpn_state:
            height += 35 + len(self.vpn_list.get_children()) * 22

        if self.dsl_state:
            height += 35 + len(self.dsl_list.get_children()) * 22
            height += 5
        height += 25

        return height

    def section_box(self, widgets):
        box = gtk.VBox(spacing=0)
        for w in widgets:
            box.pack_start(w, False, False)
        style.add_separator(box, 10)
        return box

    def remove_net(self, net_type):
        #print net_type
        getattr(self, net_type + "_box").set_no_show_all(True)
        #getattr(self, net_type).set_active((True, False))
        getattr(self, net_type + "_box").hide()
        setattr(self, net_type + "_state", False)

    def show_net(self, net_type):
        getattr(self, net_type + "_box").set_no_show_all(False)
        getattr(self, net_type + "_box").show()
        setattr(self, net_type + "_state", True)

    def set_wired_state(self, widget, new_state, reason):
        if new_state is 20:
            self.wire.set_active(0)
        else:
            tray_log.debug(__name__, new_state, reason)

    def set_visible_aps(self, show_all=False):
        if not self.__ap_list:
            self.visible_aps = []
            return

        tray_log.debug(len(self.__ap_list))

        if show_all:
            if len(self.__ap_list) <= 10:
                self.visible_aps = self.__ap_list[:]
            else:
                self.visible_aps = self.__ap_list[:10]
            self.more_button.set_ap_list([])
            self.show_all = True

        else:
            if len(self.__ap_list) <= 5:
                self.visible_aps = self.__ap_list[:]
                self.show_all = True
            else:
                self.visible_aps = self.__ap_list[:5]
                self.more_button.set_ap_list(self.__ap_list[5:])
                self.show_all = False

    def set_ap(self, ap_list, redraw=True):
        if not ap_list:
            return
        self.__set_ap_list(ap_list)
        #print "DEBUG", len(self.visible_aps), self.show_all
        self.ap_tree.delete_all_items()
        container_remove_all(self.tree_box)

        self.ap_tree.add_items(map(lambda ap: SsidItem(ap), self.__ap_list))
        length = len(self.ap_tree.visible_items)
        if length <= 10:
            self.ap_tree.set_size_request(-1, WIDGET_HEIGHT * length)
        else:
            self.ap_tree.set_size_request(-1, WIDGET_HEIGHT * 10)
            for item in self.ap_tree.visible_items:
                item.set_padding(10)

        self.tree_box.pack_start(self.ap_tree, False, False)
        self.show_all()

        if redraw:
            Dispatcher.request_resize()

    def __set_ap_list(self, ap_list):
        self.__ap_list = ap_list

    def move_active(self, index):
        if index != [] and self.__ap_list:
            for i in index:
                if i < len(self.ap_tree.visible_items):
                    self.ap_tree.delete_item_by_index(i)
                    self.ap_tree.add_items([SsidItem(self.__ap_list[i])],
                                           insert_pos=0)
                else:
                    self.ap_tree.delete_item_by_index(-1)
                    self.ap_tree.add_items([SsidItem(self.__ap_list[i])],
                                           insert_pos=0)
                self.ap_tree.visible_items[0].set_active(True)

    def set_active_ap(self, index, state):
        self.active_ap_index = index
        self.set_ap(self.__ap_list, redraw=False)

        if index:
            self.move_active(index)

    def get_active_ap(self):
        return self.active_ap_index

    def add_switcher(self):
        if not hasattr(self, "device_tree") or not self.device_tree:
            self.device_tree = TreeView([DeviceItem()], mask_bound_height=0)
            self.device_tree.set_expand_column(1)
            self.wireless_box.pack_start(self.device_tree, False, False)
            self.wireless_box.reorder_child(
                self.wireless_box.get_children()[-2],
                len(self.wireless_box.get_children()))
            tray_log.debug(self.wireless_box.get_children())
            net_manager.emit_wifi_switch(0)

    def remove_switcher(self):
        if self.device_tree:
            self.wireless_box.remove(self.device_tree)
            self.device_tree = None

    def get_active_in_ui(self):
        return filter(lambda i: i.get_active() == True,
                      self.ap_tree.visible_items)

    def reset_tree(self):
        if len(self.ap_tree.visible_items) >= 5 and self.all_showed:
            remove_items = self.ap_tree.visible_items[5:]
            self.ap_tree.delete_items(remove_items)
            self.ap_tree.set_size_request(-1, WIDGET_HEIGHT * 5)
            self.tree_box.pack_start(self.more_button, False, False)
            self.all_showed = False