import c4d
from ssp_utils_c4d_version import *
from ssp_utils_image import *
from ssp_ids import *


def GetAvailableMarker(subfolder='img'):
    availableMarker = {}
    pathImg = os.path.join(os.path.dirname(__file__), subfolder)

    for filenameMarker in os.listdir(pathImg):
        if filenameMarker[:6] != 'marker' or (filenameMarker[-4:] != '.png' and filenameMarker[-4:] != '.jpg'):
            continue

        bmpMarker = LoadImage(filenameMarker, subfolder=subfolder, suffix='')
        idMarker = MyHash(filenameMarker)

        availableMarker[idMarker] = (filenameMarker, bmpMarker, idMarker)

    return availableMarker


def GetDefaultPrefContainer(): # TODO Andreas: shouldn't be needed, if prefdata defaults worked correctly (which most likely is an issue in this code...)
    bcDefaultPrefs = c4d.BaseContainer()

    if bcDefaultPrefs is None:
        print('{0} ERROR: PrefContainer! Houston, we have got a problem!'.format(PLUGIN_NAME))
        return None

    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_PLUGIN_ENABLED, DEFAULT_PLUGIN_ENABLED)
    bcDefaultPrefs.SetInt32(BCID_INT32_PREF_START_SESSION, DEFAULT_START_SESSION)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_ASK_SESSION_SWITCH, DEFAULT_ASK_SESSION_SWITCH)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_ASK_REOPEN, DEFAULT_ASK_REOPEN)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_MENU_FILES_ENABLED, DEFAULT_MENU_FILES_ENABLED)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_MENU_SCENES_ENABLED, DEFAULT_MENU_SCENES_ENABLED)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_COMBO_ENABLED, DEFAULT_COMBO_ENABLED)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_LARGE_COMBO, DEFAULT_LARGE_COMBO)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_TABS_ENABLED, DEFAULT_TABS_ENABLED)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_ENABLED, DEFAULT_THUMB_ENABLED)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_PREV_NEXT_BUTTONS_ENABLED, DEFAULT_PREV_NEXT_BUTTONS_ENABLED)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_DESIGN_BUTTONS_ENABLED, DEFAULT_DESIGN_BUTTONS_ENABLED)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_POPUP_MODE, DEFAULT_POPUP_MODE)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_SHOW_SHORTCUTS, DEFAULT_SHOW_SHORTCUTS)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_MODE_RENDER, DEFAULT_THUMB_MODE_RENDER)
    bcDefaultPrefs.SetInt32(BCID_INT32_PREF_THUMB_SIZE, DEFAULT_THUMB_SIZE)
    bcDefaultPrefs.SetInt32(BCID_INT32_PREF_THUMB_MARKER_ID, DEFAULT_THUMB_MARKER_ID)
    bcDefaultPrefs.SetInt32(BCID_INT32_PREF_THUMB_ASPECT_RATIO, DEFAULT_THUMB_ASPECT_RATIO)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_FILTER, DEFAULT_THUMB_FILTER)
    bcDefaultPrefs.SetInt32(BCID_INT32_PREF_THUMB_UPDATES, DEFAULT_THUMB_UPDATES)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_HIDE_ACTIVE, DEFAULT_THUMB_HIDE_ACTIVE)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_TITLE, DEFAULT_THUMB_TITLE)
    bcDefaultPrefs.SetInt32(BCID_INT32_PREF_THUMB_FADE_ACTIVE, DEFAULT_THUMB_FADE_ACTIVE)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LT, DEFAULT_THUMB_OVERLAY_LT)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RT, DEFAULT_THUMB_OVERLAY_RT)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LB, DEFAULT_THUMB_OVERLAY_LB)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RB, DEFAULT_THUMB_OVERLAY_RB)
    bcDefaultPrefs.SetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LABEL, DEFAULT_THUMB_OVERLAY_LABEL)
    bcDefaultPrefs.SetVector(BCID_COLOR_PREF_THUMB_MARKER_COLOR, DEFAULT_THUMB_MARKER_COLOR)
    bcDefaultPrefs.SetVector(BCID_COLOR_PREF_THUMB_LABEL_COLOR, DEFAULT_THUMB_LABEL_COLOR)
    #bcDefaultPrefs.SetInt32(BCID_INT32_PREF_RELOAD_LAYOUT, DEFAULT_RELOAD_LAYOUT)

    return bcDefaultPrefs


