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

/**
 * Archive mode view.
 *
 * File selection mode.
 *
 * @requires {@link ArchiveManager/helpers/router}
 * @requires {@link core/event}
 * @requires {@link ArchiveManager/views/filelist}
 * @namespace ArchiveManager/views/modes/archive
 * @memberof ArchiveManager/views/modes
 */

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

        var

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

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

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

            /**
             * Select all checkbox.
             * @type {HTMLElement}
             */
            selectAll = null,

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

            /**
             * Compress button displayed in the footer in archive mode.
             * @type {HTMLElement}
             */
            compressBtn = null,

            /**
             * Back button displayed in the footer in archive mode.
             * @type {HTMLElement}
             */
            backCompressBtn = null,

            /**
             * Content list.
             * @type {HTMLElement}
             */
            filesList = null,

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

        /**
         * Shows compress footer.
         * Compress footer should be visible only in archive mode.
         */
        function showFooterCompress() {
            fileList.showFooter(footerCompress);
        }

        /**
         * Sets state of all checkboxes on content list
         * to value given as parameter.
         * @param {boolean} value
         */
        function setAllCheckboxes(value) {
            var list = filesList.querySelectorAll('input[type="checkbox"]'),
                len = list.length,
                i = 0,
                checkbox = null;

            for (i; i < len; i += 1) {
                checkbox = tau.widget.Checkbox(list[i]);
                checkbox.element.checked = value;
                checkbox.refresh();
            }
        }

        /**
         * Activates or deactivates compress button.
         * @param {boolean} enable
         */
        function activateCompressBtn(enable) {
            if (enable) {
                compressBtn.classList.remove(DISABLED_CLASS);
            } else {
                compressBtn.classList.add(DISABLED_CLASS);
            }
        }

        /**
         * Handles change event on select all checkbox.
         * @param {event} ev
         */
        function onSelectAllChange(ev) {
            var all = ev.target,
                value = all.checked;

            setAllCheckboxes(value);
            activateCompressBtn(value);
        }

        /**
         * Checks if select all checkbox should be selected.
         * Selects select all checkbox when all of the checkboxes
         * on content list are selected.
         * Deselects select all checkbox when at least one from all of the
         * checkboxes on content list is deselected.
         */
        function checkSelectAll() {
            var list = filesList.querySelectorAll('input[type="checkbox"]'),
                len = list.length,
                i = 0,
                checkbox = null,
                value = null,
                all = tau.widget.Checkbox(
                    selectAll.querySelector('input[type="checkbox"]')
                );

            for (i; i < len; i += 1) {
                checkbox = tau.widget.Checkbox(list[i]);
                if (value !== null && value !== checkbox.element.checked) {
                    break;
                }
                value = checkbox.element.checked;
            }

            if (i === len) {
                all.element.checked = value;
                activateCompressBtn(value);
            } else {
                all.element.checked = false;
                activateCompressBtn(true);
            }

            all.refresh();
        }

        /**
         * Deselects all checkboxes on content list.
         */
        function deselectAllCheckboxes() {
            var all = tau.widget.Checkbox(
                selectAll.querySelector('input[type="checkbox"]')
            );

            all.element.checked = false;
            all.refresh();
            setAllCheckboxes(false);
        }

        /**
         * Handles click event on compress button.
         */
        function onCompressBtnClick() {
            router.navigate('views/pages/name/show',
                fileList.getPath().toString());
        }

        /**
         * Handles click event.
         */
        function onBackCompressBtnClick() {
            router.navigate('views/modes/standard/show');
        }

        /**
         * Registers event listeners.
         */
        function bindEvents() {
            selectAll.addEventListener('change', onSelectAllChange);
            compressBtn.addEventListener('click', onCompressBtnClick);
            backCompressBtn.addEventListener('click', onBackCompressBtnClick);
        }

        /**
         * Activates archive mode.
         * @param {boolean} animate When it is set to true,
         * content list is animated during mode changing, otherwise not.
         * @memberof ArchiveManager/views/modes/archive
         */
        function activate(animate) {
            fileList.activateArchiveMode(animate);
            showFooterCompress();
            deselectAllCheckboxes();
            activateCompressBtn(false);
        }

        /**
         * Initializes module.
         * @memberof ArchiveManager/views/modes/archive
         */
        function init() {
            footerCompress = document.getElementById('footer-compress');
            filesList = document.getElementById('files-list');
            selectAll = document.getElementById('select-all');
            compressBtn = document.getElementById('compress-button');
            backCompressBtn = document.getElementById('back-compress-button');
            bindEvents();
        }

        e.listen({
            'views.filelist.selection.changed': checkSelectAll
        });

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

});
