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

/**
 * Galleries page module.
 * @requires {@link core/event}
 * @requires {@link core/template}
 * @requires {@link MiniGallery/models/content}
 * @requires {@link MiniGallery/views/ui}
 * @requires {@link MiniGallery/views/photosPage}
 * @namespace MiniGallery/views/galleriesPage
 * @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/galleriesPage',
    requires: [
        'core/event',
        'core/template',
        'models/content',
        'views/ui'
    ],
    def: function galleriesPage(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,

            /**
             * Photos page module object.
             * @type {Module}
             */
            photosPage = null,

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

            /**
             * Reference to the gallery list element.
             * @type {HTMLElement}
             */
            galleriesElement = null,

            /**
             * Reference to the message element.
             * @type {HTMLElement}
             */
            emptyGalleryMsgElement = null;

        /**
         * Opens and shows gallery.
         * @param {string} galleryPath
         */
        function openGallery(galleryPath) {
            if (photosPage.drawPhotos(galleryPath)) {
                photosPage.show();
            }
        }

        /**
         * Handles of click event.
         * @param {Event} event
         */
        function onClickGalleries(event) {
            var target = event.target;

            if (target.classList.contains('list-image')) {
                openGallery(target.dataset.path);
            }
        }

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

        /**
         * Draws galleries.
         * @memberof MiniGallery/views/galleriesPage
         */
        function drawGalleries() {
            var galleries = content.getGalleries();

            if (galleries.length) {
                if (galleries.length === 1) {
                    page.classList.add('one-gallery');
                } else {
                    page.classList.remove('one-gallery');
                }

                galleriesElement.innerHTML =
                    template.get('galleries', {galleries: galleries});

                emptyGalleryMsgElement.classList.add('hidden');
                galleriesElement.classList.remove('hidden');
            } else {
                galleriesElement.classList.add('hidden');
                emptyGalleryMsgElement.classList.remove('hidden');
            }
        }

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

        /**
         * Initializes module.
         * @memberof MiniGallery/views/galleriesPage
         */
        function init() {
            photosPage = require('views/photosPage');
            page = document.getElementById('galleries-page');
            galleriesElement = document.getElementById('galleries-list');
            emptyGalleryMsgElement = document.getElementById('no-images-msg');
            bindEvents();
        }

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

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

    }
});
