/*
 * Copyright (c) 2012 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.
 */

/*jslint devel: true*/
/*global tizen, $, app, localStorage */

/**
* @class Model
*/
function Model() {
    'use strict';

    this.init();
}

(function strict() { // strict mode wrapper
    'use strict';

    Model.prototype = {

       /**
        * API module initialisation
        */
        init: function Model_init() {
            return;
        },

       /**
        * Registers listener for call history change
        * @param {function} onSuccessCallback
        */
        registerChangeListener: function Model_registerChangeListener(
            onSuccessCallback
        ) {
            var callHistoryListener = {
                onadded: onSuccessCallback,
                onchanged: onSuccessCallback,
                onremoved: onSuccessCallback
            };
            tizen.callhistory.addChangeListener(callHistoryListener);
        },

       /**
        * @param {function} onSuccess Success callback
        * @param {function} onError Error callback
        */
        getCallHistory: function Model_getCallHistory(onSuccess, onError) {
            if (typeof onError !== 'function') {
                onError = function onCallHistoryFindError(e) {
                    console.error('Model_getCallHistory error', e);
                };
            }

            tizen.callhistory.find(
                onSuccess,
                onError,
                null,
                new tizen.SortMode('startTime', 'DESC')
            );
        },

       /**
        * @param {number} phoneNumber
        * @param {function} onSuccess Callback
        */
        getCallHistoryForCaller: function Model_getCallHistoryForCaller(
            phoneNumber,
            onSuccess
        ) {
            var filter = null, success;
            if (phoneNumber) {
                filter = new tizen.AttributeFilter(
                    'remoteParties.remoteParty',
                    'EXACTLY',
                    phoneNumber
                );
                success = onSuccess;
            } else {
                success = function onCallHistoryFindSuccess(calls) {
                    onSuccess(calls.filter(function onIterate(element) {
                        if (!element.remoteParties[0].remoteParty) {
                            return element;
                        }
                    }));
                };
            }

            tizen.callhistory.find(success,
                function onCallHistoryFindError(e) {
                    console.error(e);
                },
                filter,
                new tizen.SortMode('startTime', 'DESC')
                );
        },

       /**
        * Deletes specified log entry
        * @param {CallHistoryEntry} entry
        */
        deleteLog: function Model_deleteLog(entry) {
            try {
                tizen.callhistory.remove(entry);
            } catch (e) {
                console.error('Error on entry delete: ' + e.message);
            }
        },

       /**
        * @param {number} personId
        * @returns {string} photoURI
        */
        getPhotoURIForContact: function Model_getPhotoURIForContact(personId) {
            try {
                var addressBook = tizen.contact.getUnifiedAddressBook(),
                    contact;
                if (addressBook) {
                    contact = addressBook.get(personId);
                    return contact.photoURI || false;
                }
            } catch (e) {
                console.error('updatePhotoByContactId error:' + e.message);
            }
            return false;
        }
    };
}());
