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

/**
 * Grid view helper module.
 *
 * @module helpers/grid
 * @requires {@link helpers/constants}
 * @namespace helpers/grid
 */

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

        /**
         * Returns container with depending on image count.
         *
         * @memberof helpers/grid
         * @public
         * @param {number} imageCount Images to display.
         * @returns {number}
         */
        function getContainerWidth(imageCount) {
            var width = 2 * CONST.GRID_FIRST_LEFT +
                        Math.round(imageCount / 3) * CONST.GRID_ELEMENT_WIDTH;

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

            return width;
        }

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

        /**
         * Returns element top and left position.
         *
         * @memberof helpers/grid
         * @public
         * @param {number} num Element number.
         * @returns {{top: number, left: number}}
         */
        function getElementPosition(num) {
            var top = 0,
                left = 0,
                offset = Math.floor(num / 3) * CONST.GRID_ELEMENT_WIDTH;

            switch (num % 3) {
                case 0:
                    top = CONST.GRID_FIRST_TOP;
                    left = CONST.GRID_FIRST_LEFT + offset;
                    break;
                case 1:
                    top = CONST.GRID_SECOND_TOP;
                    left = CONST.GRID_SECOND_LEFT + offset;
                    break;
                case 2:
                    top = CONST.GRID_THIRD_TOP;
                    left = CONST.GRID_THIRD_LEFT + offset;
                    break;
            }

            return {top: top, left: left};
        }

        /**
         * Returns next scroll position.
         *
         * @memberof helpers/grid
         * @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.GRID_ELEMENT_WIDTH) *
                CONST.GRID_ELEMENT_WIDTH;

            if (toRight) {
                scrollTo = scrollTo + CONST.GRID_ELEMENT_WIDTH;
                if (scrollTo > containerWidth - CONST.GRID_ELEMENT_WIDTH * 3) {
                    scrollTo = currentScroll;
                }
            } else {
                scrollTo = scrollTo - CONST.GRID_ELEMENT_WIDTH;
                if (scrollTo < 0) {
                    scrollTo = 0;
                }
            }
            return scrollTo;
        }

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

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

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

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