def GetWorldPrefs():

    bcWorldPrefs = c4d.plugins.GetWorldPluginData(PLUGIN_ID)

    if bcWorldPrefs is None:
        bcWorldPrefs = GetDefaultPrefContainer()
        c4d.plugins.SetWorldPluginData(PLUGIN_ID, bcWorldPrefs, True)

    return bcWorldPrefs


def GetPrefsContainer(idBc):
    bcWorldPrefs = GetWorldPrefs()

    if bcWorldPrefs is None:
        return None

    bc = bcWorldPrefs.GetContainerInstance(idBc)

    if bc is None:
        bcWorldPrefs.SetContainer(idBc, c4d.BaseContainer())

        c4d.plugins.SetWorldPluginData(PLUGIN_ID, bcWorldPrefs, add=True)
        bc = bcWorldPrefs.GetContainerInstance(idBc) # just to be sure we return the correct instance

    return bc


def SetPrefsContainer(idBc, bc):
    bcWorldPrefs = GetWorldPrefs()

    if bcWorldPrefs is None:
        return None

    bcWorldPrefs.SetContainer(idBc, bc)

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


def SetPrefBool(idParam, flag):
    bcWorldPrefs = GetWorldPrefs()

    bcWorldPrefs.SetBool(idParam, flag)

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


def SetPrefInt32(idParam, value):
    bcWorldPrefs = GetWorldPrefs()

    bcWorldPrefs.SetInt32(idParam, value)

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


def SetPrefVector(idParam, vec):
    bcWorldPrefs = GetWorldPrefs()

    bcWorldPrefs.SetVector(idParam, vec)

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


def GetPrefPluginEnabled():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_PLUGIN_ENABLED, DEFAULT_PLUGIN_ENABLED)

def SetPrefPluginEnabled(enable):
    SetPrefBool(BCID_BOOL_PREF_PLUGIN_ENABLED, enable)


def GetPrefStartSession():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetInt32(BCID_INT32_PREF_START_SESSION, DEFAULT_START_SESSION)

def SetPrefStartSession(idSession):
    SetPrefInt32(BCID_INT32_PREF_START_SESSION, idSession)


def GetPrefAskReopen():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_ASK_REOPEN, DEFAULT_ASK_REOPEN)

def SetPrefAskReopen(enabled):
    SetPrefBool(BCID_BOOL_PREF_ASK_REOPEN, enabled)


def GetPrefAskSessionSwitch():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_ASK_SESSION_SWITCH, DEFAULT_ASK_SESSION_SWITCH)

def SetPrefAskSessionSwitch(enabled):
    SetPrefBool(BCID_BOOL_PREF_ASK_SESSION_SWITCH, enabled)


def GetPrefMenuFilesEnabled():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_MENU_FILES_ENABLED, DEFAULT_MENU_FILES_ENABLED)

def SetPrefMenuFilesEnabled(enabled):
    SetPrefBool(BCID_BOOL_PREF_MENU_FILES_ENABLED, enabled)


def GetPrefMenuScenesEnabled():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_MENU_SCENES_ENABLED, DEFAULT_MENU_SCENES_ENABLED)

def SetPrefMenuScenesEnabled(enabled):
    SetPrefBool(BCID_BOOL_PREF_MENU_SCENES_ENABLED, enabled)


def GetPrefTabsEnabled():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_TABS_ENABLED, DEFAULT_THUMB_ENABLED)

def SetPrefTabsEnabled(enabled):
    SetPrefBool(BCID_BOOL_PREF_TABS_ENABLED, enabled)


def GetPrefComboEnabled():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_COMBO_ENABLED, DEFAULT_COMBO_ENABLED)

def SetPrefComboEnabled(enabled):
    SetPrefBool(BCID_BOOL_PREF_COMBO_ENABLED, enabled)


def GetPrefLargeCombo():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_LARGE_COMBO, DEFAULT_LARGE_COMBO)

def SetPrefLargeCombo(enabled):
    SetPrefBool(BCID_BOOL_PREF_LARGE_COMBO, enabled)


