package cn.orm.util.builder.base;

import java.util.Map;
import java.util.StringJoiner;

import cn.orm.util.builder.BuildWhere;
import cn.util.core.IsKit;
import cn.util.core.TypeKit;

public class SQLUpdate {
	
	protected static SQLCondition sqlCondition = new SQLCondition();
		
	public static String sqlUpdate(String tableName,String field,String value,String ids)
	{
		if(IsKit.isNullOrEmpty(tableName)||
		   IsKit.isNullOrEmpty(field)||
		   IsKit.isNullOrEmpty(value))
		{	
			return "";
		}
		
		String[] fieldArry = TypeKit.strToArray(field,",");
		String[] valueArry = TypeKit.strToArray(value,",");
		StringBuilder builder = new StringBuilder().append("UPDATE ");
		if(IsKit.isNotNullOrEmpty(fieldArry) && 
  		   IsKit.isNotNullOrEmpty(valueArry) &&
		   fieldArry.length == valueArry.length
		 ){
			builder.append(tableName).append(" ").append("SET ");
			for(int i = 0;i < fieldArry.length; ++i) {
				builder.append(fieldArry[i])
				.append(" = ")
				.append('"')
				.append(valueArry[i])
				.append('"')
				.append(",");
			}
		}
		if(builder.lastIndexOf(",") != -1){
			builder.deleteCharAt(builder.length() - 1);
		}
		String where = BuildWhere.genQueryWrapper(ids);
		return builder.append(where).toString();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	public static enum FUNC { NOW };

	private StringBuilder builder = null;		// main builder
	private StringJoiner joiner = null;			// set joiner
	private String condition = null;
	

	
	/***
	 * Constructor
	 */
	public SQLUpdate() {
		builder = new StringBuilder().append("UPDATE ");
		joiner = new StringJoiner(", ");
	}
	
	/***
	 * Where to update
	 * @param tableName			Table to be updated
	 * @return					update
	 */
	public SQLUpdate updateTable(String tableName) {
		builder.append(tableName).append(' ');
		return this;
	}
	
	public SQLUpdate setAttributeTo(Map<String, Object> setMap) 
	{
		StringBuilder build = new StringBuilder();
		for (Map.Entry<String, Object> entry :setMap.entrySet())
		{
			String key = entry.getKey();
			Object value = entry.getValue();
			if(build.length() > 0) {
				build.append(",");
			}
			build.append(key).append(" = ").append('"').append(value).append('"');
		}
		joiner.add(build.toString());
		return this;
	}
	
	/***
	 * What to update
	 * @param attributeName		Attribute to be updated
	 * @param value				updated to
	 * @return					update
	 */
	public SQLUpdate setAttributeTo(String attributeName, String value) {
		joiner.add(new StringBuilder()
					.append(attributeName).append(" = ").append('"').append(value).append('"').toString());
		return this;
	}
	
	/***
	 * What to update
	 * @param attributeName		Attribute to be updated
	 * @param value				updated to
	 * @return					update
	 */
	public SQLUpdate setAttributeTo(String attributeName, int value) {
		joiner.add(new StringBuilder()
					.append(attributeName).append(" = ").append('"').append(value).append('"').toString());
		return this;
	}
	
	/***
	 * What to update
	 * @param attributeName		Attribute to be updated
	 * @param value				updated to
	 * @return					update
	 */
	public SQLUpdate setAttributeTo(String attributeName, char value) {
		joiner.add(new StringBuilder()
					.append(attributeName).append(" = ").append('"').append(value).append('"').toString());
		return this;
	}
	
	/***
	 * What to update
	 * @param attributeName		Attribute to be updated
	 * @param value				updated to
	 * @return					update
	 */
	public SQLUpdate setAttributeTo(String attributeName, boolean value) {
		joiner.add(new StringBuilder()
					.append(attributeName).append(" = ").append(value ? "TRUE" : "FALSE").toString());
		return this;
	}
	
	public SQLUpdate setAttributeTo(String attributeName, FUNC func) {
		joiner.add(new StringBuilder()
					.append(attributeName).append(" = ").append(func).append("()").toString());
		return this;
	}
	
	/***
	 * Where to update
	 * @param condition			Condition for update
	 * @return					update
	 */
	public SQLUpdate where(SQLCondition condition) {
		this.condition = condition.create();
		return this;
	}
	
	/***
	 * Where to update
	 * @param condition			Condition for update
	 * @return					update
	 */
	public SQLUpdate where(String condition) {
		this.condition = condition;
		return this;
	}
	
	/***
	 * Reset update
	 * @return				update
	 */
	public SQLUpdate clear() {
		builder = new StringBuilder().append("UPDATE ");
		joiner = new StringJoiner(", ");
		return this;
	}
	
	/***
	 * Return the final query
	 * @return 				update string
	 */
	public String create() {
		return toString();
	}
	
	/***
	 * Return the final query
	 */
	@Override
	public String toString() {
		return builder.append("SET ").append(joiner.toString()).append(' ')
					.append(condition != null ? "WHERE " : "").append(condition != null ? condition : "")
					.toString().trim();
	}
	
}
