class NotificationSettingList(Container):
    def ApplyAttributes(self, attributes):
        Container.ApplyAttributes(self, attributes)
        self.lastVerticalBarEnabledStatus = False
        self.notificationSettingHandler = NotificationSettingHandler()
        self.notificationSettingData = self.notificationSettingHandler.LoadSettings(
        )
        self.isDeveloperMode = attributes.get('developerMode', False)
        self._SetupUI()

    def _SetupUI(self):
        self.settingsDescriptionRowContainer = Container(name='Settings',
                                                         height=16,
                                                         align=uiconst.TOTOP,
                                                         parent=self,
                                                         padding=(5, 5, 10, 0))
        EveLabelMediumBold(
            name='Settings',
            align=uiconst.TOLEFT,
            parent=self.settingsDescriptionRowContainer,
            text=localization.GetByLabel(
                'Notifications/NotificationSettings/CategorySubscriptions'),
            bold=True)
        Sprite(
            name='popupIcon',
            parent=self.settingsDescriptionRowContainer,
            align=uiconst.TORIGHT,
            texturePath=
            'res:/UI/Texture/classes/Notifications/settingsPopupIcon.png',
            width=16,
            heigh=16,
            hint=localization.GetByLabel(
                'Notifications/NotificationSettings/PopupVisibilityTooltip'))
        Sprite(
            name='visibilityIcon',
            parent=self.settingsDescriptionRowContainer,
            align=uiconst.TORIGHT,
            texturePath=
            'res:/UI/Texture/classes/Notifications/settingsVisibleIcon.png',
            width=16,
            heigh=16,
            hint=localization.GetByLabel(
                'Notifications/NotificationSettings/HistoryVisibilityTooltip'),
            padding=(0, 0, 6, 0))
        self._MakeSeperationLine(self)
        self.scrollList = ScrollContainer(name='scrollContainer',
                                          parent=self,
                                          align=uiconst.TOALL,
                                          padding=(5, 5, 5, 5))
        self.scrollList.OnScrolledVertical = self.VerticalScrollInject

    def _MakeSeperationLine(self, parent):
        Line(name='topLine',
             parent=parent,
             align=uiconst.TOTOP,
             weight=1,
             padBottom=2,
             opacity=0.3)

    def VerticalScrollInject(self, scrollTo):
        self.AdjustCategoryHeaderForScrollBar()

    def AdjustCategoryHeaderForScrollBar(self):
        if self.lastVerticalBarEnabledStatus == self.scrollList.verticalScrollBar.display:
            return
        if self.scrollList.verticalScrollBar.display:
            self.settingsDescriptionRowContainer.padRight = 10 + self.scrollList.verticalScrollBar.width
        else:
            self.settingsDescriptionRowContainer.padRight = 10
        self.lastVerticalBarEnabledStatus = self.scrollList.verticalScrollBar.display

    def _GetGroupScrollEntries(self):
        entries = []
        for group, list in notificationConst.groupTypes.iteritems():
            groupName = localization.GetByLabel(
                notificationConst.groupNamePathsNewNotifications[group])
            entries.append(
                self.GetGroupEntry(fakeID=group, groupName=groupName))

        return entries

    def PopulateScroll(self):
        entries = self._GetGroupScrollEntries()
        entries.sort(key=lambda entr: entr.data.GetLabel().lower())
        for entry in entries:
            self.scrollList.children.append(entry)

    def ReloadScroll(self):
        self.notificationSettingHandler = NotificationSettingHandler()
        self.notificationSettingData = self.notificationSettingHandler.LoadSettings(
        )
        self.scrollList.Flush()
        self.PopulateScroll()

    def GetGroupEntry(self, fakeID, groupName):
        from eve.client.script.ui.control.treeData import TreeData
        rawNotificationList = notificationConst.groupTypes[fakeID]
        groupSettings = {}
        self.AppendEntryData(
            data=groupSettings,
            visibilityChecked=self.notificationSettingHandler.
            GetVisibilityStatusForGroup(fakeID, self.notificationSettingData),
            showPopupChecked=self.notificationSettingHandler.
            GetShowPopupStatusForGroup(fakeID, self.notificationSettingData),
            isGroup=True,
            id=fakeID)
        childrenData = []
        for notification in rawNotificationList:
            settingLabel = notificationConst.notificationToSettingDescription.get(
                notification, None)
            settingName = localization.GetByLabel(settingLabel)
            params = {}
            setting = self.notificationSettingData[notification]
            self.AppendEntryData(data=params,
                                 visibilityChecked=setting.showAtAll,
                                 showPopupChecked=setting.showPopup,
                                 isGroup=False,
                                 id=notification)
            notificationData = TreeData(label=settingName,
                                        parent=None,
                                        isRemovable=False,
                                        settings=params,
                                        settingsID=notification)
            childrenData.append(notificationData)

        childrenData.sort(key=lambda childData: childData.GetLabel().lower())
        data = TreeData(label=groupName,
                        parent=None,
                        children=childrenData,
                        icon=None,
                        isRemovable=False,
                        settings=groupSettings)
        entry = TreeViewSettingsItem(level=0,
                                     eventListener=self,
                                     data=data,
                                     settingsID=fakeID,
                                     defaultExpanded=False)
        return entry

    def AppendEntryData(self, data, visibilityChecked, showPopupChecked,
                        isGroup, id):
        data.update({
            NotificationSettingEntityDeco.VISIBILITY_CHECKED_KEY:
            visibilityChecked,
            NotificationSettingEntityDeco.POPUP_CHECKED_KEY:
            showPopupChecked,
            NotificationSettingEntityDeco.VISIBILITY_CHANGED_CALLBACK_KEY:
            self.OnVisibilityEntryChangedNew,
            NotificationSettingEntityDeco.POPUP_CHANGED_CALLBACK_KEY:
            self.OnShowPopupEntryChangedNew,
            NotificationSettingEntityDeco.GETMENU_CALLBACK:
            self.GetMenuForEntry,
            'isGroup':
            isGroup,
            'id':
            id
        })

    def OnVisibilityEntryChangedNew(self, isGroup, id, checked):
        if not isGroup:
            self._setVisibilitySettingForNotification(id, checked)

    def OnShowPopupEntryChangedNew(self, isGroup, id, checked):
        if not isGroup:
            self._setPopupSettingForNotification(id, checked)

    def _setVisibilitySettingForNotification(self, id, on):
        notificationData = self.notificationSettingData[id]
        notificationData.showAtAll = on
        self.SaveAllData()

    def _setPopupSettingForNotification(self, id, on):
        notificationData = self.notificationSettingData[id]
        notificationData.showPopup = on
        self.SaveAllData()

    def SaveAllData(self):
        self.notificationSettingHandler.SaveSettings(
            self.notificationSettingData)

    def GetMenuForEntry(self, isGroup, nodeID):
        if isGroup or not self.isDeveloperMode:
            return []
        else:
            return [('spawnNotification %s' % nodeID,
                     self.OnSpawnNotificationClick, [nodeID])]

    def OnSpawnNotificationClick(self, notificationID):
        mapper = NotificationFormatMapper()
        newFormatter = mapper.GetFormatterForType(notificationID)
        if newFormatter:
            import blue
            data = newFormatter.MakeSampleData()
            sm.ScatterEvent('OnNotificationReceived',
                            123,
                            notificationID,
                            98000001,
                            blue.os.GetWallclockTime(),
                            data=data)
        else:
            from notifications.client.development.notificationDevUI import FakeNotificationMaker
            maker = FakeNotificationMaker()
            counter = 1
            agentStartID = 3008416
            someAgentID = agentStartID + counter
            senderID = 98000001
            corpStartID = 1000089
            someCorp = corpStartID + counter
            maker.ScatterSingleNotification(counter, notificationID, senderID,
                                            someAgentID, someCorp)
