import os
import c4d
from ssp_utils_c4d_version import *
from ssp_utils_dialog import *
from ssp_ids import *
from ssp_utils_pref import *
from ssp_utils import *
#from ssp_preference_data import *


####################################################################################################
# Class DialogOptions ##############################################################################
####################################################################################################
# for C4D versions <r19
class DialogOptions(c4d.gui.GeDialog):

    ID_BUTTON_RESET_TO_DEFAULT = 42
    ID_BUTTON_OPEN_SSP = 43


    def FillSessionCombo(self):
        self.FreeChildren(BCID_INT32_PREF_START_SESSION)

        self.AddChild(BCID_INT32_PREF_START_SESSION, 10000, 'None')
        self.AddChild(BCID_INT32_PREF_START_SESSION, 10001, '(Last Opened Projects)')

        sessions = SessionGetSessions()

        for idxSession, nameSession in enumerate(sessions):
            self.AddChild(BCID_INT32_PREF_START_SESSION, idxSession, nameSession)


    def FillThumbSizeCombo(self):
        self.FreeChildren(BCID_INT32_PREF_THUMB_SIZE)

        self.AddChild(BCID_INT32_PREF_THUMB_SIZE, 0, 'Small')
        self.AddChild(BCID_INT32_PREF_THUMB_SIZE, 1, 'Medium')
        self.AddChild(BCID_INT32_PREF_THUMB_SIZE, 2, 'Large')


    def FillThumbAspectRatioCombo(self):
        self.FreeChildren(BCID_INT32_PREF_THUMB_ASPECT_RATIO)

        self.AddChild(BCID_INT32_PREF_THUMB_ASPECT_RATIO, 0, 'Square')
        self.AddChild(BCID_INT32_PREF_THUMB_ASPECT_RATIO, 1, 'Viewport')
        self.AddChild(BCID_INT32_PREF_THUMB_ASPECT_RATIO, 2, 'Viewport Safe Frame')


    def FillThumbUpdateCombo(self):
        self.FreeChildren(BCID_INT32_PREF_THUMB_UPDATES)

        self.AddChild(BCID_INT32_PREF_THUMB_UPDATES, 0, 'No Automatic Updates')
        self.AddChild(BCID_INT32_PREF_THUMB_UPDATES, 1, 'Scene Switch')
        self.AddChild(BCID_INT32_PREF_THUMB_UPDATES, 60000, '1 Minute')
        self.AddChild(BCID_INT32_PREF_THUMB_UPDATES, 300000, '5 Minute')
        self.AddChild(BCID_INT32_PREF_THUMB_UPDATES, 600000, '10 Minute')
        self.AddChild(BCID_INT32_PREF_THUMB_UPDATES, 900000, '15 Minute')


    def FillThumbMarkerCombo(self):
        availableMarker = GetAvailableMarker()

        self.FreeChildren(BCID_INT32_PREF_THUMB_MARKER_ID)

        for marker in availableMarker.values():
            self.AddChild(BCID_INT32_PREF_THUMB_MARKER_ID, marker[2], marker[0])


    def CreateLayout_GroupSession(self, wColLeftMain):
        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=1, title='Session'): # Session group
            self.GroupBorder(c4d.BORDER_NONE | c4d.BORDER_WITH_TITLE_BOLD)
            self.GroupBorderSpace(15, 5, 7, 3)

            if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, cols=2, title=''): # Start Session group
                self.AddStaticText(0, c4d.BFH_LEFT, initw=wColLeftMain, name='Start Session')
                self.AddComboBox(BCID_INT32_PREF_START_SESSION, c4d.BFH_SCALEFIT)
                self.FillSessionCombo()

            self.GroupEnd() # Start Session group

            if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=2, title=''): # Session Options group
                wColLeft = 300

                self.AddCheckbox(BCID_BOOL_PREF_ASK_REOPEN, c4d.BFH_LEFT, initw=wColLeft, inith=0, name='Ask Before Reopen on Startup')
                self.AddStaticText(0, c4d.BFH_SCALEFIT, name='')
                self.AddCheckbox(BCID_BOOL_PREF_ASK_SESSION_SWITCH, c4d.BFH_LEFT, initw=wColLeft, inith=0, name='Ask Before Switching Session')
                self.AddStaticText(0, c4d.BFH_SCALEFIT, name='')
                #self.AddButton(ID_BUTTON_PREF_MANAGE_SESSIONS, c4d.BFH_LEFT, initw=wColLeft, inith=0, name='Manage Sessions...')
                #self.AddStaticText(0, c4d.BFH_SCALEFIT, name='')

            self.GroupEnd() # Session Options group

        self.GroupEnd() # Session group


    def CreateLayout_GroupDesign(self):
        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=1, title='Design -.Menu Bar'): # Design group
            self.GroupBorder(c4d.BORDER_NONE | c4d.BORDER_WITH_TITLE_BOLD)
            self.GroupBorderSpace(15, 10, 7, 3)

            self.AddCheckbox(BCID_BOOL_PREF_MENU_FILES_ENABLED, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Files Menu')
            self.AddCheckbox(BCID_BOOL_PREF_MENU_SCENES_ENABLED, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Projects Menu')
            self.AddCheckbox(BCID_BOOL_PREF_PREV_NEXT_BUTTONS_ENABLED, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Prev/Next Buttons (Restarts Dialog)')
            self.AddCheckbox(BCID_BOOL_PREF_DESIGN_BUTTONS_ENABLED, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Design Buttons (Restarts Dialog)')

        self.GroupEnd() # Design group


    def CreateLayout_GroupDesignProject(self):
        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=1, title='Design - Project Selection'): # Design - Project Selection group
            self.GroupBorder(c4d.BORDER_NONE | c4d.BORDER_WITH_TITLE_BOLD)
            self.GroupBorderSpace(15, 10, 7, 3)

            self.AddCheckbox(BCID_BOOL_PREF_COMBO_ENABLED, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Dropdown')
            self.AddCheckbox(BCID_BOOL_PREF_THUMB_ENABLED, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Thumbnails')
            self.AddCheckbox(BCID_BOOL_PREF_TABS_ENABLED, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Tabs')

        self.GroupEnd() # Design - Project Selection group


    def CreateLayout_GroupDesignExtra(self):
        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=1, title='Design - Advanced Settings'): # Design - Advanced Settings group
            self.GroupBorder(c4d.BORDER_NONE | c4d.BORDER_WITH_TITLE_BOLD)
            self.GroupBorderSpace(15, 10, 7, 3)

            self.AddCheckbox(BCID_BOOL_PREF_NEW_PROJECT, c4d.BFH_SCALEFIT, initw=0, inith=0, name='New Project Option')
            self.AddCheckbox(BCID_BOOL_PREF_LARGE_COMBO, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Large Dropdown')
            self.AddCheckbox(BCID_BOOL_PREF_POPUP_MODE, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Close Dialog After Project Selection')
            self.AddCheckbox(BCID_BOOL_PREF_SHOW_SHORTCUTS, c4d.BFH_SCALEFIT, initw=0, inith=0, name='Show Project Index in Menus')

        self.GroupEnd() # Design - Advanced Settings group


    def CreateLayout_GroupThumbnails(self, wColLeftMain):
        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=2, title='Thumbnails'): # Thumbnails group
            self.GroupBorder(c4d.BORDER_NONE | c4d.BORDER_WITH_TITLE_BOLD)
            self.GroupBorderSpace(15, 10, 7, 3)

            self.AddStaticText(0, c4d.BFH_LEFT, name='Size', initw=wColLeftMain)
            self.AddComboBox(BCID_INT32_PREF_THUMB_SIZE, c4d.BFH_SCALEFIT)
            self.FillThumbSizeCombo()

            self.AddStaticText(0, c4d.BFH_LEFT, name='Aspect Ratio', initw=wColLeftMain)
            self.AddComboBox(BCID_INT32_PREF_THUMB_ASPECT_RATIO, c4d.BFH_SCALEFIT)
            self.FillThumbAspectRatioCombo()

            self.AddStaticText(0, c4d.BFH_LEFT, name='Update every', initw=wColLeftMain)
            self.AddComboBox(BCID_INT32_PREF_THUMB_UPDATES, c4d.BFH_SCALEFIT)
            self.FillThumbUpdateCombo()

            self.AddCheckbox(BCID_BOOL_PREF_THUMB_TITLE, c4d.BFH_LEFT, initw=wColLeftMain, inith=0, name='Show Scene Name')
            self.AddStaticText(0, c4d.BFH_SCALEFIT, name='')

        self.GroupEnd() # Thumbnails group


    def CreateLayout_GroupActiveScene(self, wColLeftMain):
        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=2, title='Active Scene'): # Active Scene
            self.GroupBorder(c4d.BORDER_NONE | c4d.BORDER_WITH_TITLE_BOLD)
            self.GroupBorderSpace(15, 10, 7, 0)

            self.AddCheckbox(BCID_BOOL_PREF_THUMB_HIDE_ACTIVE, c4d.BFH_LEFT, initw=wColLeftMain, inith=0, name='Hide Active Scene')
            self.AddStaticText(0, c4d.BFH_SCALEFIT, name='')

            self.AddStaticText(0, c4d.BFH_LEFT, name='Fade Active Scene', initw=wColLeftMain)
            self.AddEditSlider(BCID_INT32_PREF_THUMB_FADE_ACTIVE, c4d.BFH_SCALEFIT, initw=0, inith=0)

            self.AddStaticText(0, c4d.BFH_LEFT, name='Active Scene Marker', initw=wColLeftMain)
            self.AddComboBox(BCID_INT32_PREF_THUMB_MARKER_ID, c4d.BFH_SCALEFIT)
            self.FillThumbMarkerCombo()

        self.GroupEnd() # Active Scene

        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=2, title=''): # Active Scene continued
            self.GroupBorderSpace(15, 0, 7, 3)

            wColLeft = 250

            self.AddCheckbox(BCID_BOOL_PREF_THUMB_OVERLAY_LT, c4d.BFH_LEFT, initw=wColLeft, inith=0, name='Marker Left/Top')
            self.AddCheckbox(BCID_BOOL_PREF_THUMB_OVERLAY_RT, c4d.BFH_LEFT, initw=0, inith=0, name='Marker Right/Top')
            self.AddCheckbox(BCID_BOOL_PREF_THUMB_OVERLAY_LB, c4d.BFH_LEFT, initw=wColLeft, inith=0, name='Marker Left/Bottom')
            self.AddCheckbox(BCID_BOOL_PREF_THUMB_OVERLAY_RB, c4d.BFH_LEFT, initw=0, inith=0, name='Marker Right/Bottom')
            self.AddCheckbox(BCID_BOOL_PREF_THUMB_OVERLAY_LABEL, c4d.BFH_LEFT, initw=wColLeft, inith=0, name='Active Label')
            self.AddStaticText(0, c4d.BFH_LEFT, name='', initw=0) # Dummy

        self.GroupEnd() # Active Scene continued

        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=5, title=''): # Overlay Colors
            self.GroupBorderSpace(15, 0, 7, 3)

            self.AddStaticText(0, c4d.BFH_LEFT, name='Marker Color')
            self.AddColorField(BCID_COLOR_PREF_THUMB_MARKER_COLOR, c4d.BFH_LEFT)

            self.AddStaticText(0, c4d.BFH_LEFT, name=' ', initw=70) # Spcaer

            self.AddStaticText(0, c4d.BFH_LEFT, name='Label Color')
            self.AddColorField(BCID_COLOR_PREF_THUMB_LABEL_COLOR, c4d.BFH_LEFT)

        self.GroupEnd() # Overlay Colors


    def CreateLayout_GroupDialogFinish(self):
        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_BOTTOM | c4d.BFV_SCALE, cols=5): # Dialog finish
            self.GroupBorderSpace(0, 10, 0, 0)

            self.AddButton(self.ID_BUTTON_OPEN_SSP, c4d.BFH_LEFT | c4d.BFV_TOP, initw=150, inith=0, name='Open {0}'.format(PLUGIN_NAME))
            self.AddButton(self.ID_BUTTON_RESET_TO_DEFAULT, c4d.BFH_LEFT | c4d.BFV_TOP, initw=150, inith=0, name='Reset to Default')
            self.AddStaticText(0, c4d.BFH_SCALEFIT, name='')
            self.AddButton(c4d.DLG_OK, c4d.BFH_RIGHT | c4d.BFV_BOTTOM, initw=117, inith=16, name='Ok')

        self.GroupEnd() # Dialog finish


    def CreateLayout(self):
        wColLeftMain = 200

        self.SetTitle('{0} Options'.format(PLUGIN_NAME))

        if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, cols=1): # Dialog main group
            self.GroupBorderSpace(7, 5, 7, 3)
            self.GroupSpace(0, 15)

            if self.GroupBegin(0, c4d.BFH_SCALEFIT | c4d.BFV_TOP, cols=1, title=PLUGIN_NAME): # Enable group
                self.GroupBorder(c4d.BORDER_NONE | c4d.BORDER_WITH_TITLE_BOLD)
                self.GroupBorderSpace(15, 10, 7, 3)

                self.AddCheckbox(BCID_BOOL_PREF_PLUGIN_ENABLED, c4d.BFH_SCALEFIT, initw=20, inith=0, name='Enable {0}'.format(PLUGIN_NAME))

            self.GroupEnd() # Enable group

            self.CreateLayout_GroupSession(wColLeftMain)

            self.CreateLayout_GroupDesign()
            self.CreateLayout_GroupDesignProject()
            self.CreateLayout_GroupDesignExtra()

            self.CreateLayout_GroupThumbnails(wColLeftMain)

            self.CreateLayout_GroupActiveScene(wColLeftMain)

            self.CreateLayout_GroupDialogFinish()

        self.GroupEnd() # Dialog main group

        return True


    def EnableParameters(self):
        enabled = GetPrefPluginEnabled()

        self.Enable(BCID_INT32_PREF_START_SESSION, enabled)
        self.Enable(BCID_BOOL_PREF_ASK_REOPEN, enabled)
        self.Enable(BCID_BOOL_PREF_ASK_SESSION_SWITCH, enabled)
        self.Enable(BCID_BOOL_PREF_MENU_FILES_ENABLED, enabled)
        self.Enable(BCID_BOOL_PREF_MENU_SCENES_ENABLED, enabled)
        self.Enable(BCID_BOOL_PREF_COMBO_ENABLED, enabled)
        self.Enable(BCID_BOOL_PREF_LARGE_COMBO, enabled)
        self.Enable(BCID_BOOL_PREF_TABS_ENABLED, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_ENABLED, enabled)
        self.Enable(BCID_BOOL_PREF_PREV_NEXT_BUTTONS_ENABLED, enabled)
        self.Enable(BCID_BOOL_PREF_DESIGN_BUTTONS_ENABLED, enabled)
        self.Enable(BCID_BOOL_PREF_POPUP_MODE, enabled)
        self.Enable(BCID_BOOL_PREF_SHOW_SHORTCUTS, enabled)
        self.Enable(BCID_INT32_PREF_THUMB_SIZE, enabled)
        self.Enable(BCID_INT32_PREF_THUMB_ASPECT_RATIO, enabled)
        self.Enable(BCID_INT32_PREF_THUMB_UPDATES, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_TITLE, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_HIDE_ACTIVE, enabled)
        self.Enable(BCID_INT32_PREF_THUMB_FADE_ACTIVE, enabled)
        self.Enable(BCID_INT32_PREF_THUMB_MARKER_ID, enabled)
        self.Enable(BCID_COLOR_PREF_THUMB_MARKER_COLOR, enabled)
        self.Enable(BCID_COLOR_PREF_THUMB_LABEL_COLOR, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_OVERLAY_LT, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_OVERLAY_RT, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_OVERLAY_LB, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_OVERLAY_RB, enabled)
        self.Enable(BCID_BOOL_PREF_THUMB_OVERLAY_LABEL, enabled)
        self.Enable(BCID_BOOL_PREF_NEW_PROJECT, enabled)

        self.Enable(self.ID_BUTTON_OPEN_SSP, enabled)


    def InitValues(self):
        self.SetBool(BCID_BOOL_PREF_PLUGIN_ENABLED, GetPrefPluginEnabled())

        self.SetInt32(BCID_INT32_PREF_START_SESSION, GetPrefStartSession())
        self.SetBool(BCID_BOOL_PREF_ASK_REOPEN, GetPrefAskReopen())
        self.SetBool(BCID_BOOL_PREF_ASK_SESSION_SWITCH, GetPrefAskSessionSwitch())
        self.SetBool(BCID_BOOL_PREF_MENU_FILES_ENABLED, GetPrefMenuFilesEnabled())
        self.SetBool(BCID_BOOL_PREF_MENU_SCENES_ENABLED, GetPrefMenuScenesEnabled())
        self.SetBool(BCID_BOOL_PREF_COMBO_ENABLED, GetPrefComboEnabled())
        self.SetBool(BCID_BOOL_PREF_LARGE_COMBO, GetPrefLargeCombo())
        self.SetBool(BCID_BOOL_PREF_TABS_ENABLED, GetPrefTabsEnabled())
        self.SetBool(BCID_BOOL_PREF_THUMB_ENABLED, GetPrefThumbEnabled())
        self.SetBool(BCID_BOOL_PREF_PREV_NEXT_BUTTONS_ENABLED, GetPrefPrevNextButtons())
        self.SetBool(BCID_BOOL_PREF_DESIGN_BUTTONS_ENABLED, GetPrefDesignButtons())
        self.SetBool(BCID_BOOL_PREF_POPUP_MODE, GetPrefPopupMode())
        self.SetBool(BCID_BOOL_PREF_SHOW_SHORTCUTS, GetPrefShowShortcuts())
        self.SetInt32(BCID_INT32_PREF_THUMB_SIZE, GetPrefThumbSize())
        self.SetInt32(BCID_INT32_PREF_THUMB_ASPECT_RATIO, GetPrefThumbAspectRatio())
        self.SetInt32(BCID_INT32_PREF_THUMB_UPDATES, GetPrefThumbUpdates())
        self.SetBool(BCID_BOOL_PREF_THUMB_TITLE, GetPrefThumbTitle())
        self.SetBool(BCID_BOOL_PREF_THUMB_HIDE_ACTIVE, GetPrefThumbHideActive())
        self.SetInt32(BCID_INT32_PREF_THUMB_FADE_ACTIVE, GetPrefThumbFadeActive(), min=0, max=100, step=1, tristate=False, min2=0, max2=100)
        self.SetInt32(BCID_INT32_PREF_THUMB_MARKER_ID, GetPrefThumbMarkerId())
        self.SetColorField(BCID_COLOR_PREF_THUMB_MARKER_COLOR, GetPrefThumbMarkerColor(), 1.0, 1.0, 0)
        self.SetColorField(BCID_COLOR_PREF_THUMB_LABEL_COLOR, GetPrefThumbLabelColor(), 1.0, 1.0, 0)
        self.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LT, GetPrefThumbOverlayLT())
        self.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RT, GetPrefThumbOverlayRT())
        self.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LB, GetPrefThumbOverlayLB())
        self.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RB, GetPrefThumbOverlayRB())
        self.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LABEL, GetPrefThumbOverlayLabel())
        self.SetBool(BCID_BOOL_PREF_NEW_PROJECT, GetPrefNewProject())

        self.EnableParameters()

        return True


    def ResetPreferencesToDefault(self):
        bcWorldPrefs = GetWorldPrefs()
        bcPrefsDefault = GetDefaultPrefContainer()

        for idParam, value in bcPrefsDefault:
            bcWorldPrefs[idParam] = value

        c4d.plugins.SetWorldPluginData(PLUGIN_ID, bcWorldPrefs, add=True)


    # TODO Andreas: Experimented with SpecialEventAdd() from PluginMessage to close dialogs. Unfortunately not working so far...
    #               Note: In the end a new message subid would be needed, as SUBID_COREMESSAGE_CLOSE_SSP is used from prefs to close only main dialog
    #def CoreMessage(self, id, msg):
    #    if id != PLUGIN_ID_COREMESSAGE:
    #        return c4d.gui.GeDialog.CoreMessage(self, id, msg)
    #    subIdCoreMessage = GetCoreMessageParam(msg, c4d.BFM_CORE_PAR1)
    #    if subIdCoreMessage == SUBID_COREMESSAGE_CLOSE_SSP:
    #        if self.IsOpen():
    #            self.Close()
    #    return c4d.gui.GeDialog.CoreMessage(self, id, msg)


    def Command(self, id, msg):
        if id == BCID_BOOL_PREF_PLUGIN_ENABLED:
            enabled = self.GetBool(id)
            SetPrefPluginEnabled(enabled)

            if enabled == False:
                c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CLOSE_SSP)

            self.EnableParameters()

        elif id == BCID_INT32_PREF_START_SESSION:
            SetPrefStartSession(self.GetInt32(id))

        elif id == BCID_BOOL_PREF_ASK_REOPEN:
            SetPrefAskReopen(self.GetBool(id))

        elif id == BCID_BOOL_PREF_ASK_SESSION_SWITCH:
            SetPrefAskSessionSwitch(self.GetBool(id))

        elif id == ID_BUTTON_PREF_MANAGE_SESSIONS:
            pass

        elif id == BCID_BOOL_PREF_MENU_FILES_ENABLED:
            SetPrefMenuFilesEnabled(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_MENU_SCENES_ENABLED:
            SetPrefMenuScenesEnabled(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_COMBO_ENABLED:
            SetPrefComboEnabled(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_LARGE_COMBO:
            SetPrefLargeCombo(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_TABS_ENABLED:
            SetPrefTabsEnabled(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_THUMB_ENABLED:
            SetPrefThumbEnabled(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_PREV_NEXT_BUTTONS_ENABLED:
            SetPrefPrevNextButtons(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_MENU_BUTTONS)
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_DESIGN_BUTTONS_ENABLED:
            SetPrefDesignButtons(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_MENU_BUTTONS)
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_BOOL_PREF_POPUP_MODE:
            SetPrefPopupMode(self.GetBool(id))

        elif id == BCID_BOOL_PREF_SHOW_SHORTCUTS:
            SetPrefShowShortcuts(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_INT32_PREF_THUMB_SIZE:
            SetPrefThumbSize(self.GetInt32(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_INT32_PREF_THUMB_ASPECT_RATIO:
            SetPrefThumbAspectRatio(self.GetInt32(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == BCID_INT32_PREF_THUMB_UPDATES:
            SetPrefThumbUpdates(self.GetInt32(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_THUMB_INTERVAL)

        elif id == BCID_BOOL_PREF_THUMB_TITLE:
            SetPrefThumbTitle(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_BOOL_PREF_THUMB_HIDE_ACTIVE:
            SetPrefThumbHideActive(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_INT32_PREF_THUMB_FADE_ACTIVE:
            SetPrefThumbFadeActive(self.GetInt32(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_INT32_PREF_THUMB_MARKER_ID:
            SetPrefThumbMarkerId(self.GetInt32(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_COLOR_PREF_THUMB_MARKER_COLOR:
            SetPrefThumbMarkerColor(self.GetColorField(id)['color'])
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_COLOR_PREF_THUMB_LABEL_COLOR:
            SetPrefThumbLabelColor(self.GetColorField(id)['color'])
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_BOOL_PREF_THUMB_OVERLAY_LT:
            SetPrefThumbOverlayLT(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_BOOL_PREF_THUMB_OVERLAY_RT:
            SetPrefThumbOverlayRT(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_BOOL_PREF_THUMB_OVERLAY_LB:
            SetPrefThumbOverlayLB(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_BOOL_PREF_THUMB_OVERLAY_RB:
            SetPrefThumbOverlayRB(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_BOOL_PREF_THUMB_OVERLAY_LABEL:
            SetPrefThumbOverlayLabel(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_REDRAW_THUMBS)

        elif id == BCID_BOOL_PREF_NEW_PROJECT:
            SetPrefNewProject(self.GetBool(id))
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)

        elif id == self.ID_BUTTON_RESET_TO_DEFAULT:
            self.ResetPreferencesToDefault()

            self.InitValues()

            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_DESIGN)
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_THUMB_INTERVAL)
            c4d.SpecialEventAdd(PLUGIN_ID_COREMESSAGE, p1=SUBID_COREMESSAGE_CHANGE_MENU_BUTTONS)

        elif id == self.ID_BUTTON_OPEN_SSP:
            c4d.CallCommand(PLUGIN_ID)

        elif id == c4d.DLG_OK:
            self.Close()

        return True
