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

/**
 * Exif module.
 * @requires {@link core/tizen}
 * @namespace JPEGExifEditor/models/exif
 * @memberof JPEGExifEditor/models
 */
define({
    name: 'models/exif',
    requires: [
        'core/tizen'
    ],
    def: function exifModule(tizen) {
        'use strict';

        /**
         * Compares alphanumerically the 'path' string field of the given
         * objects.
         * This function is passed to the Array.sort function.
         * @param {object} file1
         * @param {object} file2
         * @returns {number}
         */
        function comparePaths(file1, file2) {
            return file1.path.localeCompare(file2.path);
        }

        /**
         * Finds all JPEG images in the media content and pass them to the
         * specified callback function.
         *
         * The object passed to the callback is an array. Each element of the
         * array is an object with following fields:
         * {string} title - title of the image.
         * {string} path - absolute path to the image.
         * {string} thumbnailPath - absolute path to the thumbnail of the image.
         *
         * @memberof JPEGExifEditor/models/exif
         * @param {function} onSuccess Success callback.
         * @param {function} onError Error callback.
         */
        function getImages(onSuccess, onError) {
            tizen.content.find(
                function onContentFound(contents) {
                    var result = [], content = null, i = 0;

                    for (i = 0; i < contents.length; i += 1) {
                        content = contents[i];
                        result.push({
                            title: content.title,
                            path: content.contentURI,
                            thumbnailPath: content.thumbnailURIs.length ?
                                content.thumbnailURIs[0] : null
                        });
                    }
                    onSuccess(result.sort(comparePaths));
                },
                onError,
                null,
                new tizen.AttributeFilter('mimeType', 'EXACTLY', 'image/jpeg'));
        }

        /**
         * Returns an ExifInformation object instance with initialized URI.
         * @memberof JPEGExifEditor/models/exif
         * @param {string} imageUri
         * @returns {ExifInformation}
         */
        function createExifInfo(imageUri) {
            var exifInfo = new tizen.ExifInformation();
            exifInfo.uri = imageUri;
            return exifInfo;
        }

        /**
         * Obtains the exif information from an image file located in the
         * specified URI and pass it to the specified success callback.
         * @memberof JPEGExifEditor/models/exif
         * @param {string} imageUri URI of the image file.
         * @param {function} onSuccess Success callback.
         * @param {function} onError Error callback.
         */
        function readExifInfo(imageUri, onSuccess, onError) {
            try {
                tizen.exif.getExifInfo(imageUri, onSuccess, onError);
            } catch (e) {
                onError(e);
            }
        }

        /**
         * Saves the exif information.
         * @memberof JPEGExifEditor/models/exif
         * @param {ExifInformation} exifInfo Exif information object.
         * @param {function} onSuccess Success callback.
         * @param {function} onError Error callback.
         */
        function saveExifInfo(exifInfo, onSuccess, onError) {
            try {
                tizen.exif.saveExifInfo(exifInfo, onSuccess, onError);
            } catch (e) {
                onError(e);
            }
        }

        return {
            getImages: getImages,
            readExifInfo: readExifInfo,
            saveExifInfo: saveExifInfo,
            createExifInfo: createExifInfo
        };
    }
});