def GetPrefThumbEnabled():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_ENABLED, DEFAULT_THUMB_ENABLED)

def SetPrefThumbEnabled(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_ENABLED, enabled)


def GetPrefPrevNextButtons():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_PREV_NEXT_BUTTONS_ENABLED, DEFAULT_PREV_NEXT_BUTTONS_ENABLED)

def SetPrefPrevNextButtons(enabled):
    SetPrefBool(BCID_BOOL_PREF_PREV_NEXT_BUTTONS_ENABLED, enabled)


def GetPrefDesignButtons():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_DESIGN_BUTTONS_ENABLED, DEFAULT_DESIGN_BUTTONS_ENABLED)

def SetPrefDesignButtons(enabled):
    SetPrefBool(BCID_BOOL_PREF_DESIGN_BUTTONS_ENABLED, enabled)


def GetPrefPopupMode():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_POPUP_MODE, DEFAULT_POPUP_MODE)

def SetPrefPopupMode(enabled):
    SetPrefBool(BCID_BOOL_PREF_POPUP_MODE, enabled)


def GetPrefShowShortcuts():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_SHOW_SHORTCUTS, DEFAULT_SHOW_SHORTCUTS)

def SetPrefShowShortcuts(enabled):
    SetPrefBool(BCID_BOOL_PREF_SHOW_SHORTCUTS, enabled)


def GetPrefThumbModeRender():
    return True
    # TODO Andreas: reenable if preview mode is usable (if ever...)
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_MODE_RENDER, DEFAULT_THUMB_MODE_RENDER)


def GetPrefThumbSize():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetInt32(BCID_INT32_PREF_THUMB_SIZE, DEFAULT_THUMB_SIZE)

def SetPrefThumbSize(thumbSize):
    SetPrefInt32(BCID_INT32_PREF_THUMB_SIZE, thumbSize)


def GetPrefThumbFilter():
    if c4d.GetC4DVersion() < 22000:
        return False

    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_FILTER, DEFAULT_THUMB_FILTER)

def SetPrefThumbFilter(enabled):
    if c4d.GetC4DVersion() < 22000:
        enabled = False

    SetPrefBool(BCID_BOOL_PREF_THUMB_FILTER, enabled)


def GetPrefThumbUpdates():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetInt32(BCID_INT32_PREF_THUMB_UPDATES, DEFAULT_THUMB_UPDATES)

def SetPrefThumbUpdates(thumbUpdateInterval):
    SetPrefInt32(BCID_INT32_PREF_THUMB_UPDATES, thumbUpdateInterval)


def GetPrefThumbUpdateIntervalEffective():
    bcWorldPrefs = GetWorldPrefs()
    interval = bcWorldPrefs.GetInt32(BCID_INT32_PREF_THUMB_UPDATES, DEFAULT_THUMB_UPDATES)

    if interval < 10:
        interval = 0

    return interval


def GetPrefThumbHideActive():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_HIDE_ACTIVE, DEFAULT_THUMB_HIDE_ACTIVE)

def SetPrefThumbHideActive(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_HIDE_ACTIVE, enabled)


def GetPrefThumbOverlay():
    bcWorldPrefs = GetWorldPrefs()
    overlay = 0

    if bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LT, DEFAULT_THUMB_OVERLAY_LT):
        overlay |= 0x01

    if bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RT, DEFAULT_THUMB_OVERLAY_RT):
        overlay |= 0x02

    if bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LB, DEFAULT_THUMB_OVERLAY_LB):
        overlay |= 0x04

    if bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RB, DEFAULT_THUMB_OVERLAY_RB):
        overlay |= 0x08

    if bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LABEL, DEFAULT_THUMB_OVERLAY_LABEL):
        overlay |= 0x10

    return overlay


def GetPrefThumbOverlayLT():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LT, DEFAULT_THUMB_OVERLAY_LT)

def SetPrefThumbOverlayLT(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_OVERLAY_LT, enabled)


def GetPrefThumbOverlayRT():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RT, DEFAULT_THUMB_OVERLAY_RT)

def SetPrefThumbOverlayRT(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_OVERLAY_RT, enabled)


def GetPrefThumbOverlayLB():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LB, DEFAULT_THUMB_OVERLAY_LB)

