package org.bench4q.master.domain.service;

import java.util.Date;
import java.util.List;
import javax.xml.bind.JAXBException;

import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Script;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.repository.ScriptRepositoty;
import org.bench4q.master.domain.repository.UserRepository;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class ScriptService {
	private ScriptRepositoty scriptRepositoty;
	private UserRepository userRepository;
	private Logger logger = Logger.getLogger(ScriptService.class);

	private ScriptRepositoty getScriptRepositoty() {
		return scriptRepositoty;
	}

	@Autowired
	private void setScriptRepositoty(ScriptRepositoty scriptRepositoty) {
		this.scriptRepositoty = scriptRepositoty;
	}

	private UserRepository getUserRepository() {
		return userRepository;
	}

	@Autowired
	private void setUserRepository(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	/**
	 * 
	 * @param scriptName
	 * @param userId
	 * @param scriptContent
	 * @param paramFiles
	 *            , this param may be null, and is permitted so.
	 * @return
	 */
	public boolean saveScript(String scriptName, int userId,
			String scriptContent, List<MultipartFile> paramFiles) {

		Script script = this.getScriptRepositoty().attach(
				Script.createScriptWithoutId(scriptName, scriptContent, this
						.getUserRepository().getEntity(userId), paramFiles));
		if (script == null) {
			return false;
		}
		return script.saveScriptParamFiles(paramFiles);

	}

	public Script getScript(int scriptId) {
		return this.getScriptRepositoty().getEntity(scriptId);
	}

	public Script getScriptByName(String name) {
		return this.getScriptRepositoty().getScriptByName(name);
	}

	public List<Script> loadScripts(User user) {
		return this.getScriptRepositoty().loadEntities(user);
	}

	public List<Script> queryScriptsByCreateTime(Date startDate, Date endDate,
			User user) {
		return this.getScriptRepositoty().getWith(
				Restrictions.between("createDateTime", startDate, endDate),
				user);
	}

	public RunScenarioModel getRunSceniroModelByScriptId(int scriptId) {
		String content = this.getScriptRepositoty().getEntity(scriptId)
				.getScriptContent();
		if (content == null) {
			return null;
		}
		try {
			return (RunScenarioModel) MarshalHelper.unmarshal(
					RunScenarioModel.class, content);
		} catch (JAXBException e) {
			logger.error(ExceptionLog.getStackTrace(e));
			return null;
		}
	}

	public boolean deleteScript(int id, int userId) {
		try {
			Script script = this.getScriptRepositoty().getEntity(id);
			if (script == null || script.getUser().getId() != userId) {
				return false;
			}
			return this.getScriptRepositoty().detach(id);
		} catch (Exception e) {
			return false;
		}
	}

	public boolean alterScriptContent(int id, int ownerId, String content,
			String scriptName) {
		Script script = this.getScriptRepositoty().getEntity(id);
		if (script.getUser().getId() != ownerId) {
			return false;
		}
		script.setName(scriptName);
		script.setScriptContent(content);
		return this.getScriptRepositoty().update(script);
	}

}
