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

/**
 * Date helpers module.
 *
 * @namespace helpers/date
 * @memberof helpers
 */
define({
    name: 'helpers/date',
    def: function helpersDate() {
        'use strict';

        /**
         * Names of months.
         *
         * @private
         * @const {string[]}
         */
        var MONTH_NAMES = [
                'January',
                'February',
                'March',
                'April',
                'May',
                'June',
                'July',
                'August',
                'September',
                'October',
                'November',
                'December'
            ];

        /**
         * Returns current date and time.
         *
         * @memberof helpers/date
         * @public
         * @returns {Date}
         */
        function getCurrentDateTime() {
            return new Date();
        }

        /**
         * Clones date.
         *
         * @memberof helpers/date
         * @public
         * @param {Date} date
         * @returns {Date}
         */
        function clone(date) {
            return new Date(date);
        }

        /**
         * Returns numbers of days in month.
         *
         * @memberof helpers/date
         * @public
         * @param {Date} date
         * @returns {number}
         */
        function daysInMonth(date) {
            var newDate = clone(date);

            newDate.setDate(1);
            newDate.setMonth(newDate.getMonth() + 1);

            return (
                new Date(
                    newDate.getFullYear(),
                    newDate.getMonth(),
                    0
                )
            ).getDate();
        }

        /**
         * Returns next month.
         *
         * @memberof helpers/date
         * @public
         * @param {Date} date
         * @returns {Date}
         */
        function getNextMonth(date) {
            var newDate = clone(date);

            newDate.setDate(1);
            newDate.setMonth(date.getMonth() + 1);

            return newDate;
        }

        /**
         * Returns previous month.
         *
         * @memberof helpers/date
         * @public
         * @param {Date} date
         * @returns {Date}
         */
        function getPreviousMonth(date) {
            var newDate = clone(date);

            newDate.setDate(1);
            newDate.setMonth(date.getMonth() - 1);

            return newDate;
        }

        /**
         * Returns name of month.
         *
         * @memberof helpers/date
         * @public
         * @param {Date} date
         * @returns {string}
         */
        function getMonthName(date) {
            return MONTH_NAMES[date.getMonth()];
        }

        /**
         * Returns position of first day of month.
         *
         * @memberof helpers/date
         * @public
         * @param {Date} date
         * @returns {number}
         */
        function getFirstDay(date) {
            var newDate = clone(date);

            newDate.setDate(1);
            return newDate.getDay();
        }

        /**
         * Prepares date to data format.
         *
         * @memberof helpers/date
         * @public
         * @param {Date} data
         * @returns {string}
         */
        function toDataString(data) {
            return data.getFullYear() + ',' +
                data.getMonth() + ',' + data.getDate();
        }

        return {
            clone: clone,
            daysInMonth: daysInMonth,
            getFirstDay: getFirstDay,
            getPreviousMonth: getPreviousMonth,
            getNextMonth: getNextMonth,
            getMonthName: getMonthName,
            getCurrentDateTime: getCurrentDateTime,
            toDataString: toDataString
        };
    }
});
