package org.opengroupware.jope.appserver.tests;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.opengroupware.jope.appserver.WOApplication;
import org.opengroupware.jope.appserver.WOAssociation;
import org.opengroupware.jope.appserver.WOContext;
import org.opengroupware.jope.appserver.WOElement;
import org.opengroupware.jope.appserver.WORequest;

public class WOElementTest {
  
  protected WOApplication application = null;
  protected WORequest     request     = null;
  protected WOContext     context     = null;
  
  @Before
  public void setUp() {
    this.application = new WOApplication();
    this.application.awake();
    
    this.request = new WORequest("GET", "/", "HTTP/1.1", 
                                 null /* headers  */,
                                 null /* content  */,
                                 null /* userinfo */);
    
    this.context = new WOContext(this.application, this.request);
  }

  @After
  public void tearDown() {
    this.context = null;
    this.request = null;
    
    this.application.sleep();
    this.application = null;
  }
  
  /* generating elements */
  
  protected String generateElement(WOElement e) {
    e.appendToResponse(this.context.response(), this.context);
    return this.context.response().contentString();
  }
  
  /* creating elements */
  
  @SuppressWarnings("unchecked")
  protected WOElement createElement(String _clazz, String _name,
                                    Map<String,WOAssociation> _assocs,
                                    WOElement _template)
  {
    Class elementClass = null;
    try {
      elementClass = Class.forName(_clazz);
    }
    catch (ClassNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      return null;
    }
    
    WOElement element = null;
    try {
      Constructor ctor;
      
      ctor = elementClass.getConstructor(new Class[] { 
                String.class, Map.class, WOElement.class });
      
      Object[] args = new Object[3];
      args[0] = _name;
      args[1] = _assocs;
      args[2] = _template;
      element = (WOElement)ctor.newInstance(args); 
    }
    catch (InstantiationException e) {
      // TODO: add proper error handling
      e.printStackTrace();
    }
    catch (IllegalAccessException e) {
      // TODO: add proper error handling
      e.printStackTrace();
    }
    catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (InvocationTargetException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return element;
  }
  
  protected Map<String,WOAssociation> createAssocMapFromArray(Object[] _arr) {
    Map<String, WOAssociation> amap = new HashMap<String, WOAssociation>();
    int count = _arr.length;
    
    for (int i = 0; i < count; i += 2) {
      Object v;
      
      v = _arr[i + 1];
      if (v instanceof WOAssociation)
        ; /* do nothing, its already an association */
      else if (v instanceof String) {
        /* if a String starts with "var:" its treated as a keypath */
        String s = (String)v;
        
        if (s.startsWith("var:"))
          v = WOAssociation.associationWithKeyPath(s);
        else
          v = WOAssociation.associationWithValue(s);
      }
      else {
        /* treat all other objects as values */
        v = WOAssociation.associationWithValue(v);
      }
      
      amap.put((String)_arr[i], (WOAssociation)v);
    }
    return amap;
  }

  protected WOElement createElement(String _clazz, String _name,
                                    Object[] _assocs, WOElement _template)
  {
    return this.createElement(_clazz, _name, 
                              this.createAssocMapFromArray(_assocs),
                              _template);
  }

  protected WOElement createElement(String _clazz, String _name,
                                    Object[] _assocs)
  {
    return this.createElement(_clazz, _name, _assocs, null);   
  }

  protected WOElement createElement(String _clazz, Object[] _assocs) {
    return this.createElement(_clazz, "test", _assocs, null);
  }
}
