(function(root, factory) {

if (typeof module === 'object' && module.exports) {
    // CommonJS-like environments that support module.exports, like Node.
    module.exports = factory(require('logtown').getLogger('/src/handlers/CreateRecurringPaymentSetupHandler'), require('../ApiClient'), require('../api/PaymentSetupApi'), require('../OrbipayApiError'), require('../CommonUtil'));
  } 
}(this, function(logger, ApiClient, PaymentSetupApi, OrbipayApiError, CommonUtil) {
  'use strict';

  /**
   * @module ApiClient
   */

  /**
   * <h3 style="color:red"> This class subject to change without prior notice, Please dont use this class directly. </h3>

   * @module handlers/CreateRecurringPaymentSetupHandler
   * @class
   */

  var exports = function() {
  };

  exports.prototype.process = function (paymentSetup, callback) {
        
        var secret = paymentSetup.invocation_context.secret;
		var host = paymentSetup.api_end_point;
        var api_key = paymentSetup.invocation_context.api_key;
        var auth_scheme = paymentSetup.invocation_context.auth_scheme;
        var _this=this;
		var response = {};
        var exception;
        try {
            var api_client = new ApiClient(secret, host,api_key,auth_scheme);
			var api_instance = new PaymentSetupApi(api_client);
			var channel = paymentSetup.invocation_context['channel'];
			var client_key = paymentSetup.invocation_context['client_key'];
			var product = paymentSetup.invocation_context['product'];
			var timestamp;
			if (paymentSetup.invocation_context['timestamp'])
			{
				timestamp = paymentSetup.invocation_context['timestamp'];
			}		
			else
			{
				timestamp = CommonUtil.getTimeStamp();
			}
			var idempotent_request_key = paymentSetup.invocation_context['idempotent_request_key'];
			var requestor_type = paymentSetup.invocation_context['requestor_type'];
			var id_customer = paymentSetup['ID_CUSTOMER'];
			var create_payment_setup_request = _this.populate_body(paymentSetup);
            var opts = _this.getOptions(paymentSetup);
			var result = api_instance.createRecurringPaymentSetup(channel, client_key, product, timestamp, idempotent_request_key, requestor_type, id_customer, create_payment_setup_request,  opts, callback);
			response['request'] = result;
			return response;
        } catch (e) {
            exception = OrbipayApiError.getDefaultException(e);
            if (callback) {
                callback(null, null, null, exception);
            }
            response['exception'] = exception;
            return response;
        }
    };

    exports.prototype.getOptions = function (paymentSetup) {
        return {
		    'requestor': paymentSetup['invocation_context']['requestor'], 
		    'x_opay_headers': paymentSetup['invocation_context']['X-OPAY-Headers'], 
		    'trace_id': paymentSetup['invocation_context']['trace_id']
        };
    };  

    exports.prototype.populate_body = function(paymentSetup){
		var CreatePaymentSetupRequest = require('../models/CreatePaymentSetupRequest');

		return  CreatePaymentSetupRequest.constructFromObject(paymentSetup);
	};


  return exports;
}));