package flexa.ff;

import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import javax.script.*;



public class subform extends FComponent {
    
    FCollection collection = null;

    public subform() {
        super("ff.subform");
    }

	public subformrow cloneToRow() throws CloneNotSupportedException {
		subformrow srow = new subformrow();
		
		int imax = -1;
		for( FComponent child: this.getChildren() ) {
		    if( !child.getType().equals("ff.subformrow") ) continue;
		    int ithis = child.getIndexFromId();
		    //Log.info(" child index "+  ithis);
		    if( ithis > imax ) imax = ithis;
		}
		String tnewid = this.id + "[" + (imax + 1) + "]";
		srow.setId(tnewid);
		srow.setName(tnewid);
		
		//srow.container = (String)this.getValue("name");

        // Rename children (fields): subformid[nn]_fieldname
		for (FComponent child : this.getChildren()) {
			//cloned.addChild((FComponent)child.clone());
			if( child.getType().equals("ff.subformrow") ) continue;
			FComponent tclonedchild = (FComponent)child.clone();
			tclonedchild.setId(tnewid + "_" + tclonedchild.getName());
            srow.addChild(tclonedchild);
		}
		this.addChild(srow);
		return srow;
	}	



	public void updateFromRequest(HashMap<String, Object> aParameters)  throws Exception {
	    super.updateFromRequest(aParameters);
	    //if( aParameters.containsKey(this.getValue("name")) )
        //   this.setValue("value", aParameters.get(this.getValue("name")).toString());
        
        // @todo update Object 
	}    
	
	public void updateFromObject(FObjectInterface aObject) throws Exception {
		String tName = (String)this.getValue("name");
		Log.getInstance().info("updateFromObject subform " + tName);
		if( tName != null && !tName.equals("") ) {
			if( aObject.hasMember(tName) ) {
			    FCollection fcol = (FCollection)aObject.getMember(tName);
			    this.collection = fcol; 
			    //@todo this.removeClonedChildren();
			    int totalrows = fcol.size();
			    for(int i = 0; i < totalrows; i++) {
			        FObject tobj = fcol.get(i);
			        // buscar obj en subformrows
			        subformrow srow = null;
			        for (FComponent element : this.getChildren()) {
			            if( element.getClass() == subformrow.class ) {
			                if( ((subformrow)element).fobject == tobj ) {
			                    srow = (subformrow)element;
			                    break;
			                }
			            }
			        }
			        if( srow == null ) {
    		            srow = this.cloneToRow();
    		            srow.setValue("id", this.id + "[" + i + "]");
    		            srow.setId(this.id + "[" + i + "]");
    		            //this.addChild(srow);
    		            srow.fobject = tobj;
    			    }
		            for (FComponent element : srow.getChildren()) {
				        element.updateFromObject(tobj); 
			        }	
			    }
			}
		}
	}
	
	
	public void event(String aAction, FContext aContext) throws Exception {
	    
	    if( aAction.equals(this.id + ".add") ) {
    	    Log.info("SUBFORM event " + aAction + " my id " + this.id);
            // New subformrow
            subformrow srow = this.cloneToRow();

            // If it has FCollection asociated, add new Object to collection
            if( this.collection != null ) {
                FObject tobj = this.collection.addNew(aContext);
                srow.fobject = tobj;
                srow.updateFromObject(tobj);
            }
            //this.addChild(srow);

	    } else if( aAction.equals(this.name + ".delete") ) {
	        // Remove myself
	        if( this.fobject != null ) {
	            this.collection.remove(this.fobject); 
	        }
	        this.removeMyself();
	    
	    }
	    
	    super.event(aAction, aContext);
	}
}