/**
 * @var modX $modx
 * @var array $scriptProperties
 * @var StaticElementsLive $staticelementslive
 * @var StaticElementsLiveTools $tools
 * @var modElement $object
 */

$staticelementslive = $modx->getService('staticelementslive', 'StaticElementsLive');
$tools = $staticelementslive->getTools();

switch ($modx->event->name) {
    case 'OnManagerPageBeforeRender':
        $staticelementslive->loadControllerJsCss($modx->controller, 'manager');
        break;
    case 'OnManagerPageInit':
        $autoScan = $modx->getOption('staticelementslive_auto_scan');
        $usergroups = $tools->explodeAndClean($modx->getOption('staticelementslive_usergroups'));
        if (!$autoScan || !$modx->user->isMember($usergroups)) return;
        /** @var modProcessorResponse $response */
        $response = $staticelementslive->runProcessor('web/element/scan');
        break;
    case 'OnTempFormPrerender':
    case 'OnChunkFormPrerender':
    case 'OnPluginFormPrerender':
    case 'OnSnipFormPrerender':
        $staticelementslive->loadControllerJsCss($modx->controller);
        break;
    case 'OnBeforeTempFormSave':
    case 'OnBeforeChunkFormSave':
    case 'OnBeforeSnipFormSave':
    case 'OnBeforePluginFormSave':
        $className = $tools->getElementClassName($object);
        $fieldName = $tools->getElementKeyFieldName($className);
        if ($object->get('auto_create_static')) {
            $path = $tools->getElementPathByClassName($className);
            $path = $tools->normalizePath($path . '/');
            $sourceId = $tools->getMediaSourceId();
            $filename = $object->get($fieldName);
            $filename = $tools->prepareFileName($filename);
            if ($object->get('category')) {
                $categoryPath = $tools->getElementCategoryPath($object->get('category'));
                if ($categoryPath) {
                    $path .= $categoryPath;
                }
            }
            $file = $path . $filename . $tools->getElementFileExtension($className);
            $staticFile = $tools->prepareFileMediaSourcePath($sourceId, $file);
            $object->set('static', 1);
            $object->set('source', $sourceId);
            $object->set('static_file', $staticFile);
        } else if (
            $object->get('static') &&
            $object->isDirty($fieldName) &&
            $modx->getOption('staticelementslive_rename_file')
        ) {
            $oldStaticFile = $object->get('static_file');
            $oldFileName = pathinfo($oldStaticFile, PATHINFO_FILENAME);
            $oldFileName = $tools->prepareElementName($oldFileName);
            $fieldName = $tools->getElementKeyFieldName($className);
            $newFileName = $object->get($fieldName);
            $newStaticFile = str_replace($oldFileName, $newFileName, $oldStaticFile);
            $object->set('static_file', $newStaticFile);
            $modx->setOption('static_elements_automate_' . $tools->getElementType($className), true);
        }
        break;
    case 'OnTempFormDelete':
    case 'OnChunkFormDelete':
    case 'OnSnipFormDelete':
    case 'OnPluginFormDelete':
        if ($object->get('static') &&
            $modx->getOption('staticelementslive_remove_file')
        ) {
            $file = $object->getSourceFile();
            if (file_exists($file)) {
                unlink($file);
            }
        }
        break;
}
return;