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

/**
 * Popup module.
 * @requires {@link core/event}
 * @namespace ArchiveManager/views/popup
 * @memberof ArchiveManager/views
 */
define({
    name: 'views/popup',
    requires: [
        'core/event'
    ],
    def: function viewsPopup(e) {
        'use strict';

        /**
         * Popup element.
         * @type {HTMLElement}
         */
        var popup = null,

            /**
             * Popup footer element.
             * @type {HTMLElement}
             */
            popupFooter = null,

            /**
             * Element which displays percentage value
             * of the total progressbar.
             * @type {HTMLElement}
             */
            totalValue = null,

            /**
             * Element which shows graphically percentage progress
             * of the total progressbar.
             * @type {HTMLElement}
             */
            totalProgress = null,

            /**
             * Element which displays percentage value
             * of the partial progressbar.
             * @type {HTMLElement}
             */
            partialValue = null,

            /**
             * Element which shows graphically percentage progress
             * of partial progressbar.
             * @type {HTMLElement}
             */
            partialProgress = null,

            /**
             * Element which displays popup title.
             * @type {HTMLElement}
             */
            popupTitle = null,

            /**
             * Parent container of the total progressbar element.
             * @type {HTMLElement}
             */
            totalFrame = null,

            /**
             * Parent container of the partial progressbar element.
             * @type {HTMLElement}
             */
            partialFrame = null,

            /**
             * Element which displays popup message.
             * @type {HTMLElement}
             */
            message = null,

            /**
             * Abort button.
             * @type {HTMLElement}
             */
            abortBtn = null,

            /**
             * Cancel button.
             * @type {HTMLElement}
             */
            cancelBtn = null,

            /**
             * OK button.
             * @type {HTMLElement}
             */
            okBtn = null,

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

            /**
             * Two button class string.
             * @type {string}
             */
            TWO_BUTTON_CLASS = 'ui-grid-col-2',

            /**
             * Popup warning title.
             * @type {string}
             */
            POPUP_WARNING_TITLE = 'Warning',

            /**
             * Exit message.
             * @type {string}
             */
            EXIT_MESSAGE = 'Are you sure you want to close this application?',

            /**
             * Zip archive extract text message.
             * @type {string}
             */
            ZIP_ARCHIVE_EXTRACT_MESSAGE =
                'Are you sure you want to extract this archive file?',

            /**
             * Indicates if the popup is exit popup.
             * @type {boolean}
             */
            isExitPopup = false,

            /**
             * If 'false' displayed popup can be hidden only by call 'hide'
             * method, otherwise popup can be hidden also by tapping on the
             * screen.
             * @type {boolean}
             */
            dismissible = true;

        /**
         * Deactivates progressbar view
         * (hides total and partial progressbars and abort button).
         */
        function deactivateProgress() {
            tau.widget.Progress(totalProgress).value(0);
            tau.widget.Progress(partialProgress).value(0);
            totalFrame.classList.add(HIDDEN_CLASS);
            partialFrame.classList.add(HIDDEN_CLASS);
            abortBtn.classList.add(HIDDEN_CLASS);
        }

        /**
         * Deactivates message view
         * (hides message text and OK button).
         */
        function deactivateMessage() {
            message.classList.add(HIDDEN_CLASS);
            okBtn.classList.add(HIDDEN_CLASS);
        }

        /**
         * Makes two buttons (OK and Cancel) visible.
         */
        function activateOKCancelButtons() {
            popupFooter.classList.add(TWO_BUTTON_CLASS);
            okBtn.classList.remove(HIDDEN_CLASS);
            cancelBtn.classList.remove(HIDDEN_CLASS);
        }

        /**
         * Makes two buttons (OK and Cancel) invisible.
         */
        function deactivateOKCancelButtons() {
            popup.classList.remove(TWO_BUTTON_CLASS);
            okBtn.classList.add(HIDDEN_CLASS);
            cancelBtn.classList.add(HIDDEN_CLASS);
        }

        /**
         * Sets popup title.
         * @param {string} title
         */
        function setTitle(title) {
            popupTitle.innerHTML = title || '';
        }

        /**
         * Shows popup.
         */
        function show() {
            tau.engine.createWidgets(popup);
            tau.widget.Popup(popup).open({
                transition: 'none',
                positionTo: 'window'
            });
        }

        /**
         * Activates progressbar view while create archive process
         * (shows total and partial progressbars and abort button).
         * @memberof ArchiveManager/views/popup
         */
        function activateCreateProgress() {
            deactivateMessage();
            abortBtn.classList.remove(HIDDEN_CLASS);
            totalFrame.classList.remove(HIDDEN_CLASS);
            partialFrame.classList.remove(HIDDEN_CLASS);
        }

        /**
         * Activates progressbar view while extract archive process.
         * (shows only total progressbars and abort button).
         */
        function activateExtractProgress() {
            deactivateMessage();
            abortBtn.classList.remove(HIDDEN_CLASS);
            totalFrame.classList.remove(HIDDEN_CLASS);
        }

        /**
         * Activates message view
         * (shows message text and OK button).
         * @memberof ArchiveManager/views/popup
         * @param {string} text
         * @param {string} title
         */
        function activateMessage(text, title) {
            deactivateProgress();
            setTitle(title);
            okBtn.classList.remove(HIDDEN_CLASS);
            message.innerHTML = text;
            message.classList.remove(HIDDEN_CLASS);
        }

        /**
         * Hides popup.
         * @memberof ArchiveManager/views/popup
         */
        function hide() {
            dismissible = true;
            tau.widget.Popup(popup).close();
        }

        /**
         * Updates progressbar value.
         * @memberof ArchiveManager/views/popup
         * @param {Object} values
         */
        function setProgress(values) {
            var total = values.getTotal(),
                partial = values.getPartial(),
                filename = values.filename,
                hasPartialInfo = values.hasPartialInfo;

            popupTitle.innerHTML = filename;

            totalValue.innerHTML = total.toFixed(0);
            tau.widget.Progress(totalProgress).value(total);

            if (hasPartialInfo) {
                partialValue.innerHTML = partial.toFixed(0);
                tau.widget.Progress(partialProgress).value(partial);
            }
        }

        /**
         * Handles popupafterclose on popup element.
         */
        function onPopupAfterClose() {
            if (dismissible) {
                setTitle();
                deactivateProgress();
                deactivateMessage();
                deactivateOKCancelButtons();
            } else {
                show();
            }
        }

        /**
         * Handles click event on OK button.
         */
        function onOkBtnClick() {
            if (isExitPopup) {
                isExitPopup = false;
                e.fire('exit.button.click');
            } else {
                e.fire('ok.button.click');
            }
        }

        /**
         * Shows popup as exit confirm.
         * @memberof ArchiveManager/views/popup
         */
        function showExitConfirm() {
            isExitPopup = true;
            activateMessage(EXIT_MESSAGE, POPUP_WARNING_TITLE);
            activateOKCancelButtons();
            show();
        }

        /**
         * Shows popup as extract confirm.
         * @memberof ArchiveManager/views/popup
         * @param {string} name
         */
        function showExtractConfirm(name) {
            activateMessage(ZIP_ARCHIVE_EXTRACT_MESSAGE, name);
            activateOKCancelButtons();
            show();
        }

        /**
         * Shows popup with extract progressbar.
         * @memberof ArchiveManager/views/popup
         */
        function showExtractProgress() {
            activateExtractProgress();
            show();
        }

        /**
         * Shows message popup with no buttons that can be hidden only by
         * calling 'hide' method.
         * @memberof ArchiveManager/views/popup
         * @param {string} text
         * @param {string} title
         */
        function showUnclosableMessage(text, title) {
            dismissible = false;
            activateMessage(text, title);
            okBtn.classList.add(HIDDEN_CLASS);
            show();
        }

        /**
         * Handles click event on Abort button.
         */
        function onAbortBtnClick() {
            e.fire('abort.button.click');
        }

        /**
         * Registers event listeners.
         */
        function bindEvents() {
            popup.addEventListener('popupafterclose', onPopupAfterClose);
            okBtn.addEventListener('click', onOkBtnClick);
            abortBtn.addEventListener('click', onAbortBtnClick);
        }

        /**
         * Initializes module.
         * @memberof ArchiveManager/views/popup
         */
        function init() {
            popup = document.getElementById('main-popup');
            popupFooter = popup.querySelector('.ui-popup-footer');
            totalValue = document.getElementById('total-value');
            totalProgress = document.getElementById('total-progress');
            partialValue = document.getElementById('partial-value');
            partialProgress = document.getElementById('partial-progress');
            popupTitle = document.getElementById('main-popup-title');
            totalFrame = document.getElementById('total-frame');
            partialFrame = document.getElementById('partial-frame');
            message = document.getElementById('main-popup-message');
            abortBtn = document.getElementById('main-popup-abort-button');
            cancelBtn = document.getElementById('main-popup-cancel-button');
            okBtn = document.getElementById('main-popup-ok-button');
            bindEvents();
        }

        return {
            init: init,
            hide: hide,
            showExitConfirm: showExitConfirm,
            showExtractConfirm: showExtractConfirm,
            showExtractProgress: showExtractProgress,
            showUnclosableMessage: showUnclosableMessage,
            activateMessage: activateMessage,
            activateCreateProgress: activateCreateProgress,
            setProgress: setProgress
        };
    }

});
