package flexa.ff;
import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import javax.script.*;
import java.util.ArrayList;
public class subform extends FComponent {
    //
    // CONSTRUCTOR
    //
    public subform() {
        super("ff.subform");
    }
    
    // 
    // Methods
    //
    
    
    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.fobject = 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 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;

    }
    


    // 
    // Events
    //
	public void event(String aAction, FContext aContext) throws Exception {
        
    
        // add EVENT
        if( LibText.stringToArray("" + this.id + ".add").contains(aAction) ) {
        			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.fobject != null ) {
			    FObject tobj = ((FCollection)this.fobject).addNew(aContext);
			    srow.fobject = tobj;
			    srow.updateFromObject(tobj);
			}
			//this.addChild(srow);

        }
    


    
        // delete EVENT
        if( LibText.stringToArray("" + this.id + ".delete").contains(aAction) ) {
        			// Remove myself
			if( this.fobject != null ) {
			    ((FCollection)this.fobject).remove(this.fobject); 
			}
			this.removeMyself();

        }
    


	    super.event(aAction, aContext);
	}
	
	
	
	
}