package it.enea.cruise.model.parametricQuery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.PostLoad;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Fetch;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Getter;
import lombok.Setter;


@Entity (name = "parametricquery")
@Table (name = "cruise.parametricquery")
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ParametricQuery {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long queryid;
	private String name;
	//@JsonIgnore
	private String sql;
	private String description;
	private String queryType; // {SQL, API};
	
	@Transient
	@JsonProperty("metadata")
	@JsonInclude(JsonInclude.Include.NON_NULL)
	List<HashMap<String, String>> parameterMetadata;
	
	@ManyToMany
	@JoinTable(
	  name = "cruise.queries_parameters", 
	  joinColumns = @JoinColumn
	  (name = "queryid"), 
	  inverseJoinColumns = @JoinColumn(name = "paramid"))
	@Fetch(FetchMode.JOIN)
	//@JsonManagedReference(value = "parametricquery-parameter")
	@JsonProperty("parameters")
	@JsonIgnoreProperties("parametricquery")
	private List<LocalQueryParameter> parameters;
	
	@ManyToMany
	@JoinTable(
	  name = "cruise.queries_admittedqueryvalues", 
	  joinColumns = @JoinColumn (name = "queryid"), 
	  inverseJoinColumns = @JoinColumn(name = "admittedqueryvalueid"))
	private List<AdmittedQueryValues> admittedqueryvalues;
	
	@Transient
	Map<String, List<String>> values = new HashMap<String, List<String>>();
	
	@PostLoad
	public void postLoad(){

		
		for(AdmittedQueryValues aqv : admittedqueryvalues) {
			List<String> tempList; 
			if  (values.containsKey(aqv.getType())) {
				tempList = values.get(aqv.getType());
				tempList.add(aqv.getValue());
				
			}else {
				tempList = new ArrayList<String>();
				tempList.add(aqv.getValue());	
			}
			values.put(aqv.getType(), tempList);
		}
	}
	
//	public String getName() {
//		return name;
//	}
//	public void setName(String name) {
//		this.name = name;
//	}
//	public String getSql() {
//		return sql;
//	}
//	public void setSql(String sQL) {
//		sql = sQL;
//	}
//	public List<LocalQueryParameter> getParameters() {
//		return parameters;
//	}
//	public void setParameters(List<LocalQueryParameter> parameters) {
//		this.parameters = parameters;
//	}
//	public Long getQueryid() {
//		return queryid;
//	}
//	public void setQueryid(Long queryid) {
//		this.queryid = queryid;
//	}
//	public String getDescription() {
//		return description;
//	}
//	public void setDescription(String description) {
//		this.description = description;
//	}
//	public String getQueryType() {
//		return queryType;
//	}
//	public void setQueryType(String queryType) {
//		this.queryType = queryType;
//	}
//
//	public void setAdmittedqueryvalues(List<AdmittedQueryValues> admittedqueryvalues) {
//		this.admittedqueryvalues = admittedqueryvalues;
//	}
//
//	public void setValues(Map<String, List<String>> values) {
//		this.values = values;
//	}
//
//	public Map<String, List<String>> getValues() {
//		return values;
//	}
//
//	public List<HashMap<String, String>> getParameterMetadata() {
//		return parameterMetadata;
//	}
//
//	public void setParameterMetadata(List<HashMap<String, String>> parameterMetadata) {
//		this.parameterMetadata = parameterMetadata;
//	}
	
	
	
}
