package com.github.aureliano.achmed.resources;

import org.apache.log4j.Logger;

import com.github.aureliano.achmed.command.CommandBuilder;
import com.github.aureliano.achmed.command.CommandFacade;
import com.github.aureliano.achmed.command.CommandResponse;
import com.github.aureliano.achmed.exception.ExecResourceException;
import com.github.aureliano.achmed.helper.StringHelper;
import com.github.aureliano.achmed.resources.properties.ExecProperties;
import com.github.aureliano.achmed.resources.properties.IResourceProperties;

public class ExecResource implements IResource {

	private static final Logger logger = Logger.getLogger(ExecResource.class);
	
	private ExecProperties properties;
	
	public ExecResource() {
		this.properties = new ExecProperties();
	}
	
	public ExecResource(ExecProperties properties) {
		this.properties = properties;
	}

	public void apply() {
		logger.info(" >>> Apply exec resource with command: " + this.properties.getCommand());
		
		this.properties.configureAttributes();
		
		if (this.canExecute()) {
			this.execute();
		}
	}
	
	public void setProperties(IResourceProperties properties) {
		this.properties = (ExecProperties) properties;
	}
	
	public IResourceProperties getProperties() {
		return this.properties;
	}

	public ResourceType type() {
		return ResourceType.EXEC;
	}
	
	private boolean canExecute() {
		boolean canExecute = true;
		
		if (!StringHelper.isEmpty(this.properties.getOnlyIf())) {
			canExecute = canExecute && this.shouldExecute(true, this.properties.getOnlyIf());
		}
		
		if (!StringHelper.isEmpty(this.properties.getUnless())) {
			canExecute = canExecute && this.shouldExecute(false, this.properties.getUnless());
		}
		
		return canExecute;
	}
	
	private boolean shouldExecute(boolean queryCase, String cmd) {
		CommandBuilder commandBuilder = this.createCommandBuilder(cmd);
		CommandResponse res = CommandFacade.executeCommand(commandBuilder);
		
		return (queryCase == res.isOK());
	}
	
	private void execute() {
		CommandResponse res = CommandFacade.executeCommand(this.properties);
		
		if (!res.isOK()) {
			String message = this.executionErrorMessage(res);
			logger.warn(message);
			
			throw new ExecResourceException(message);
		}
	}
	
	private CommandBuilder createCommandBuilder(String command) {
		return new CommandBuilder()
			.withCommand(command)
			.withWorkingDir(this.properties.getCwd())
			.withVerbose(this.properties.isVerbose());
	}
	
	private String executionErrorMessage(CommandResponse res) {
		return String.format(
			"Command [%s] exited with status code [%d]. Detail: %s",
			this.properties.getCommand(),
			res.getExitStatusCode(),
			res.getError()
		);
	}
}
@@ -2081,90 +2081,8 @@
 ) %7B%0A
-%09%09%09String message = this.executionErrorMessage(res);%0A%09%09%09logger.warn(message);%0A%09%09%09%0A
 %09%09%09t
@@ -2116,15 +2116,11 @@
 ion(
-m
+r
 es
-sage
 );%0A%09
@@ -2290,16 +2290,16 @@
 tCwd())%0A
+
 %09%09%09.with
@@ -2343,239 +2343,5 @@
 %0A%09%7D%0A
-%09%0A%09private String executionErrorMessage(CommandResponse res) %7B%0A%09%09return String.format(%0A%09%09%09%22Command %5B%25s%5D exited with status code %5B%25d%5D. Detail: %25s%22,%0A%09%09%09this.properties.getCommand(),%0A%09%09%09res.getExitStatusCode(),%0A%09%09%09res.getError()%0A%09%09);%0A%09%7D%0A
 %7D

Configure exception handling.