/*
 *      Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 *      Licensed under the Flora License, Version 1.1 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *              http://floralicense.org/license/
 *
 *      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 */

/**
 * App module.
 *
 * @module app
 * @requires {@link core/event}
 * @requires {@link core/application}
 * @requires {@link core/systeminfo}
 * @requires {@link helpers/constants}
 * @requires {@link views/empty}
 * @requires {@link views/main}
 * @requires {@link views/send}
 * @namespace app
 */

define({
    name: 'app',
    requires: [
        'core/event',
        'core/application',
        'core/systeminfo',
        'helpers/constants',
        'views/empty',
        'views/main',
        'views/send'
    ],
    def: function appInit(req) {
        'use strict';

        /**
         * Core event module.
         *
         * @memberof app
         * @private
         * @type {Module}
         */
        var e = req.core.event,

            /**
             * Constants helper module.
             *
             * @memberof app
             * @private
             * @type {Module}
             */
            constants = req.helpers.constants,

            /**
             * Main view module.
             *
             * @memberof app
             * @private
             * @type {Module}
             */
            main = req.views.main,

            /**
             * Core application module.
             *
             * @memberof app
             * @private
             * @type {Module}
             */
            app = req.core.application,

            /**
             * Core SystemInfo module.
             *
             * @memberof app
             * @private
             * @type {Module}
             */
            systeminfo = req.core.systeminfo;

        /**
         * Handler onLowBattery state.
         *
         * @memberof app
         * @private
         */
        function onLowBattery() {
            app.exit();
        }

        /**
         * Handler on press 'back' key.
         *
         * @memberof app
         * @private
         * @param {Event} event Press key event.
         */
        function onTizenHWKey(event) {
            var activePopup = tau.activePage.querySelector('.ui-popup-active');

            if (event.keyName === 'back') {
                if (activePopup) {
                    tau.widget.Popup(activePopup).close();
                } else if (tau.activePage.id === 'main') {
                    if (main.getCurrentMode() === constants.MODE.GRID) {
                        app.exit();
                    } else {
                        main.onBack();
                    }
                } else if (tau.activePage.id === 'empty-page') {
                    app.exit();
                } else {
                    tau.back();
                }
            }
        }

        /**
         * Initializes the app.
         *
         * @memberof app
         * @public
         */
        function init() {
            document.addEventListener('tizenhwkey', onTizenHWKey);

            systeminfo.checkBatteryLowState();
            systeminfo.listenBatteryLowState();
        }

        e.listeners({
            'core.systeminfo.battery.low': onLowBattery
        });

        return {
            init: init
        };
    }
});
