/* Copyright 2009 Palm, Inc.  All rights reserved. */

var ContactsMojoService = Class.create({
    initialize: function() {
        //Set up the html5 db
        //      var options = {
        //        name: "contacts-prefs",
        //        version: 1,
        //        replace: false
        //      };
        //      this.depot = new Mojo.Depot(options, null, null);
    },
    
    list: function(sceneController, filter, callback, subscriberId, offset, limit) {
        var params = {
            offset: offset,
            limit: limit,
            filter: filter
        };
        if (subscriberId) {
            params.subscriberId = subscriberId;
        } else {
            params.subscribe = true;
        }
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'list',
            parameters: params,
            onSuccess: callback
        });
    },
    
    newList: function(sceneController, filter, callback, subscriberId, resetFlag, offset, limit) {
        var params = {
            offset: offset,
            limit: limit,
            filter: filter
        };
        if (subscriberId) {
        
            if (subscriberId != -1) {
                params.subscriberId = subscriberId;
                
                if (resetFlag) {
                    params.reset = true;
                }
            }
            
            return sceneController.serviceRequest(ContactsMojoService.identifier, {
                method: 'newList',
                parameters: params,
                onSuccess: callback
            });
            
        } else {
            params.subscribe = true;
            
            return sceneController.serviceRequest(ContactsMojoService.identifier, {
                method: 'newList',
                parameters: params,
                onSuccess: callback
            });
        }
    },
    
    count: function(sceneController, filter, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'newCount',
            parameters: {
                filter: filter
            },
            onSuccess: callback
        });
    },
    
    details: function(personId, callback, sceneController, extraParams, failCallback) {
        var params = {
            id: personId
        };
        if (extraParams) {
            Object.extend(params, extraParams);
		}
        var serviceParams = {
            method: 'details',
            parameters: params,
            onSuccess: callback
        };
        if (failCallback !== undefined) {
            serviceParams.onFailure = failCallback;
		}
        return sceneController.serviceRequest(ContactsMojoService.identifier, serviceParams);
    },
    
    basicDetails: function(personId, callback, sceneController) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'basicDetails',
            parameters: {
                id: personId,
                subscribe: true
            },
            onSuccess: callback
        });
    },
    
    profileDetails: function(contactId, callback, sceneController) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'profileDetails',
            parameters: {
                id: contactId
            },
            onSuccess: callback
        });
    },
    
    save: function(contact, callback, failure) {
        return new Mojo.Service.Request(ContactsMojoService.identifier, {
            method: 'save',
            parameters: contact,
            onSuccess: callback,
            onFailure: failure
        });
    },
    
    saveContactPicture: function(contactId, filePath, cropInfo, callback, failure) {
        return new Mojo.Service.Request(ContactsMojoService.identifier, {
            method: 'saveContactPicture',
            parameters: {
                contactId: contactId,
                cropInfo: cropInfo,
                path: filePath
            },
            onFailure: failure,
            onSuccess: callback
        });
    },
    
    doDelete: function(contactId, callback, sceneController, failure) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'delete',
            parameters: {
                id: contactId
            },
            onSuccess: callback,
            onFailure: failure
        });
    },
    
    doAsyncDelete: function(contactId, callback) {
        return new Mojo.Service.Request(ContactsMojoService.identifier, {
            method: 'delete',
            parameters: {
                id: contactId
            },
            onSuccess: callback
        });
    },
    
    deletePerson: function(sceneController, personId) {
        var deleteCallbacks = function(resp) {
            var that = this;
            resp.list.each(function(c) {
                that.doAsyncDelete(c.id, null);
            });
        };
        sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'getLinkedContacts',
            parameters: {
                id: personId
            },
            onSuccess: deleteCallbacks.bind(this)
        });
    },
    
    getLinkedContacts: function(personId, callback, sceneController, failback, dontSubscribe) {
        var params = {
            id: personId,
            subscribe: !dontSubscribe,
            returnStrings: false
        };
        if (sceneController) {
            return sceneController.serviceRequest(ContactsMojoService.identifier, {
                method: 'getLinkedContacts',
                parameters: params,
                onSuccess: callback,
                onFailure: failback
            });
        } else {
            return new Mojo.Service.Request(ContactsMojoService.identifier, {
                method: 'getLinkedContacts',
                parameters: params,
                onSuccess: callback,
                onFailure: failback
            });
        }
    },
    
    linkContacts: function(sceneController, primaryId, idList, callback) {
        if (!sceneController) {
            return new Mojo.Service.Request(ContactsMojoService.identifier, {
                method: 'link',
                parameters: {
                    primaryId: primaryId,
                    toLink: idList
                },
                onSuccess: callback
            });
        }
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'link',
            parameters: {
                primaryId: primaryId,
                toLink: idList
            },
            onSuccess: callback
        });
    },
    
    linkByContactIDs: function(params) {
        var primaryContact = params.primaryId;
        var contactsToLink = params.contactsToLink;
        var stageController = params.stageController;
        var onSuccess = params.onSuccess;
        var onFailure = params.onFailure;
		//XXX: should be rename outParams?
        var params = {
            method: 'linkContacts',
            parameters: {
                primaryId: primaryContact,
                toLink: contactsToLink
            },
            onSuccess: onSuccess,
            onFailure: onFailure
        };
        if (stageController) {
            return stageController.serviceRequest(ContactsMojoService.identifier, params);
        } else {
            return new Mojo.Service.Request(ContactsMojoService.identifier, params);
        }
    },
    
    unlinkContact: function(sceneController, id, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'unlink',
            parameters: {
                id: id
            },
            onSuccess: callback
        });
    },
    
    setPrimary: function(sceneController, id, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'setPrimary',
            parameters: {
                id: id
            },
            onSuccess: callback
        });
    },
    
    markVIP: function(sceneController, callback, id, type, val) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'markVIP',
            parameters: {
                id: id,
                type: type,
                value: val
            },
            onSuccess: callback
        });
    },
    
    addToLauncher: function(sceneController, callback, personId, name, icon) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'addToLauncher',
            parameters: {
                id: personId,
                name: name,
                icon: icon
            },
            onSuccess: callback
        });
    },
    
    handleAddToLauncher: function(sceneController, personId, launcherId) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'handleAddToLauncher',
            parameters: {
                personId: personId,
                launchPointId: launcherId
            }
        });
    },
    
    listContactPoints: function(targetId, callback, sceneController) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'listContactPoints',
            parameters: {
                id: targetId,
                includeEmails: true,
                includeIMs: true
            },
            onSuccess: callback
        });
    },
    
    getSortOrder: function(sceneController, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'getSortOrder',
            parameters: {},
            onSuccess: callback
        });
    },
    
    setSortOrder: function(sceneController, sortOrder, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'setSortOrder',
            parameters: {
                order: sortOrder
            },
            onSuccess: callback
        });
    },
    
    isFirstUse: function(callback) {
        this.cookie = this.cookie || new Mojo.Model.Cookie("contacts");
        var cookie = this.cookie.get();
        if (!cookie) {
            callback({
                firstUse: true
            });
            return;
        } else {
            callback(cookie);
            return;
        }
        //		var firstUseObject = this.depot.get(this.firstUseKey, callback, callback);
        //		Mojo.Log.info("FIRST USE OBJECT : " + Object.toJSON(firstUseObject))
        //		if(!firstUseObject || firstUseObject.isFirstUse == true) return {firstUse: true};
        //		return {firstUse: false};
        //    if(this.isNotFirstUse){
        //		  callback({firstUse: false});
        //			return;
        //		}
        //		return new Mojo.Service.Request(ContactsMojoService.identifier, {
        //			method: 'isFirstUse',
        //			parameters: {},
        //			onSuccess: callback
        //		});
    },
    
    firstUseKey: "isFirstUse",
    
    setFirstUseToFalse: function() {
        this.cookie = this.cookie || new Mojo.Model.Cookie("contacts");
        this.cookie.put({
            firstUse: false
        });
        //		this.isNotFirstUse = true;
        ////		this.depot.add(this.firstUseKey, {isFirstUse: false});
        //		return new Mojo.Service.Request(ContactsMojoService.identifier, {
        //			method:"setFirstUseToFalse"
        //		})
    },
    
    getDefaultAccount: function(callback, sceneController) {
        if (sceneController) {
            return sceneController.serviceRequest(ContactsMojoService.identifier, {
                method: 'getDefaultAccount',
                parameters: {
                    subscribe: true
                },
                onSuccess: callback
            });
        } else {
            return new Mojo.Service.Request(ContactsMojoService.identifier, {
                method: 'getDefaultAccount',
                parameters: {
                    subscribe: true
                },
                onSuccess: callback
            });
        }
    },
    
    setDefaultAccount: function(sceneController, id, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'setDefaultAccount',
            parameters: {
                defaultAccountId: id
            },
            onSuccess: callback
        });
    },
    
    getQuickDials: function(sceneController, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'getQuickDials',
            parameters: {
                returnStrings: false
            },
            onSuccess: callback
        });
    },
    
    removeQuickDials: function(sceneController, letter) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'removeQuickDials',
            parameters: {
                letter: letter
            }
        });
    },
    
    setQuickDial: function(sceneController, contactPointId, letter) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'setQuickDial',
            parameters: {
                quickDial: letter,
                contactPointId: contactPointId
            }
        });
    },
    
    setPersonDetails: function(sceneController, personId, details, failure) {
        var params = {};
        params = Object.extend(params, details);
        params.id = personId;
        if (sceneController) {
            return sceneController.serviceRequest(ContactsMojoService.identifier, {
                method: 'setPersonDetails',
                parameters: params,
                onFailure: failure
            });
        } else {
            return new Mojo.Service.Request(ContactsMojoService.identifier, {
                method: 'setPersonDetails',
                parameters: params,
                onFailure: failure
            });
        }
    },
    
    getSIMAccount: function(sceneController, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'getSIMAccount',
            parameters: {},
            onSuccess: callback
        });
    },
    
    generateContactPicturePath: function(sceneController, id, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'generateContactPicturePath',
            parameters: {
                id: id
            },
            onSuccess: callback
        });
    },
    
    deleteContactPicture: function(sceneController, id, path, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'deleteContactPicture',
            parameters: {
                id: id,
                path: path
            },
            onSuccess: callback
        });
    },
    
    countAccountsContacts: function(sceneController, accountId, callback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'countAccountContactRecords',
            parameters: {
                accountId: accountId
            },
            onSuccess: callback
        });
    },
    
    makeVCard: function(sceneController, params, callback, failCallback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'makeVCard',
            parameters: params,
            onSuccess: callback,
            onFailure: failCallback
        });
    },
    
    makeVCardList: function(sceneController, params, callback, failCallback) {
        return sceneController.serviceRequest(ContactsMojoService.identifier, {
            method: 'makeVCardList',
            parameters: params,
            onSuccess: callback,
            onFailure: failCallback
        });
    }
    
});

ContactsMojoService.identifier = 'palm://com.palm.contacts';
