/*
 *      Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 *      Licensed under the Flora License, Version 1.1 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *              http://floralicense.org/license/
 *
 *      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, window, document, tau*/

/**
 * Init send view module.
 *
 * @module views/send
 * @namespace views/send
 */

define({
    name: 'views/send',
    requires: [],
    def: function viewsSend() {
        'use strict';

        /**
         * Class dictionary.
         *
         * @memberof views/send
         * @private
         * @const {Object}
         */
        var CLASSES = Object.freeze({
                SEQUENCE: 'sequence',
                HIDDEN: 'hidden'
            }),

            /**
             * Id dictionary.
             *
             * @memberof views/send
             * @private
             * @const {Object}
             */
            ID = Object.freeze({
                MAIN_PAGE: 'main',
                PAGE: 'send-page',
                SEND_CANCEL_FOOTER_BUTTON: 'send-cancel-footer-button',
                FOOTER: 'send-footer',
                PROGRESS: 'send-progress',
                PROGRESS_VALUE: 'send-progress-value',
                POPUP: 'send-popup',
                POPUP_ICON: 'send-popup-icon',
                POPUP_FAILED: 'failed-popup'
            }),

            /**
             * Check prefix.
             *
             * @memberof views/send
             * @private
             * @const {string}
             */
            CHECK_PREFIX = '/images/check_sequence/check_',

            /**
             * Page element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            page = null,

            /**
             * Footer element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            footer = null,

            /**
             * Footer cancel button element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            footerCancelButton = null,

            /**
             * Progress element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            progress = null,

            /**
             * Value element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            value = null,

            /**
             * Popup element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            popup = null,

            /**
             * Popup icon element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            popupIcon = null,

            /**
             * Failed popup element.
             *
             * @memberof views/send
             * @private
             * @type {HTMLElement}
             */
            popupFailed = null,

            /**
             * Current value.
             *
             * @memberof views/send
             * @private
             * @type {number}
             */
            currentValue = 0,

            /**
             * Progress interval id.
             *
             * @memberof views/send
             * @private
             * @type {number}
             */
            progressInterval = 0,

            /**
             * Sequence number.
             *
             * @memberof views/send
             * @private
             * @type {number}
             */
            sequenceNum = 0,

            /**
             * Popup interval id.
             *
             * @memberof views/send
             * @private
             * @type {number}
             */
            popupInterval = 0;

        /**
         * Updates progress text and widget value.
         *
         * @memberof views/send
         * @private
         * @param {number} val Progress value.
         * @param {number} total Progress total value.
         */
        function updateProgress(val, total) {
            value.innerText = val + '/' + total;
            tau.widget.CircleProgressBar(progress).value(val * 100 / total);
        }

        /**
         * Starts progress mock-up.
         *
         * @memberof views/send
         * @private
         */
        function startProgress() {
            progressInterval = window.setInterval(function onInterval() {
                if (currentValue < 1000) {
                    currentValue += 10;
                    updateProgress(currentValue, 1000);
                } else {
                    window.clearInterval(progressInterval);
                    progressInterval = 0;
                    currentValue = 0;
                    tau.widget.Popup(popup).open();
                }
            }, 50);
        }

        /**
         * Popup show event handler.
         *
         * @memberof views/send
         * @private
         */
        function popupShow() {
            footer.classList.add(CLASSES.HIDDEN);
            popupIcon.classList.remove(CLASSES.SEQUENCE + '18');
            popupIcon.classList.add(CLASSES.SEQUENCE + '1');
            popupInterval = window.setInterval(function onInterval() {
                popupIcon.classList.remove(CLASSES.SEQUENCE + sequenceNum);
                sequenceNum += 1;
                popupIcon.classList.add(CLASSES.SEQUENCE + sequenceNum);
                if (sequenceNum >= 18) {
                    window.clearInterval(popupInterval);
                    popupInterval = 0;
                    sequenceNum = 0;
                }
            }, 50);
        }

        /**
         * Popup hide event handler.
         *
         * @memberof views/send
         * @private
         */
        function popupHide() {
            footer.classList.remove(CLASSES.HIDDEN);
            tau.changePage('#' + ID.MAIN_PAGE);
        }

        /**
         * Page before show event handler.
         *
         * @memberof views/send
         * @private
         */
        function pageBeforeShow() {
            tau.widget.CircleProgressBar(progress, {size: 'full'});
            currentValue = 0;
            startProgress();
        }

        /**
         * Cancel button click event handler.
         *
         * @memberof views/send
         * @private
         */
        function onFooterCancelButtonClick() {
            window.clearInterval(progressInterval);
            footer.classList.add(CLASSES.HIDDEN);
            tau.widget.Popup(popupFailed).open();
        }

        /**
         * Popup hide event handler.
         *
         * @memberof views/send
         * @private
         */
        function popupFailedHide() {
            footer.classList.remove(CLASSES.HIDDEN);
            tau.changePage('#' + ID.MAIN_PAGE);
        }

        /**
         * Registers event handlers.
         *
         * @memberof views/send
         * @private
         */
        function bindEvents() {
            page.addEventListener('pagebeforeshow', pageBeforeShow);
            popup.addEventListener('popupshow', popupShow);
            popup.addEventListener('popuphide', popupHide);
            popupFailed.addEventListener('popuphide', popupFailedHide);

            footerCancelButton.addEventListener('click',
                onFooterCancelButtonClick);
        }

        /**
         * Preloads check sequence to prevent blinking.
         *
         * @memberof views/send
         * @private
         */
        function preloadImages() {
            document.createElement('img').src = CHECK_PREFIX + '01.png';
            document.createElement('img').src = CHECK_PREFIX + '02.png';
            document.createElement('img').src = CHECK_PREFIX + '03.png';
            document.createElement('img').src = CHECK_PREFIX + '04.png';
            document.createElement('img').src = CHECK_PREFIX + '05.png';
            document.createElement('img').src = CHECK_PREFIX + '06.png';
            document.createElement('img').src = CHECK_PREFIX + '07.png';
            document.createElement('img').src = CHECK_PREFIX + '08.png';
            document.createElement('img').src = CHECK_PREFIX + '09.png';
            document.createElement('img').src = CHECK_PREFIX + '10.png';
            document.createElement('img').src = CHECK_PREFIX + '11.png';
            document.createElement('img').src = CHECK_PREFIX + '12.png';
            document.createElement('img').src = CHECK_PREFIX + '13.png';
            document.createElement('img').src = CHECK_PREFIX + '14.png';
            document.createElement('img').src = CHECK_PREFIX + '15.png';
            document.createElement('img').src = CHECK_PREFIX + '16.png';
            document.createElement('img').src = CHECK_PREFIX + '17.png';
            document.createElement('img').src = CHECK_PREFIX + '18.png';
        }

        /**
         * Initializes the module.
         *
         * @memberof views/send
         * @public
         */
        function init() {
            page = document.getElementById(ID.PAGE);
            footer = document.getElementById(ID.FOOTER);
            footerCancelButton = document
                .getElementById(ID.SEND_CANCEL_FOOTER_BUTTON);
            progress = document.getElementById(ID.PROGRESS);
            value = document.getElementById(ID.PROGRESS_VALUE);
            popup = document.getElementById(ID.POPUP);
            popupIcon = document.getElementById(ID.POPUP_ICON);
            popupFailed = document.getElementById(ID.POPUP_FAILED);

            preloadImages();
            bindEvents();
        }

        return {
            init: init
        };
    }
});