class OpportunitySettings(object):
    def __init__(self, *args, **kwargs):
        self.handler = NotificationSettingHandler()
        self.disableNotifications = self.GetNotifcationSettingCheckState()
        self.oldDisableNotifications = self.disableNotifications
        self.disableAura = settings.user.ui.Get(
            AchievementSettingConst.AURA_DISABLE_CONFIG, False)
        self.oldDisableAura = self.disableAura
        self.hideInfoPanel = settings.user.ui.Get(
            AchievementSettingConst.INFOPANEL_DISABLE_CONFIG, False)
        self.oldHideInfoPanel = self.hideInfoPanel

    def GetOpportunitySettings(self, menuParent):
        menuParent.AddCheckBox(
            text=GetByLabel('UI/Achievements/SupressNotifications'),
            checked=self.disableNotifications,
            callback=self.OnNotificationSettingsChanged)
        menuParent.AddCheckBox(text=GetByLabel('UI/Achievements/SuppressAura'),
                               checked=self.disableAura,
                               callback=self.OnAuraSettingsChanged)
        menuParent.AddCheckBox(
            text=GetByLabel('UI/Achievements/HideFromInfoPanel'),
            checked=self.hideInfoPanel,
            callback=self.OnInfoPanelSettingsChanged)

    def OnAuraSettingsChanged(self):
        self.disableAura = not self.disableAura

    def OnInfoPanelSettingsChanged(self):
        self.hideInfoPanel = not self.hideInfoPanel

    def OnNotificationSettingsChanged(self):
        self.disableNotifications = not self.disableNotifications

    def GetNotifcationSettingCheckState(self):
        notificationSetting = self.handler.LoadSettings()
        for settingID in NOTIFICATIONIDS:
            specificSetting = notificationSetting.get(settingID)
            if specificSetting.showPopup or specificSetting.showAtAll:
                return False

        return True

    def StoreChanges(self):
        self.StoreInfoPanelChanges()
        self.StoreAuraChanges()
        self.StoreNotificationChanges()

    def OnOpportunitiesSettingChanged(self, configName, value):
        settings.user.ui.Set(configName, value)

    def StoreInfoPanelChanges(self):
        if self.hideInfoPanel == self.oldHideInfoPanel:
            return
        self.OnOpportunitiesSettingChanged(
            AchievementSettingConst.INFOPANEL_DISABLE_CONFIG,
            self.hideInfoPanel)
        sm.GetService('infoPanel').Reload()

    def StoreAuraChanges(self):
        if self.disableAura == self.oldDisableAura:
            return
        self.OnOpportunitiesSettingChanged(
            AchievementSettingConst.AURA_DISABLE_CONFIG, self.disableAura)
        if self.disableAura:
            from achievements.client.auraAchievementWindow import AchievementAuraWindow
            auraWnd = AchievementAuraWindow.GetIfOpen()
            if auraWnd and not auraWnd.destroyed:
                auraWnd.Close()

    def StoreNotificationChanges(self):
        if self.disableNotifications == self.oldDisableNotifications:
            return
        newNotificationValue = not self.disableNotifications
        notificationSetting = self.handler.LoadSettings()
        for settingID in NOTIFICATIONIDS:
            specificSetting = notificationSetting.get(settingID)
            specificSetting.showPopup = newNotificationValue
            specificSetting.showAtAll = newNotificationValue

        self.handler.SaveSettings(notificationSetting)
        from notifications.client.controls.notificationSettingsWindow import NotificationSettingsWindow
        notificationWnd = NotificationSettingsWindow.GetIfOpen()
        if notificationWnd and not notificationWnd.destroyed:
            notificationWnd.ReloadSettings()
            notificationWnd.Open()