let executeCamp = {},
	semusiConfig = require('./../../config'),
	logger = require('../../../logger'),
	netcoreEmailAPI = require('./netcore-email');

let appsList = semusiConfig.campaignsApps,
		    counter = 0;

(function(executeCamp){
	executeCamp.emailCampaign = function(app){
		if(app){
			logger.info(`app => ${app}`);
				netcoreEmailAPI.getAllAppsIds({app_id:app},function(error, apps){
						if(error){
							logger.error(`apps list error =>> ${error}`);		 
						}
						else{
							logger.info(`apps => ${app}`);	 
								netcoreEmailAPI.processActiveCampaigns(apps, function(status){
									logger.info(`status:: ${status}`);
									logger.info('Done script');		 
										 
										//process.exit(1);
								});
						}
				});
		}
		else{
				counter = 0;
				function checkCampaignAppWise(counter){
						if(appsList[counter]){
								netcoreEmailAPI.getAllAppsIds(appsList[counter],function(error, apps){
										if(error){
											logger.error(`apps list error =>> ${error}`);		 
										}
										else{
												netcoreEmailAPI.processActiveCampaigns(apps, function(status){
													logger.info(`status:: ${status}`); 
														counter++;
														checkCampaignAppWise(counter);
												});
										}
								});
						}
						else{
							logger.info('Done script');
							 //process.exit(1);
						}
				}

				checkCampaignAppWise(counter);
		}
	}

	executeCamp.runCampaign = function(app){
		app = (app === undefined)? '' : app;
		executeCamp.emailCampaign(app);
		executeCamp.smsCampaign(app);
	}

}(executeCamp));

module.exports = executeCamp;
