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

Weave.Services.PaymentServer = new (Class.create(Weave.Services.CatalogServer,
{    
    $whenReadyServerUrl: function(callback)
	{	
		if (this._serverUrl) 
		{
			callback();
		}
		else 
		{
			// We need the payment server url and the carrier info
			var self = this;
			Weave.Services.AccountServices.getPaymentServerUrl(function(status, url)
			{
				self._serverUrl = status ? url : 'error:///';
				callback();
			});
		}
    },

    getCCTypes: function(binCountry, callback)
	{
        var self = this;
        this.getSecurityToken(function(token)
		{
            var ingetCCTypes = 
			{
                InGetCCTypes: 
				{
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId,
                    billToCountry: binCountry
                }
            };
            self._callServer('getCCTypes', ingetCCTypes, function(status, response, extra)
			{
				if (status) 
				{
					callback(true, response);
				}
				else if (response == "jsonexception")
				{
					callback(false, extra);
				}
				else
				{
					callback(false, { errorCode: response });
				}
            });
        });
        
    },
    
    setInvoiceEmail: function(emailAdd, callback)
    {
        var self = this;
        this.getSecurityToken(function(token)
        {
            var insetUserInfo = 
            {
                InSetUserInfo: 
                {
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId,
                    invoiceEmail: emailAdd
                }
            };
            self._callServer('setUserInfo', insetUserInfo, function(status, response, extra)
            {
                if (status)
                {
                    callback(true, response);
                }
                else if (response == "jsonexception")
                {
                   	callback(false, extra);
                }
                else 
                {
                    callback(false, {
                        errorCode: response
                     });
               	}
            });
        });
        
    },
    
    getBillToCountries: function(callback)
    {
        var self = this;
        this.getSecurityToken(function(token)
        {
            var ingetBillToCountries = 
            {
                InGetBillToCountries: 
                {
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId
                }
            };
           
            self._callServer('getBillToCountries', ingetBillToCountries, function(status, response, extra)
            {
            	if (status) 
            	{
            		callback(true, response);
            	}
            	else if (response == "jsonexception")
            	{
            		callback(false, extra);
            	}
            	else
            	{
            		callback(false, { errorCode: response });
            	}
             });
        });
        
    },
    
    verifyPaymentSetup: function(callback)
	{
        var self = this;
        this.getSecurityToken(function(token)
		{
            var inGetCCPaymentInfos = 
			{
                InGetCCPaymentInfos: 
				{
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId
                }
            };
            
            self._callServer('getCCPaymentInfos', inGetCCPaymentInfos, function(status, response, extra)
            {
                if (status) 
                {
                    if (!response.OutGetCCPaymentInfos.invoiceEmail) 
                        response.OutGetCCPaymentInfos.invoiceEmail = token.email;
                    callback(true, response);
                }
                else if (response == "jsonexception") 
                {
                    callback(false, extra);
                }
                else 
                {
                    callback(false,{
                            errorCode: response
                        });
                }
            });
        });
    },
	
    addAccount: function(address, ccInfo, callback)
	{
        var self = this;
		
        this.getSecurityToken(function(token)
		{
            //ccInfo.email = token.email;
            var accountInfo = 
			{
                InAddCCPaymentInfo: 
				{
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId,
                    billTo: address,
                    creditCard: ccInfo
                }
            };
 
            self._callServer('addCCPaymentInfo', accountInfo, function(status, response, extra)
			{
				if (status) 
				{
					if (response.OutAddCCPaymentInfo) 
					{
						callback(true,response);
					} 
					else 
					{
						callback(false,'badresponse');
					}
				} 
				else if (response == "jsonexception")
				{
					callback(false, extra);
				}
				else
				{
					callback(false, { errorCode: response });
				}
            });
        });
		
	},
	
	updateAccount: function(address, ccInfo, callback)
	{
        var self = this;
		
        this.getSecurityToken(function(token)
		{
            ccInfo.email = token.email;
            var accountInfo = 
			{
                InUpdateCCPaymentInfo: 
				{
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId,
					paymentInfoId : ccInfo.paymentInfoId,
                    billTo: address,
                    creditCard: ccInfo
                }
            };
			
            self._callServer('updateCCPaymentInfo', accountInfo, function(status, response, extra)
			{
				if (status) 
				{
					if (response.OutUpdateCCPaymentInfo) 
					{
						callback(true,response);
					} 
					else 
					{
						callback(false,'badresponse');
					}
				} 
				else if (response == "jsonexception")
				{
					callback(false, extra);
				}
				else
				{
					callback(false, { errorCode: response });
				}
            });
            
        });
    },
	
	removeAccount: function(ccInfo, callback)
	{
        var self = this;
        this.getSecurityToken(function(token)
		{
            ccInfo.email = token.email;
            var accountInfo = 
			{
                InRemoveCCPaymentInfo: 
				{
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId,
					paymentInfoId : ccInfo.paymentInfoId
                }
            };
            
            self._callServer('removeCCPaymentInfo', accountInfo, function(status, response, extra)
			{
				if (status) 
				{
					if (response.OutRemoveCCPaymentInfo) 
					{
						callback(true, response);
					} 
					else 
					{
						callback(false,'badresponse');
					}
				} 
				else if (response == "jsonexception")
				{
					callback(false, extra);
				}
				else
				{
					callback(false, { errorCode: response });
				}
            });
            
        });
    },

	capturePayment: function(orderObj, callback)
	{
        var self = this;
        this.getSecurityToken(function(token)
		{
            var orderInfo = 
			{
                InCapturePayment: 
				{
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId,
					order: orderObj
                }
            };
            
            self._callServer('capturePayment', orderInfo, function(status, response, extra)
			{
				if (status) 
				{
					if (response.OutCapturePayment) 
					{
						callback(true, response);
					} 
					else 
					{
						callback(false, 'badresponse');
					}
				}
				else if (response == "jsonexception")
				{
					callback(false, extra);
				}
				else
				{
					callback(false, { errorCode: response });
				}
            });
        });
    },
	
	getEmbargoedEmailExtensions: function(callback)
	{
        var self = this;
        this.getSecurityToken(function(token)
		{
            var inGetEmbargoedEmailExtensions = 
			{
                InGetEmbargoedEmailExtensions: 
				{
                    authToken: token.token,
                    accountAlias: token.email,
                    deviceId: token.deviceId
                }
            };
			
			self._callServer('getEmbargoedEmailExtensions', inGetEmbargoedEmailExtensions, function(status, response, extra)
			{
				if (status) 
				{
					if (response.OutGetEmbargoedEmailExtensions) 
					{	
						//callback(true,{"OutGetEmbargoedEmailExtensions":{"embargoedEmailExtensions":["cu", "ir", "kp", "sd", "sy"]}});
						callback(true, response);
					} 
					else 
					{
						callback(false, 'badresponse');
					}
				}
				else if (response == "jsonexception")
				{
					callback(false, extra);
				}
				else
				{
					callback(false, { errorCode: response });
				}
            });
        });
    },
}));
