<?php

use Contao\System;
use Symfony\Component\HttpFoundation\Request;

use Contao\Input;

/**
 * BACKEND GROUP
 */
$GLOBALS['BE_MOD']['form_submissions'] = [];
$GLOBALS['BE_MOD']['form_submissions']['form_submissions_statistics'] = [
    'callback'  => '\CreoCode\ContaoFormSubmissions\Classes\Statistics'
];
$GLOBALS['BE_MOD']['form_submissions']['form_submissions_data'] = [
    'tables'    => ['tl_form_submissions_set', 'tl_form_submissions', 'tl_form_submissions_data']
];


/**
 * HOOKS
 */
$GLOBALS['TL_HOOKS']['processFormData'][] = array('CreoCode\ContaoFormSubmissions\Classes\FormSubmissions', 'saveSubmission');

/**
 * Callback
 */
if (htmlspecialchars(Input::get('showForm'))) {
    $GLOBALS['BE_MOD']['form_submissions']['form_submissions_data']['callback'] = 'CreoCode\ContaoFormSubmissions\Classes\FormSubmissionsDataShow';
}
if (htmlspecialchars(Input::get('resendForm'))) {
    $GLOBALS['BE_MOD']['form_submissions']['form_submissions_data']['callback'] = 'CreoCode\ContaoFormSubmissions\Classes\FormSubmissionsDataResend';
}


/**
 * Register Models
 */
$GLOBALS['TL_MODELS'][\CreoCode\ContaoFormSubmissions\Model\FormSubmissionsModel::getTable()] = '\CreoCode\ContaoFormSubmissions\Model\FormSubmissionsModel';
$GLOBALS['TL_MODELS'][\CreoCode\ContaoFormSubmissions\Model\FormSubmissionsSetModel::getTable()] = '\CreoCode\ContaoFormSubmissions\Model\FormSubmissionsSetModel';
$GLOBALS['TL_MODELS'][\CreoCode\ContaoFormSubmissions\Model\FormSubmissionsDataModel::getTable()] = '\CreoCode\ContaoFormSubmissions\Model\FormSubmissionsDataModel';

/**
 * BACK END STYLESHEET
 */
if (System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest(System::getContainer()->get('request_stack')->getCurrentRequest() ?? Request::create(''))) {
    $GLOBALS['TL_CSS'][] = 'bundles/contaoformsubmissions/css/backend/backend.css';
}
/**
 * BACK END JAVASCRIPT
 */
//if (System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest(System::getContainer()->get('request_stack')->getCurrentRequest() ?? Request::create(''))) {
//    $GLOBALS['TL_JAVASCRIPT'][] = 'bundles/contaoformsubmissions/js/backend.js';
//}

/**
 * CRONJOBS
 */
$GLOBALS['TL_HOOKS']['generatePage'][] = [\CreoCode\ContaoFormSubmissions\Classes\Cron::class, 'dailyCron'];
