import c4d
from ssp_ids import *
from ssp_dialog_options import DialogOptions


g_dlgOptions = None

# To be called during shutdown
def CmdOptionsDestroyGlobals():
    global g_dlgOptions

    if g_dlgOptions is not None:
        if not g_dlgOptions.Close():
            c4d.gui.MessageDialog('{0} SEVERE ISSUE: FAILED TO CLOSE OPTIONS DIALOG'.format(PLUGIN_NAME))
        g_dlgOptions = None


####################################################################################################
# Class CommandDataSSPOptions ######################################################################
####################################################################################################
class CommandDataSSPOptions(c4d.plugins.CommandData):
    _dlgOptions = None


    def Execute(self, doc):
        global g_dlgOptions

        if self._dlgOptions is None:
            self._dlgOptions = DialogOptions(self)
            g_dlgOptions = self._dlgOptions

        if self._dlgOptions.IsOpen():
            self._dlgOptions.Close()
            return True

        self._dlgOptions.Open(c4d.DLG_TYPE_ASYNC, PLUGIN_ID_OPTIONS, xpos=-1, ypos=-1, defaultw=500, defaulth=0) # -1: mouse pos, -2: screen center

        return True


    def RestoreLayout(self, secref):
        global g_dlgOptions

        if self._dlgOptions is None:
            self._dlgOptions = DialogMain(self)
            g_dlgOptions = self._dlgOptions

        return self._dlgOptions.Restore(PLUGIN_ID_OPTIONS, secref)


    def GetState(self, doc):
        state = c4d.CMD_ENABLED

        if self._dlgOptions is not None and self._dlgOptions.IsOpen():
            state |= c4d.CMD_VALUE

        return state
