package org.bench4q.master.domain.entity;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.log4j.Logger;
import org.bench4q.master.Main;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.share.helper.FileHelper;
import org.springframework.web.multipart.MultipartFile;

@Entity
@Table(name = "Script")
public class Script {
	private int id;
	private String name;
	private String scriptContent;
	private String filteredScriptCnt;
	private int behaviorCount;
	private Date createDateTime;
	private User user;
	private Logger logger = Logger.getLogger(Script.class);

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", nullable = false)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "createDateTime", nullable = false)
	public Date getCreateDateTime() {
		return createDateTime;
	}

	public void setCreateDateTime(Date createDateTime) {
		this.createDateTime = createDateTime;
	}

	@Column(name = "scriptContent", columnDefinition = "LONGTEXT", nullable = false)
	public String getScriptContent() {
		return scriptContent;
	}

	public void setScriptContent(String scriptContent) {
		this.scriptContent = scriptContent;
	}

	@Column(name = "behaviorCount", nullable = false)
	public int getBehaviorCount() {
		return behaviorCount;
	}

	public void setBehaviorCount(int behaviorCount) {
		this.behaviorCount = behaviorCount;
	}

	@ManyToOne
	@JoinColumn(name = "userId", nullable = false)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public static Script createScriptWithoutId(String name,
			String scriptContent, User user, List<MultipartFile> paramFiles) {
		Script script = new Script();
		script.setBehaviorCount(0);
		script.setCreateDateTime(new Date());
		script.setName(name);
		script.setScriptContent(scriptContent);
		script.setUser(user);
		return script;
	}

	public boolean saveScriptParamFiles(List<MultipartFile> paramFiles) {
		String folderPath = buildParamFilesFolder();
		System.out.println(folderPath);
		FileHelper.guardFolderExist(folderPath);
		if (paramFiles == null) {
			return true;
		}
		for (MultipartFile multipartFile : paramFiles) {
			try {
				System.out.println(multipartFile.getOriginalFilename());
				multipartFile.transferTo(new File(folderPath
						+ Main.FILE_SEPARATOR
						+ multipartFile.getOriginalFilename()));

			} catch (Exception e) {
				logger.info(ExceptionLog.getStackTrace(e));
				return false;
			}
		}
		return true;
	}

	private String buildParamFilesFolder() {
		return Main.SCRIPT_PARAM_ROOT_FOLDER + Main.FILE_SEPARATOR
				+ this.getId() + Main.FILE_SEPARATOR;
	}

	public List<File> loadParamFiles() {
		File folder = new File(buildParamFilesFolder());
		if (!folder.isDirectory()) {
			return Collections.emptyList();
		}
		return Arrays.asList(folder.listFiles());
	}

	public String getFilteredScriptCnt() {
		return filteredScriptCnt;
	}

	public void setFilteredScriptCnt(String filteredScriptCnt) {
		this.filteredScriptCnt = filteredScriptCnt;
	}
}
