/*
 * Copyright (c) 2014 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, history*/

/**
 * Init page module.
 * @requires {@link core/application}
 * @requires {@link core/event}
 * @requires {@link ArchiveManager/views/pages/main}
 * @namespace ArchiveManager/views/init
 * @memberof ArchiveManager/views
 */
define({
    name: 'views/init',
    requires: [
        'core/application',
        'core/event',
        'views/pages/main'
    ],
    def: function viewsInitPage(req) {
        'use strict';

        /**
         * Application module object.
         */
        var app = req.core.application,

            /**
             * Event module object.
             */
            e = req.core.event,

            /**
             * Main view module object.
             */
            mainView = req.views.pages.main;

        /**
         * Handles views.popup.ok.button.click event.
         */
        function onExitBtnClick() {
            app.exit();
        }

        /**
         * Registers view event listeners.
         */
        function bindEvents() {
            document.addEventListener('tizenhwkey', function onTizenHwKey(e) {
                if (e.keyName === 'back') {
                    if (tau.activePage.id === 'name') {
                        history.back();
                    } else {
                        mainView.exit();
                    }
                }
            });
        }

        /**
         * Initializes module.
         * @memberof ArchiveManager/views/init
         */
        function init() {
            bindEvents();
        }

        e.listen({
            'views.popup.exit.button.click': onExitBtnClick
        });

        return {
            init: init
        };
    }

});
