/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*global define, tau, document */

/**
 * UI helper module.
 * @requires {@link core/template}
 * @namespace SoundManager/helpers/ui
 * @memberof SoundManager/helpers
 */
define({
    name: 'helpers/ui',
    requires: [
        'core/template'
    ],
    def: function ui(template) {
        'use strict';

        /**
         * Reference to message popup element.
         * @type {HTMLElement}
         */
        var messagePopup = null,
            /**
             * Reference to message content element.
             * @type {HTMLElement}
             */
            messageContent = null;

        /**
         * Creates HTML content of the list and fills the specified 'ul' element
         * with this content.
         * The content will be composed of 'li' elements created using specified
         * template.
         * @memberof SoundManager/helpers/ui
         * @param {HTMLElement} list Element which will be filled with created
         * content.
         * @param {array} templateArgs List of objects. Each object will be
         * passed to the template module in order to create the 'li' item.
         * @param {string} templateName Name of template.
         */
        function fillList(list, templateArgs, templateName) {
            var result = '', i = 0;

            for (i = 0; i < templateArgs.length; i += 1) {
                result += template.get(templateName, templateArgs[i]);
            }

            list.innerHTML = result;
            tau.engine.createWidgets(list);
            tau.widget.Listview(list).refresh();
        }

        /**
         * Opens message popup with a given message text.
         * @memberof SoundManager/helpers/ui
         * @param {string} message
         */
        function showMessagePopup(message) {
            var popup = tau.widget.Popup(messagePopup);
            messageContent.innerText = message;
            popup.open();
        }

        /**
         * Opens 'add profile' popup.
         * @memberof SoundManager/helpers/ui
         */
        function openPopup(id) {
            tau.widget.Popup(document.getElementById(id)).open();
        }

        /**
         * Initializes module.
         * @memberof SoundManager/helpers/ui
         */
        function init() {
            messagePopup = document.getElementById('messagePopup');
            messageContent = document.getElementById('messageContent');
        }

        return {
            init: init,
            fillList: fillList,
            showMessagePopup: showMessagePopup,
            openPopup: openPopup
        };
    }
});
