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

/**
 * Double click helper module.
 *
 * @module helpers/doubleClick
 * @namespace helpers/doubleClick
 */

define({
    name: 'helpers/doubleClick',
    requires: [],
    def: function helpersDoubleClick() {
        'use strict';

        /**
         * Double click maximum time.
         *
         * @memberof helpers/doubleClick
         * @private
         * @const {number}
         */
        var DOUBLE_CLICK_TIME = 500,

            /**
             * Double click timeout id.
             *
             * @memberof helpers/doubleClick
             * @private
             * @type {number}
             */
            timeout = 0,

            /**
             * Click count.
             *
             * @memberof helpers/doubleClick
             * @private
             * @type {number}
             */
            clickCount = 0;

        /**
         * Click event handler.
         *
         * @memberof helpers/doubleClick
         * @private
         * @param {function} callback Double click callback.
         * @param {Event} event Click event.
         */
        function clickHandler(callback, event) {
            clickCount += 1;

            if (clickCount === 1) {
                timeout = window.setTimeout(function reset() {
                    clickCount = 0;
                    timeout = 0;
                }, DOUBLE_CLICK_TIME);
            } else if (clickCount === 2) {
                window.clearTimeout(timeout);
                clickCount = 0;
                timeout = 0;
                callback(event);
            }
        }

        /**
         * Adds handler to given element.
         *
         * @memberof helpers/doubleClick
         * @public
         * @param {HTMLElement} element Target element.
         * @param {function} doubleClickCallback Double click callback.
         */
        function addHandler(element, doubleClickCallback) {
            element.addEventListener(
                'click',
                clickHandler.bind(null, doubleClickCallback)
            );
        }

        /**
         * Removes handler from element.
         *
         * @memberof helpers/doubleClick
         * @public
         * @param {HTMLElement} element Target element.
         */
        function removeHandler(element) {
            element.removeEventListener('click', clickHandler);
        }

        return {
            addHandler: addHandler,
            removeHandler: removeHandler
        };
    }
});
