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

/**
 * Line view helper module.
 *
 * @module helpers/line
 * @requires {@link helpers/constants}
 * @namespace helpers/line
 */

define({
    name: 'helpers/line',
    requires: [
        'helpers/constants'
    ],
    def: function helpersLine(CONST) {
        'use strict';

        /**
         * Returns container with depending on image count.
         *
         * @memberof helpers/line
         * @public
         * @param {number} imageCount Images to display.
         * @returns {number}
         */
        function getContainerWidth(imageCount) {
            var width = 2 * CONST.LINE_LEFT +
                        imageCount * CONST.LINE_ELEMENT_WIDTH;

            if (width < CONST.GALLERY_MIN_WIDTH) {
                width = CONST.GALLERY_MIN_WIDTH;
            }

            return width;
        }

        /**
         * Returns element width.
         *
         * @memberof helpers/line
         * @public
         * @returns {number}
         */
        function getElementWidth() {
            return CONST.LINE_ELEMENT_WIDTH;
        }

        /**
         * Returns element top and left position.
         *
         * @memberof helpers/line
         * @public
         * @param {number} num Element number.
         * @returns {{top: number, left: number}}
         */
        function getElementPosition(num) {
            return {
                top: CONST.LINE_TOP,
                left: CONST.LINE_LEFT + num * CONST.LINE_ELEMENT_WIDTH
            };
        }

        /**
         * Returns scroll left position of given element.
         *
         * @memberof helpers/line
         * @public
         * @param {number} num Element number.
         * @returns {number}
         */
        function getElementScroll(num) {
            return num * CONST.LINE_ELEMENT_WIDTH;
        }

        /**
         * Returns next scroll position.
         *
         * @memberof helpers/line
         * @public
         * @param {number} currentScroll Current scroll position.
         * @param {number} containerWidth Container width.
         * @param {boolean} toRight Should be scrolled to right.
         * @returns {number}
         */
        function getNextScroll(currentScroll, containerWidth, toRight) {
            var scrollTo =
                Math.round(currentScroll / CONST.LINE_ELEMENT_WIDTH) *
                CONST.LINE_ELEMENT_WIDTH;

            if (toRight) {
                scrollTo = scrollTo + CONST.LINE_ELEMENT_WIDTH;
                if (scrollTo > containerWidth - CONST.LINE_ELEMENT_WIDTH) {
                    scrollTo = scrollTo - CONST.LINE_ELEMENT_WIDTH;
                }
            } else {
                scrollTo = scrollTo - CONST.LINE_ELEMENT_WIDTH;
                if (scrollTo < 0) {
                    scrollTo = 0;
                }
            }
            return scrollTo;
        }

        /**
         * Click event handler.
         *
         * @memberof helpers/line
         * @public
         * @returns {number|null} Display mode change.
         */
        function clickHandler() {
            return CONST.MODE.FULL;
        }

        /**
         * Long press event handler.
         *
         * @memberof helpers/line
         * @public
         * @returns {number|null}
         */
        function longPressHandler() {
            return null;
        }

        /**
         * Double click event handler.
         *
         * @memberof helpers/line
         * @public
         * @returns {number|null}
         */
        function doubleClickHandler() {
            return null;
        }

        return {
            getContainerWidth: getContainerWidth,
            getElementWidth: getElementWidth,
            getElementPosition: getElementPosition,
            getElementScroll: getElementScroll,
            getNextScroll: getNextScroll,
            clickHandler: clickHandler,
            longPressHandler: longPressHandler,
            doubleClickHandler: doubleClickHandler
        };
    }
});
