import c4d
from ssp_ids import *
from ssp_utils_pref import *
from ssp_utils import *
from ssp_dialog_auth import TestAuth


####################################################################################################
# Class CommandDataSSPSwitch #######################################################################
####################################################################################################
class CommandDataSSPSwitch(c4d.plugins.CommandData):
    _dlgMain = None


    def Execute(self, doc):
        # Nothing to see here, go away!
        msg = '{0}: Dear user, obviously you tried to reconfigure a project switch keyboard shortcut!'.format(PLUGIN_NAME)
        msg += 'For this to work, you need to enable "Option Mode" for the shortcut in Customize Commands dialog.'

        c4d.gui.MessageDialog(msg)

        return True


    def ExecuteOptionID(self, doc, plugid, subid):
        if not TestAuth():
            return True

        idxSwitch = PLUGIN_IDS_SWITCH.index(plugid)

        bcScenesNames = GetScenesNames()

        idDocSwitch = -1
        for idxDoc, (idDoc, _) in enumerate(bcScenesNames):
            if idxDoc == idxSwitch:
                idDocSwitch = idDoc

        if idDocSwitch == -1:
            print('{0}: Switch doc ({1}) not found'.format(PLUGIN_NAME, idxSwitch))
            return True

        docSwitch = None
        doc = c4d.documents.GetFirstDocument()
        while doc is not None:
            _, idDocTest, _, _ = GetDocumentPathFull(doc)

            if idDocTest == idDocSwitch:
                docSwitch = doc
                break

            doc = doc.GetNext()

        if docSwitch is not None:
            c4d.documents.SetActiveDocument(docSwitch)
            c4d.EventAdd()

        return True
