/*
 * Copyright (c) 2015 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*/

/**
 * Double-type inputs validation module.
 * @namespace JPEGExifEditor/helpers/validateDouble
 * @memberof JPEGExifEditor/helpers
 */
define({
    name: 'helpers/validateDouble',

    def: function validateDouble() {
        'use strict';

        /**
         * Validable inputs.
         * @type {NodeList}
         */
        var inputs = null,

            /**
             * Css class of invalid inputs.
             * @type {string}
             */
            INVALID_CLASS = 'invalid';

        /**
         * Return true if the specified string is empty or contains a correctly
         * formatted double number.
         * @param {string} value
         * @returns {boolean}
         */
        function isDoubleOrEmpty(value) {
            return value === '' || !!value.match(/^\d+(?:\.\d+)?$/);
        }

        /**
         * Validates the specified input.
         * If the input contains an empty string or a correctly formatted double
         * value then the 'invalid' class is removed from the input and true is
         * returned.
         * Otherwise the 'invalid' class is added to the input and false is
         * returned.
         * @param {HTMLElement} input
         * @returns {boolean}
         */
        function validateInput(input) {
            var isValid = isDoubleOrEmpty(input.value);

            if (isValid) {
                input.classList.remove(INVALID_CLASS);
            } else {
                input.classList.add(INVALID_CLASS);
            }

            return isValid;
        }

        /**
         * Returns an array of IDs of the specified inputs array.
         * @param {HTMLElement[]} inputs
         * @returns {string[]}
         */
        function getFieldIds(inputs) {
            var fieldNames = [],
                length = inputs.length,
                i = 0,
                input = null;

            for (i = 0; i < length; i += 1) {
                input = inputs[i];
                fieldNames.push(input.id);
            }

            return fieldNames;
        }

        /**
         * Validates all inputs and returns an array of IDs of invalid inputs.
         * @memberof JPEGExifEditor/helpers/validateDouble
         * @returns {string[]}
         */
        function validate() {
            var length = inputs.length,
                i = 0,
                input = null,
                invalidInputs = [];

            for (i = 0; i < length; i += 1) {
                input = inputs[i];
                if (!validateInput(input)) {
                    invalidInputs.push(input);
                }
            }

            return getFieldIds(invalidInputs);
        }

        /**
         * Handles a change event on an input.
         * Removes the 'invalid' class from the changed input.
         * @param {Event} event
         */
        function onInputChanged(event) {
            event.target.classList.remove(INVALID_CLASS);
        }

        /**
         * Removes the 'invalid' class from all validable inputs.
         * @memberof JPEGExifEditor/helpers/validateDouble
         */
        function clear() {
            var i = 0, length = inputs.length;

            for (i = 0; i < length; i += 1) {
                inputs[i].classList.remove(INVALID_CLASS);
            }
        }

        /**
         * Initializes the module.
         * Finds inputs with the data-validate="double" attribute.
         * These inputs will be validated.
         * @memberof JPEGExifEditor/helpers/validateDouble
         */
        function init() {
            inputs =
                document.querySelectorAll('input[data-validate="double"]');

            var length = inputs.length, i = 0;

            for (i = 0; i < length; i += 1) {
                inputs[i].addEventListener('change', onInputChanged);
            }
        }

        return {
            init: init,
            validate: validate,
            clear: clear
        };
    }
});
