/*
 * 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, document, tau, console, setTimeout*/

/**
 * PhotosPage module.
 * @requires {@link core/event}
 * @requires {@link core/template}
 * @requires {@link MiniGallery/models/content}
 * @requires {@link MiniGallery/views/photoPage}
 * @namespace MiniGallery/views/photosPage
 * @memberof MiniGallery/views
 * @author Dominik Koleda <d.koleda@samsung.com>
 * @author Piotr Dabrowski <p.dabrowski2@samsung.com>
 * @author Kamil Stepczuk <k.stepczuk@samsung.com>
 */
define({
    name: 'views/photosPage',
    requires: [
        'core/event',
        'core/template',
        'models/content',
        'views/photoPage'
    ],
    def: function photosPage(req) {
        'use strict';

        /**
         * Event module object.
         * @type {Module}
         */
        var e = req.core.event,

            /**
             * Template module object.
             * @type {Module}
             */
            template = req.core.template,

            /**
             * Content module object.
             * @type {Module}
             */
            content = req.models.content,

            /**
             * Photo page module object.
             * @type {Module}
             */
            photoPage = req.views.photoPage,

            /**
             * Currently displayed gallery path.
             * @type {string}
             */
            currentGalleryPath = '',

            /**
             * Currently displayed gallery object.
             * @type {object}
             */
            currentGallery = null,

            /**
             * Photos page element.
             * @type {HTMLElement}
             */
            page = null,

            /**
             * Header element.
             * @type {HTMLElement}
             */
            pageHeader = null,

            /**
             * Content element.
             * @type {HTMLElement}
             */
            pageContent = null;

        /**
         * Shows photo page.
         * @param {string} galleryPath
         * @param {number} photoIndex
         */
        function showPhotoPage(galleryPath, photoIndex) {
            if (photoPage.drawPhoto(galleryPath, photoIndex)) {
                photoPage.show();
            }
        }

        /**
         * Handles click event on page element.
         * @param {Event} event
         */
        function onPhotosPageClick(event) {
            var target = event.target;

            if (target.classList.contains('list-image')) {
                showPhotoPage(currentGalleryPath, Number(target.dataset.id));
            }
        }

        /**
         * Registers module event listeners.
         */
        function bindEvents() {
            page.addEventListener('click', onPhotosPageClick);
        }

        /**
         * Draws photos.
         * Returns true on success, false otherwise.
         * @memberof MiniGallery/views/photosPage
         * @param {string} galleryPath
         * @return {boolean}
         */
        function drawPhotos(galleryPath) {
            var galleries;

            currentGalleryPath = galleryPath;
            galleries = content.getGalleries();
            currentGallery = galleries[galleryPath];

            if (currentGallery === undefined) {
                console.warn('Gallery "' + galleryPath + '" does not exist!');
                return false;
            }
            pageHeader.innerHTML = template.get(
                'photosPageTitle',
                currentGallery
            );
            pageContent.innerHTML = template.get('photos', currentGallery);
            return true;
        }

        /**
         * Shows photos page.
         * @memberof MiniGallery/views/photosPage
         */
        function show() {
            tau.changePage(page);
        }

        /**
         * Handles content.refreshed event.
         */
        function onContentRefreshed() {
            if (currentGalleryPath !== '') {
                drawPhotos(currentGalleryPath);
            }
        }

        /**
         * Initializes module.
         * @memberof MiniGallery/views/photosPage
         */
        function init() {
            page = document.getElementById('photos-page');
            pageHeader = page.querySelector('.ui-header');
            pageContent = page.querySelector('.ui-content');
            bindEvents();
        }

        e.listeners({
            'models.content.refreshed': onContentRefreshed
        });

        return {
            init: init,
            show: show,
            drawPhotos: drawPhotos
        };

    }
});
