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

/**
 * Standard mode view.
 *
 * Standard mode is a standard navigable list without checkboxes
 * and includes the actual archivization process.
 *
 * @requires {@link core/event}
 * @requires {@link ArchiveManager/views/filelist}
 * @requires {@link ArchiveManager/helpers/router}
 * @requires {@link ArchiveManager/model/filesystem}
 * @namespace ArchiveManager/views/modes/standard
 * @memberof ArchiveManager/views/modes
 */

define({
    name: 'views/modes/standard',
    requires: [
        'core/event',
        'views/filelist',
        'helpers/router',
        'model/filesystem'
    ],
    def: function viewsModesStandard(req) {
        'use strict';

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

            /**
             * Router helper module object.
             */
            router = req.helpers.router,

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

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

            /**
             * Footer element displayed in standard mode,
             * when there is no archive type elements selected on content list.
             * @type {HTMLElement}
             */
            footerCreate = null,

            /**
             * Create button displayed in the create footer in standard mode.
             * @type {HTMLElement}
             */
            createBtn = null,

            /**
             * Disabled class string.
             * @type {string}
             */
            DISABLED_CLASS = 'ui-disabled';

        /**
         * Shows footer which contains create button.
         */
        function showFooterCreate() {
            fileList.showFooter(footerCreate);
        }

        /**
         * Enables footer which contains create button.
         */
        function enableFooterCreate() {
            footerCreate.classList.remove(DISABLED_CLASS);
        }

        /**
         * Disables footer which contains create button.
         */
        function disableFooterCreate() {
            footerCreate.classList.add(DISABLED_CLASS);
        }

        /**
         * Handles click event on create button.
         */
        function onCreateBtnClick() {
            router.navigate('views/modes/archive/show');
        }

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

        /**
         * Handles changed path event.
         * @param {CustomEvent} ev
         */
        function onPathChanged(ev) {
            if (ev.detail.archiveIndex ===
                    fileList.getPath().EMPTY_ARCHIVE_INDEX) {
                fileList.showFooter(footerCreate);
            }
        }

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

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

        /**
         * Shows or hides footer create
         * depending on number of files on content list
         * and current path.
         * @param {number} length Number of elements on content list.
         */
        function updateFooterCreate(length) {
            if (length > 0 && !fileList.getPath().isHome()) {
                enableFooterCreate();
            } else {
                disableFooterCreate();
            }
        }

        /**
         * Handles model.filesystem.storages.loaded event.
         * @param {CustomEvent} ev
         */
        function onStoragesLoaded(ev) {
            updateFooterCreate(ev.detail.storages.length);
        }

        /**
         * Handles model.filesystem.files.loaded event.
         * @param {CustomEvent} ev
         */
        function onFilesLoaded(ev) {
            updateFooterCreate(ev.detail.files.length);
        }

        /**
         * Activates standard mode.
         * @param {boolean} animate When it is set to true,
         * content list is animated during mode changing, otherwise not.
         * @memberof ArchiveManager/views/modes/standard
         */
        function activate(animate) {
            fileList.activateStandardMode(animate);
            showFooterCreate();
        }

        /**
         * Initializes module.
         * @memberof ArchiveManager/views/modes/standard
         */
        function init() {
            footerCreate = document.getElementById('footer-create');
            createBtn = document.getElementById('create-button');
            bindEvents();
        }

        e.listeners({
            'model.path.changed': onPathChanged,
            'views.filelist.selected': onFilelistElementSelected,
            'model.filesystem.storages.loaded': onStoragesLoaded,
            'model.filesystem.files.loaded': onFilesLoaded
        });

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

});
