/*
 * 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*/

/**
 * Preview module.
 * @requires {@link ArchiveManager/model/archive}
 * @requires {@link ArchiveManager/views/popup}
 * @requires {@link ArchiveManager/views/filelist}
 * @requires {@link core/event}
 * @requires {@link ArchiveManager/model/filesystem}
 * @namespace ArchiveManager/views/modes/preview
 * @memberof ArchiveManager/views/modes/modes
 */
define({
    name: 'views/modes/preview',
    requires: [
        'model/archive',
        'views/popup',
        'views/filelist',
        'core/event',
        'model/filesystem'
    ],
    def: function viewsModesPreview(req) {
        'use strict';

        var

            /**
             * Archive model.
             */
            archive = req.model.archive,

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

            /**
             * Filesystem module object.
             */
            modelFilesystem = req.model.filesystem,

            /**
             * Filelist view.
             */
            fileList = req.views.filelist,

            /**
             * Popup view module object.
             */
            popupView = req.views.popup,

            /**
             * Footer element displayed in preview mode.
             * @type {HTMLElement}
             */
            footerPreview = null,

            /**
             * Extract button displayed in the footer in preview mode.
             * @type {HTMLElement}
             */
            extractPreviewBtn = null,

            /**
             * Hidden class string.
             * @type {string}
             */
            HIDDEN_CLASS = 'hidden';

        /**
         * Shows footer which contains extract and back buttons.
         */
        function showFooterPreview() {
            footerPreview.classList.remove(HIDDEN_CLASS);
        }

        /**
         * Handles click event on extract button.
         */
        function onExtractPreviewBtnClick() {
            popupView.showExtractConfirm(
                fileList.getPath().getFullArchiveName()
            );
        }

        /**
         * Handles views.filelist.selected event.
         * @param {CustomEvent} ev
         */
        function onFilelistElementSelected(ev) {
            var data = ev.detail,
                fileInfo = data.file;

            if (fileInfo.type === fileList.ZIP_CLASS &&
                    data.mode === fileList.STANDARD_MODE) {
                archive.loadEntries(fileInfo.url);
            }

            if (fileInfo.type === fileList.FOLDER_CLASS &&
                    data.mode === fileList.PREVIEW_MODE) {
                fileList.getPath().goDown(fileInfo.label);
            }
        }

        /**
         * Handles model.filesystem.name.resolved event.
         * @param {CustomEvent} ev
         */
        function onNameResolved(ev) {
            modelFilesystem.createDir(
                ev.detail.name,
                fileList.getPath().getArchivePath()
            );
        }

        /**
         * Handles views.popup.ok.button.click event.
         */
        function onOkBtnClick() {
            if (fileList.getMode() === fileList.PREVIEW_MODE) {
                modelFilesystem.resolveNameForPath(
                    fileList.getPath().getArchiveName(),
                    fileList.getPath().getArchivePath()
                );
            }
        }

        /**
         * Handles model.filesystem.dir.created event.
         * @param {CustomEvent} ev
         */
        function onDirCreated(ev) {
            archive.extractArchive(
                fileList.getPath().getFullArchivePath(),
                ev.detail.dir
            );
        }

        /**
         * Registers event listeners.
         */
        function bindEvents() {
            extractPreviewBtn.addEventListener(
                'click',
                onExtractPreviewBtnClick
            );
        }

        /**
         * Activates preview mode.
         * @param {object} data
         * @memberof ArchiveManager/views/modes/preview
         */
        function activate(data) {
            fileList.activatePreviewMode(data);
            showFooterPreview();
        }

        /**
         * Initializes module.
         * @memberof ArchiveManager/views/modes/preview
         */
        function init() {
            footerPreview = document.getElementById('footer-preview');
            extractPreviewBtn = document.getElementById(
                'extract-preview-button'
            );
            bindEvents();
        }

        e.listen({
            'views.filelist.selected': onFilelistElementSelected,
            'views.popup.ok.button.click': onOkBtnClick,
            'model.filesystem.name.resolved': onNameResolved,
            'model.filesystem.dir.created': onDirCreated
        });

        return {
            init: init,
            activate: activate
        };
    }

});
