<?php

defined('MOODLE_INTERNAL') || die();

// import various files logic is organized in
require_once($CFG->dirroot . '/mod/assign/submission/dta/models/DtaResult.php');
require_once($CFG->dirroot . '/mod/assign/submission/dta/utils/database.php');
require_once($CFG->dirroot . '/mod/assign/submission/dta/utils/backend.php');
require_once($CFG->dirroot . '/mod/assign/submission/dta/utils/view.php');

/**
 * library class for DTA submission plugin extending assign submission plugin base class
 *
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class assign_submission_dta extends assign_submission_plugin {

    // broadly used in logic, parametrized for easier change
    const COMPONENT_NAME = "assignsubmission_dta";

    // draft file area for dta tests to be uploaded by the teacher
    const ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST = "tests_draft_dta";
    // file area for dta tests to be uploaded by the teacher
    const ASSIGNSUBMISSION_DTA_FILEAREA_TEST = "tests_dta";
    // file area for dta submission assignment
    const ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION = "submissions_dta";

    // ========== abstract methods to be implemented ========== //

    /**
     * get plugin name
     * @return string
     */
    public function get_name(): string {
        return get_string("pluginname", self::COMPONENT_NAME);
    }

    // ========== end of section ========== //

    // ========== parent methods overloaded ========== //

        // ===== assignment settings ===== //
    /**
     * Get default settings for assignment submission settings
     *
     * @param MoodleQuickForm $mform form to add elements to
     * @return void
     */
    public function get_settings(MoodleQuickForm $mform): void {
        // add draft filemanager to form
        $mform->addElement(
            // filemanager
            "filemanager",
            // unique element name in form
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
            // label shown to user left of filemanager
            get_string("submission_settings_label", self::COMPONENT_NAME),
            // attributes
            null,
            // options array
            $this->get_file_options(true)
        );

        // add help button to added filemanager
        $mform->addHelpButton(
            // form-unique element id to add button to
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
            // key to search for
            "submission_settings_label",
            // language file to use
            self::COMPONENT_NAME
        );

        // only show filemanager, if our plugin is enabled
        $mform->hideIf(
            // form-unique element id to hide
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
            // condition to check
            self::COMPONENT_NAME . '_enabled',
            // state to match for hiding
            'notchecked'
        );
    }

    /**
     * Allows the plugin to update the defaultvalues passed in to
     * the settings form (needed to set up draft areas for editor
     * and filemanager elements)
     * @param array $defaultvalues
     */
    public function data_preprocessing(&$defaultvalues): void {
        $draftitemid = file_get_submitted_draft_itemid(self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST);

        // prepare draft area with created draft filearea
        file_prepare_draft_area(
            // draft filemanager form-unique id
            $draftitemid,
            // id of current assignment
            $this->assignment->get_context()->id,
            // component name
            self::COMPONENT_NAME,
            // proper filearea
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            // entry id
            0,
            // options array?
            array('subdirs' => 0)
        );

        $defaultvalues[self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST] = $draftitemid;
    }

    /**
     * Save settings of assignment submission settings
     *
     * @param stdClass $data
     * @return bool
     */
    public function save_settings(stdClass $data): bool {

        // if the assignment has no filemanager for our plugin just leave
        $draftFileManagerId = self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST;
        if (!isset($data->$draftFileManagerId)) {
            return true;
        }

        // store files from draft filearea to proper one
        file_save_draft_area_files(
            // form-unique element id of draft filemanager from the edit
            $data->$draftFileManagerId,
            // id of the assignment we edit right now
            $this->assignment->get_context()->id,
            // component name
            self::COMPONENT_NAME,
            // proper file area
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            // entry id
            0
        );

        // get files from proper filearea
        $fs = get_file_storage();
        $files = $fs->get_area_files(
            // id of current assignment
            $this->assignment->get_context()->id,
            // component name
            self::COMPONENT_NAME,
            // proper filearea
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            // entry id
            0,
            // ?
            'id',
            // ?
            false
        );

        // check if a file is uploaded
        if (empty($files)) {
            \core\notification::error(get_string("no_testfile_warning", self::COMPONENT_NAME));
            return true;
        }

        // get file
        $file = reset($files);

        // send file to backend
        return DtaBackendUtils::sendTestConfigToBackend($this->assignment, $file);
    }

        // ===== student submission ===== //

    /**
     * Add elements to submission form
     *
     * @param mixed $submission stdClass|null
     * @param MoodleQuickForm $mform
     * @param stdClass $data
     * @param int $userid 
     * @return bool
     */
    public function get_form_elements_for_user($submissionorgrade, MoodleQuickForm $mform, stdClass $data, $userid): bool {
        // prepare submission filearea
        $data = file_prepare_standard_filemanager(
            $data,
            'tasks',
            $this->get_file_options(false),
            $this->assignment->get_context(),
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submissionorgrade ? $submissionorgrade->id : 0
        );

        // add filemanager to form
        $mform->addElement(
            // filemanager
            'filemanager',
            // form-unique identifier
            'tasks_filemanager',
            // label to show next to filemanager
            get_string("submission_label", self::COMPONENT_NAME),
            // attributes
            null,
            // options
            $this->get_file_options(false)
        );

        // add help button
        $mform->addHelpButton(
            // what form item to add a helpbutton
            "tasks_filemanager",
            // what key to use
            "submission_label",
            // in which language file to look in
            self::COMPONENT_NAME
        );

        return true;
    }

    /**
     * @param stdClass $submission submission to check
     * @return bool true if file count is zero
     */
    public function is_empty(stdClass $submission): bool {
        return $this->count_files($submission->id, self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION) == 0;
    } 

    /**
     * Count the number of files in a filearea
     *
     * @param int $submissionId submission id to check
     * @param string $areaId filearea id to count
     * @return int
     */
    private function count_files($submissionId, $areaId) {
        $fs = get_file_storage();
        $files = $fs->get_area_files($this->assignment->get_context()->id,
            self::COMPONENT_NAME,
            $areaId,
            $submissionId,
            'id',
            false);

        return count($files);
    }

    /**
     * Save data to the database
     *
     * @param stdClass $submission
     * @param stdClass $data
     * @return bool
     */
    public function save(stdClass $submission, stdClass $data) {
        $data = file_postupdate_standard_filemanager(
            $data,
            'tasks',
            $this->get_file_options(false),
            $this->assignment->get_context(),
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id
        );

        // if submission is empty leave directly
        if ($this->is_empty($submission)) {
            return true;
        }

        // get submitted files
        $fs = get_file_storage();
        $files = $fs->get_area_files(
            // id of current assignment
            $this->assignment->get_context()->id,
            // component name
            self::COMPONENT_NAME,
            // proper filearea
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            // entry id
            $submission->id,
            // ?
            'id',
            // ?
            false
        );

        // check if a file is uploaded
        if (empty($files)) {
            \core\notification::error(get_string("no_submissionfile_warning", self::COMPONENT_NAME));
            return true;
        }

        // Get the file and post it to our backend.
        $file = reset($files);

        $response = DtaBackendUtils::sendSubmissionToBackend($this->assignment, $file);

        // if we got a null response, return with error
        if (is_null($response)) {
            return false;
        }

        // convert received json to valid class instances
        $resultSummary = DtaResultSummary::decodeJson($response);

        // persist new results to database
        DbUtils::storeResultSummaryToDatabase($this->assignment->get_instance()->id, $submission->id, $resultSummary);

        return true;
    }

        // ===== view submission results ===== //

    /**
     * Display a short summary of the test results of the submission
     * This is diplayed as default view, with the option to expand
     * to the full detailed results.
     *
     * @param stdClass $submission to show
     * @param bool $showviewlink configuration variable to show expand option
     * @return string summary results html
     */
    public function view_summary(stdClass $submission, & $showviewlink) {
        $showviewlink = true;

        return ViewSubmissionUtils::generateSummaryHtml(
            $this->assignment->get_instance()->id,
            $submission->id
        );
    }

    /**
     * Display detailed results
     *
     * @param stdClass $submission the submission the results are shown for.
     * @return string detailed results html
     */
    public function view(stdClass $submission) {
        return ViewSubmissionUtils::generateDetailHtml(
            $this->assignment->get_instance()->id,
            $submission->id
        );
    }

    // ========== end of section ========== //

    /**
     * generate array of allowed filetypes to upload.
     *
     * @param bool $settings switch to define if list for assignment settings
     *      or active submission should be returned
     *
     * @return array
     */
    private function get_file_options(bool $settings): array {
        $fileoptions = array('subdirs' => 0,
            "maxfiles" => 1,
            'accepted_types' => ($settings ? array(".txt") : array(".txt",".zip")),
            'return_types' => FILE_INTERNAL);
        return $fileoptions;
    }

    /**
     * Get file areas returns a list of areas this plugin stores files
     * @return array - An array of fileareas (keys) and descriptions (values)
     */
    public function get_file_areas() {
        return array(
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION => get_string("dta_submissions_fa", self::COMPONENT_NAME),
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST => get_string("dta_tests_fa", self::COMPONENT_NAME)
        );
    }

    /**
     * Produce a list of files suitable for export that represent this feedback or submission
     *
     * @param stdClass $submission The submission
     * @param stdClass $user The user record - unused
     * @return array - return an array of files indexed by filename
     */
    public function get_files(stdClass $submission, stdClass $user) {
        $result = array();
        $fs = get_file_storage();
        $files = $fs->get_area_files($this->assignment->get_context()->id,
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id,
            'timemodified',
            false);

        foreach ($files as $file) {
            // Do we return the full folder path or just the file name?
            if (isset($submission->exportfullpath) && $submission->exportfullpath == false) {
                $result[$file->get_filename()] = $file;
            } else {
                $result[$file->get_filepath().$file->get_filename()] = $file;
            }
        }
        return $result;
    }

    /**
     * The plugin is beeing uninstalled - cleanup
     *
     * @return bool
     */
    public function delete_instance() {
        DbUtils::uninstallPluginCleanUp();

        return true;
    }
}