package flexa.ff;

import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;


public class combo extends FField {
    
    public combo() {
        super("ff.combo");
    }

	
	public void event(String aAction, FContext aContext) throws Exception {
	    //Log.getInstance().info("COMBO " + this.name + " event " + aAction);
	    
	    if( (aAction.equals(this.name + ".select") || aAction.equals("start") || aAction.equals("onopen") || aAction.equals("refresh") || aAction.equals(this.name + ".refresh")    ) ) {
	        String tds = (String)getValue("datasource");
	        String trel = (String)getValue("relation");
	        String tuds = (String)getValue("_datasource");
	        String ttype = (String)getValue("type");
	        Log.info("COMBO " + ttype + " #" + (String)getValue("id") + " name "  + (String)getValue("name") + " event " + aAction + " ds " + tds + " rel " + trel + " uds " + tuds);
	        if( ttype != null && ttype.indexOf("autocomplete") >= 0 && !aAction.endsWith(".select") ) {
	            setValue("datasource", "/flexa/c?_c=" + aContext.mainComponent.getModule() + "/" + aContext.mainComponent.getName() +  "&_x=" + (String)getValue("id") + ".select");
	            Log.info("COMBO autocomplete dont fill with event " + aAction);
	        } else {
    	        if( (tds != null && tds.toLowerCase().startsWith("select")) || (tuds != null && tuds.toLowerCase().startsWith("select")) ) {
        	        if( tuds != null && tuds.toLowerCase().startsWith("select") ) tds = tuds;
        	        Log.getInstance().info("COMBO event " + aAction + " datasource : " + tds );
                    FResult result = Util.sqltoarrayddtotal(aContext, tds);
                    if( aAction.endsWith(".select") ) {
                        aContext.hash.put( ((String)getValue("name")) + ".select", result);
                    } else {
                        setValue("_datasource", tds);
                        setValue("datasource", result.toJsonMatrix());
                    }
        	    } else if( trel != null && !trel.trim().equals("") ) {
        	        String tRel = trel.trim();
        	        String tRelFilter = ((String)getValue("relationfilter"));
        	        FClass tClassRel = ORM.getInstance().getFClass(tRel);
        	        String tSql = tClassRel.generateSql(FClass.QUERY_ONLIST); //generateSqlSelectList(tRelFilter);
        	        Log.getInstance().info("COMBO event " + aAction + " sql : " + tSql);
        	        FResult result = Util.sqltoarrayddtotal(aContext, tSql);
                    
                    // Json
                    if( aAction.endsWith(".select") ) {
                        aContext.hash.put( ((String)getValue("name")) + ".select", result);
                    } else {
                        setValue("datasource", result.toJsonMatrix());
                        setValue("_datasource", "[relation]");
                    }
        	    }
    	    }
    	}
	    super.event(aAction, aContext);
	}
	
	
	public String getCompleteName() {
	    if( getValue("relation") != null && !(((String)getValue("relation")).trim().equals("")) ) {
	        return ((String)getValue("relation")).trim();
	    } else {
	        return "";
		    //return this.module + "." + this.name;
		}
	}


}