def SetPrefThumbOverlayLB(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_OVERLAY_LB, enabled)


def GetPrefThumbOverlayRB():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_RB, DEFAULT_THUMB_OVERLAY_RB)

def SetPrefThumbOverlayRB(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_OVERLAY_RB, enabled)


def GetPrefThumbOverlayLabel():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_OVERLAY_LABEL, DEFAULT_THUMB_OVERLAY_LABEL)

def SetPrefThumbOverlayLabel(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_OVERLAY_LABEL, enabled)


def GetPrefThumbMarkerColor():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetVector(BCID_COLOR_PREF_THUMB_MARKER_COLOR, DEFAULT_THUMB_MARKER_COLOR)

def SetPrefThumbMarkerColor(color):
    SetPrefVector(BCID_COLOR_PREF_THUMB_MARKER_COLOR, color)


def GetPrefThumbLabelColor():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetVector(BCID_COLOR_PREF_THUMB_LABEL_COLOR, DEFAULT_THUMB_LABEL_COLOR)

def SetPrefThumbLabelColor(color):
    SetPrefVector(BCID_COLOR_PREF_THUMB_LABEL_COLOR, color)


def GetPrefThumbTitle():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_THUMB_TITLE, DEFAULT_THUMB_TITLE)

def SetPrefThumbTitle(enabled):
    SetPrefBool(BCID_BOOL_PREF_THUMB_TITLE, enabled)


def GetPrefThumbMarkerId():
    availableMarker = GetAvailableMarker()
    if len(availableMarker) <= 0:
        print('{0} ERROR: No marker images!'.format(PLUGIN_NAME))
        return None

    bcWorldPrefs = GetWorldPrefs()

    idMarker = bcWorldPrefs.GetInt32(BCID_INT32_PREF_THUMB_MARKER_ID, DEFAULT_THUMB_MARKER_ID)

    if idMarker not in availableMarker:
        # configured marker image seems to be missing, we'll take first available instead
        print('{0} ERROR: Configured marker image {1} not available!'.format(PLUGIN_NAME, idMarker))
        #idMarker = availableMarker.keys()[0] # TODO Py3

        for key in availableMarker.keys():
            idMarker = key
            break

        SetPrefThumbMarkerId(idMarker)

    return idMarker

def SetPrefThumbMarkerId(idMarker):
    SetPrefInt32(BCID_INT32_PREF_THUMB_MARKER_ID, idMarker)


def GetPrefThumbMarkerImage():
    availableMarker = GetAvailableMarker()
    idMarker = GetPrefThumbMarkerId()

    if idMarker is None:
        return None

    return availableMarker[idMarker][1]

def GetPrefThumbMarkerName():
    availableMarker = GetAvailableMarker()
    idMarker = GetPrefThumbMarkerId()

    if idMarker is None:
        return None

    return availableMarker[idMarker][0]


def GetPrefThumbFadeActive():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetInt32(BCID_INT32_PREF_THUMB_FADE_ACTIVE, DEFAULT_THUMB_FADE_ACTIVE)

def SetPrefThumbFadeActive(valueFade):
    SetPrefInt32(BCID_INT32_PREF_THUMB_FADE_ACTIVE, valueFade)


def GetPrefThumbAspectRatio():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetInt32(BCID_INT32_PREF_THUMB_ASPECT_RATIO, DEFAULT_THUMB_ASPECT_RATIO)

def SetPrefThumbAspectRatio(idAspectRatio):
    SetPrefInt32(BCID_INT32_PREF_THUMB_ASPECT_RATIO, idAspectRatio)


def GetPrefNewProject():
    bcWorldPrefs = GetWorldPrefs()
    return bcWorldPrefs.GetBool(BCID_BOOL_PREF_NEW_PROJECT, DEFAULT_NEW_PROJECT)

def SetPrefNewProject(enabled):
    SetPrefBool(BCID_BOOL_PREF_NEW_PROJECT, enabled)


#def GetPrefReloadLayout():
#    bcWorldPrefs = GetWorldPrefs()
#    return bcWorldPrefs.GetInt32(BCID_INT32_PREF_RELOAD_LAYOUT, DEFAULT_RELOAD_LAYOUT)
