/*
 * Copyright (c) 2015 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, console, tau */

/**
 * Select image view module.
 * @requires {@link core/template}
 * @requires {@link core/event}
 * @requires {@link JPEGExifEditor/models/exif}
 * @namespace JPEGExifEditor/views/selectImage
 * @memberof JPEGExifEditor/views
 */

define({
    name: 'views/selectImage',
    requires: [
        'core/template',
        'core/event',
        'models/exif'
    ],
    def: function chooseImageModule(template, event, exifModule) {
        'use strict';

        /**
         * Reference to JPEG files list element.
         * @type {HTMLElement}
         */
        var imagesList = null,

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

        /**
         * Click event listener called when an item of the image list is tapped.
         * @param {Event} ev
         * @fires selected
         */
        function onListItemClick(ev) {
            event.fire('selected', ev.currentTarget.dataset.uri);
        }

        /**
         * Initializes the click event listener on items of the image list.
         */
        function initImageListItems() {
            var links = imagesList.querySelectorAll('a'),
                i = 0;

            for (i = 0; i < links.length; i += 1) {
                links[i].addEventListener('click', onListItemClick);
            }
        }

        /**
         * Success handler of loading images from the media content.
         * @param {object[]} images
         */
        function onImagesGetSuccess(images) {
            imagesList.innerHTML = images.length ?
                template.get('imageFile', {images: images}) : '';
            tau.engine.createWidgets(imagesList);
            tau.widget.Listview(imagesList).refresh();
            if (images.length) {
                noImagesMsg.classList.add('hidden');
            } else {
                noImagesMsg.classList.remove('hidden');
            }
            initImageListItems();
        }

        /**
         * Error handler of loading images from the media content.
         * @param {Error} error
         */
        function onImagesGetFailed(error) {
            console.error('Loading content images failed: ' + error.message);
        }

        /**
         * Loads all JPEG files found in the media content and fills the images
         * list with these files.
         */
        function loadImagesList() {
            exifModule.getImages(onImagesGetSuccess, onImagesGetFailed);
        }

        /**
         * Initializes the module.
         * @memberof JPEGExifEditor/views/selectImage
         */
        function init() {
            imagesList = document.getElementById('imagesList');
            noImagesMsg = document.getElementById('noImagesMsg');

            loadImagesList();
        }

        return {
            init: init
        };
    }
});
