/*jslint         browser : true, continue : true,
  devel  : true, indent  : 2,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : true,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/

/*global $, app */

app.rfcases = (function () {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var
        stateMap = {},
        configModule, initModule,
        $cbProjects, $appList, $open, $priority, $search,
        projectList,
        onFilterChange, loadCases, getPriorityColorClass,
        restoreSelections;
    //----------------- END MODULE SCOPE VARIABLES ---------------

    //------------------- BEGIN UTILITY METHODS ------------------



    getPriorityColorClass = function (priority) {
        switch (priority) {
            case 1:
                return 'success';
                break;
            case 2:
                return 'warning';
                break;
            case 3:
                return 'danger';
                break;
            default:
                return 'secondary';
                break;
        }
    }


    //case 3363
    restoreSelections = function () {
        //if there are selections then restore them
        if (stateMap.savedSelections) {
            $cbProjects.val(stateMap.savedSelections.project);
            $open.prop('checked', stateMap.savedSelections.open);
            $priority.val(stateMap.savedSelections.priority);
            $search.val(stateMap.savedSelections.search);
        } else {
            //Defaults
            //select Rockfish as the default project
            $cbProjects.val(44);

        }
    }

    loadCases = function (projects) {

        $appList.empty();

        //get the filters
        // public JsonResult GetList(long? Project, bool? Open, int? Priority, string Search)
        var selectedProject = $cbProjects.val();
        var selectedOpen = $open.prop('checked')
        var selectedPriority = $priority.val();
        var selectedSearch = $search.val();

        stateMap.savedSelections = {
            project: selectedProject,
            open: selectedOpen,
            priority: selectedPriority,
            search: selectedSearch
        }

        if (selectedSearch) {
            selectedSearch = encodeURI(selectedSearch);
        }


        //case 3363 save settings here

        var filterUrl = '?project=' + selectedProject + '&open=' + selectedOpen + '&priority=' + selectedPriority + '&search=' + selectedSearch;
        var that = this;
        //get the cases
        app.api.get('rfcase/list' + filterUrl, function (res) {

            if (res.error) {
                $.gevent.publish('app-show-error', res.msg);
            } else {

                //case 3450 count
                $('#rf-list-count').empty().append(res.length+" items");

                $.each(res, function (i, obj) {

                    var badgeClass = getPriorityColorClass(obj.priority);
                    var idColumn = '<span class="rf-larger"><strong>' + obj.id + '</strong></span><span class="rf-smaller ml-2 badge badge-' + badgeClass + '">' + obj.priority + '</span>';



                    $appList.append("<li class='mdc-list-item'><a href=\"#!/rfcaseEdit/" + obj.id + "\">" +
                        app.utilB.genListColumn(idColumn) +
                        '&nbsp;' +
                        (selectedProject == 0 ? app.utilB.genListColumn(projects[obj.rfCaseProject_Id]) : '') +
                        '&nbsp;' +
                        app.utilB.genListColumn(obj.title) +
                        '&nbsp;' +
                        app.utilB.genListColumn(app.utilB.epochToLocalShortDateTime(obj.dtCreated)) +
                        "</a></li>");
                });
            }
        });
    }
    //-------------------- END UTILITY METHODS -------------------


    //------------------- BEGIN EVENT HANDLERS -------------------
    onFilterChange = function (event) {
        event.preventDefault();
        loadCases(projectList);
        return false; //prevent default
    };
    //-------------------- END EVENT HANDLERS --------------------

    //------------------- BEGIN PUBLIC METHODS -------------------
    //CONFIGMODULE
    //
    configModule = function (context) {
        stateMap.context = context.context;
        if (stateMap.context.params.id) {
            stateMap.id = stateMap.context.params.id;
        }
    };

    //INITMODULE
    //
    initModule = function ($container) {

        if (typeof $container === 'undefined') {
            $container = $('#app-shell-main-content');
        }
        $container.html(Handlebars.templates['app.rfcases']({}));

        //cache dom items
        $appList = $('#rf-list');
        $cbProjects = $('#projects');
        $open = $('#open');
        $priority = $("#priority");
        $search = $("#csearch");

        projectList = {};

        //get projects
        app.api.get('rfcaseproject', function (res) {
            if (res.error) {
                $.gevent.publish('app-show-error', res.msg);
            } else {

                var html = '<option value="0">All</option>';

                for (var i = 0, len = res.length; i < len; ++i) {
                    html += ('<option value="' + res[i]['id'] + '">' + res[i]['name'] + '</option>');
                    projectList[res[i]['id']] = res[i]['name'];
                }
                $cbProjects.append(html);


                //case 3363 re-hydrate settings here
                restoreSelections();

                //subscribe to change event
                $cbProjects.change(onFilterChange);
                $open.change(onFilterChange);
                $priority.change(onFilterChange);
                $search.change(onFilterChange);


                loadCases(projectList);
            }
        });

        app.nav.contextClear();
        app.nav.contextAddLink("rfcaseEdit/new", "New", "plus");
    };

    //PUBLIC METHODS
    //
    return {
        configModule: configModule,
        initModule: initModule
    };
    //------------------- END PUBLIC METHODS ---------------------
}());