// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.


define(['jquery'], function ($) {
    function search(searchstring, reloadBool, displaymode, instanceid, sesskey, contextid, idsearching, orderbysemester, wwwroot) {
        if (reloadBool) {
            $("#hshcourseprogress").show();
            $.ajax({
                type: 'GET',
                url: wwwroot + '/blocks/hshcourselist/hshcourselist.php',
                data: 'course=' + searchstring + '&instanceid=' + instanceid
                    + '&sesskey=' + sesskey + '&contextid=' + contextid
                    + '&idsearching=' + idsearching + '&orderbysemester=' + orderbysemester,
                dataType: "json",
                success: function (response) {
                    var plainJSON = JSON.stringify(response);
                    var courses = JSON.parse(plainJSON);
                    var list = $("<ul class='list-group' id='hshcourselistul'></ul>");
                    if (courses.length > 0) {
                        $.each(courses, function (index, course) {
                            switch (displaymode) {
                                case '0':
                                    displaystr = course.shortname;
                                    break;

                                case '1':
                                    displaystr = course.fullname;
                                    break;

                                case '2':
                                    displaystr = course.shortname + ': ' + course.fullname;
                                    break;

                                default:
                                    displaystr = course.fullname;
                                    break;
                            }
                            $('<li class="list-group-item hshcoursesearchlistitem " value="' + course.id + '">'
                                + '<a href="' + wwwroot + '/course/view.php?id=' + course.id + '">' + displaystr + '</a> </li>')
                                .appendTo(list);
                        });
                    }
                    $("#hshcourselistul").replaceWith(list);
                    $("#hshcourseprogress").hide();
                },
                error: function (a) {
                    console.error("error " + JSON.stringify(a));
                }
            });
        } else {
        }
    }

    return {
        init: function (jsdataobject, wwwroot) {
            var jsdataobject = jsdataobject;
            var instanceid = jsdataobject.instanceid;
            var sesskey = jsdataobject.sesskey;
            var displaymode = jsdataobject.displaymode;
            var contextid = jsdataobject.contextid;
            var reloadList = false;
            var idsearching = false;
            var searchstring;
            var orderbysemester = jsdataobject.orderbysemester;
            var displaystr;

            $("#hshcourseprogress").hide();
            $(".idSarchingLabel").hide();

            $('#orderbysemestercheckbox_id').on('change', function () {
                orderbysemester = document.getElementById('orderbysemestercheckbox_id').checked;
                reloadList = true;
                searchstring = document.getElementById('hshcourselistsearch').value;
                searchstring = searchstring.replace(/%/g, '');
                searchstring = searchstring.trim();
                if (searchstring.slice(0, 1) === '#') {
                    $(".idSarchingLabel").show();
                    idsearching = true;
                    searchstring = searchstring.slice(1);
                } else {
                    $(".idSarchingLabel").hide();
                    idsearching = false;
                }
                if (idsearching == true || searchstring.length >= 3) {
                    search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idsearching, orderbysemester, wwwroot);
                } else {
                    // Search start after 3 chars or searching by id.
                }
            });

            $('#hshcourselistsearch').on('keyup', function (e) {
                var key = e.keyCode;
                orderbysemester = document.getElementById('orderbysemestercheckbox_id').checked;
                if (key !== 40 && key !== 38 && key !== 13) {
                    reloadList = true;
                    searchstring = document.getElementById('hshcourselistsearch').value;
                    searchstring = searchstring.replace(/%/g, '');
                    searchstring = searchstring.trim();
                    if (searchstring.slice(0, 1) === '#') {
                        $(".idSarchingLabel").show();
                        idsearching = true;
                        searchstring = searchstring.slice(1);
                    } else {
                        $(".idSarchingLabel").hide();
                        idsearching = false;
                    }
                    if (idsearching == true || searchstring.length >= 3) {
                        search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idsearching, orderbysemester, wwwroot);
                    } else {
                        // Search start after 3 chars or searching by id.
                        $('#' + parentId).children().remove();
                    }
                }
            });
            $('#hshcourseform').on('submit', function (e) {
                e.preventDefault;
                reloadList = true;
                idsearching = false;
                orderbysemester = document.getElementById('orderbysemestercheckbox_id').checked;
                searchstring = document.getElementById('hshcourselistsearch').value;
                searchstring = searchstring.replace(/%/g, '');
                searchstring = searchstring.trim();
                if (searchstring.slice(0, 1) === '#') {
                    idsearching = true;
                    searchstring = searchstring.slice(1);
                } else {
                    idsearching = false;
                }
                if (idsearching == true || searchstring.length >= 3) {
                    search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idsearching, orderbysemester, wwwroot);
                } else {
                    // Search start after 3 chars or searching by id.
                }
            });
        },
    }
});