/*
 * 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, document, tau, setTimeout */

/**
 * Notification helper module.
 * @namespace JPEGExifEditor/helpers/notification
 * @memberof JPEGExifEditor/helpers
 */
define({
    name: 'helpers/notification',
    def: function notificationHelperModule() {
        'use strict';

        /**
         * Notification widget displaying 'Success' message.
         * @type {object}
         */
        var successNotification = null,

            /**
             * Notification widget displaying 'Failed' message.
             * @type {object}
             */
            failedNotification = null,

            /**
             * Reference to the message field on the 'Failed' notification.
             * @type {HTMLElement}
             */
            errorMessage = null,

            /**
             * Delay after which the notification disappears.
             * @readonly
             * @type {number}
             */
            NOTIFICATION_DELAY = 1000;

        /**
         * Displays the 'Success' notification.
         * @memberof JPEGExifEditor/helpers/notification
         */
        function showSuccess() {
            successNotification.open();
            setTimeout(function closePopup() {
                successNotification.close();
            }, NOTIFICATION_DELAY);
        }

        /**
         * Displays the 'Failed' notification with the specified error message.
         * @memberof JPEGExifEditor/helpers/notification
         * @param {string} errorMessage
         */
        function showError(errorMessage) {
            errorMessage.innerText = errorMessage;
            failedNotification.open();
        }

        /**
         * Initializes the module.
         * @memberof JPEGExifEditor/helpers/notification
         */
        function init() {
            successNotification = tau.widget.Popup(
                document.getElementById('saveSucceededPopup')
            );
            failedNotification = tau.widget.Popup(
                document.getElementById('saveFailedPopup')
            );
            errorMessage = document.getElementById('errorMessage');
        }

        return {
            init: init,
            showSuccess: showSuccess,
            showError: showError
        };
    }
});
