package com.huigou.uasp.form.pojo.analysis;

import java.util.ArrayList;
import java.util.List;

import net.sf.ezmorph.bean.MorphDynaBean;

import org.apache.commons.beanutils.DynaProperty;

import com.huigou.uasp.form.pojo.Event;
import com.huigou.uasp.form.pojo.Options;
import com.huigou.uasp.form.pojo.TableColumns;
import com.huigou.uasp.util.ReflectionUtil;

public class PublicAnalysis {

	  //**获取属性类型
		 public  static AttributeTypeInter  getAttrNameType(String attrName){
			  AttributeTypeInter inter=null;
		      switch(attrName)
			   { 
			     case "tableColumns":
			       inter=new  GetRootTableColumns();
				   break;
			     case "columns":  
				   inter=new  GetRootFormColumns();
				   break;	
			     case "options":  
					   inter=new  GetRootOptions();
					   break;	
			     case "event":  
					   inter=new  GetRootEvent();
					   break;	
			     case "rules":  
					   inter=new  GetRootRules();
					   break;	
			      default:
			    	  inter=new GetRootDefault();
			    	  break;
			   }
		      return inter;
		  }
	
	//获取表格 的 元素
	 public static List <TableColumns> getTableColumnsBeans(MorphDynaBean mbean,String objectName){
			   TableColumns object=null;
			   List <TableColumns> tableColumnsList=new ArrayList<TableColumns>();
	
			   object=new TableColumns(); 

			   List <MorphDynaBean>tabbeans=(List <MorphDynaBean>) mbean.get(objectName);
			   DynaProperty[] tabdynaProperties=null;
			   Object attrVal=null;
			   String attrName="";
	 		   for (MorphDynaBean tabbean:tabbeans){
	 			   object=new TableColumns(); 
	 			   tabdynaProperties = tabbean.getDynaClass().getDynaProperties();
	 		     for(int x=0; x<tabdynaProperties.length;x++){
	 			    attrName = tabdynaProperties[x].getName();//属性名称
	 			    attrVal = tabbean.get(attrName);//属性值
	 			   if(attrVal!=null&&!"null".equals(attrVal.toString().trim())){
	 	         	  System.out.println("tableColumns==="+attrName+":="+attrVal);
	 	         	  object=getTableColumnsByAttrName(attrName,tabbean,object,attrVal);
	 	           }
	 		      }
	 		      tableColumnsList.add(object);
	 		   }
	 		   return tableColumnsList;
	 	}
		
		//获取表单的元素
		public static List <TableColumns> getGridColumnBeans(MorphDynaBean mbean,String objectName){
			   TableColumns object=null;
			    List <TableColumns> tableColumnsList=new ArrayList<TableColumns>();
	  	        object=new TableColumns(); 

			   List <MorphDynaBean>formbeans=(List <MorphDynaBean>) mbean.get(objectName);
			   DynaProperty[] tabdynaProperties=null;
			   DynaProperty[] coldynaProperties=null;
			   List <MorphDynaBean>colbeans=null;
			   Object attrVal=null;
			   String attrName="";
	 		   for (MorphDynaBean formbean:formbeans){
	 			  
	 			   tabdynaProperties = formbean.getDynaClass().getDynaProperties();
	 		     for(int x=0; x<tabdynaProperties.length;x++){
	 			    attrName = tabdynaProperties[x].getName();//属性名称
	 			  // System.out.println("GridColumns9000==="+attrName);
	 			  
	 			    attrVal = formbean.get(attrName);//属性值
	 			    
	 			   if(attrVal!=null&&!"null".equals(attrVal.toString().trim())){
	 	         	//  System.out.println("GridColumns==="+attrName+":="+attrVal);
	 	          	if  ("list".equals(attrName)){
	 	          	    
	 	            	 colbeans=(List <MorphDynaBean>) formbean.get(attrName);
	 	            	 //System.out.println("GridColumns===");
	 	             for (MorphDynaBean colbean:colbeans){
	 	            	 object=new TableColumns(); 
	 	            	 coldynaProperties = colbean.getDynaClass().getDynaProperties();
	 	      		     for(int y=0; y<coldynaProperties.length;y++){  //
	 	      			    attrName = coldynaProperties[y].getName();//属性名称
	 	      			    attrVal = colbean.get(attrName);//属性值
	 	      			  //System.out.println("GridColumns==="+attrName+":----="+attrVal);
	
	 	      		      object=getTableColumnsByAttrName(attrName,formbean,object,attrVal);
	 	      		 
	 	       		   } //coldynaProperties ended
	 	      		  tableColumnsList.add(object);
	 	             }//colbeans  ended 
	 	           
	 	            }
	 			   }//list ended
	 		     }
	 		      
	 		   }
	 		   return tableColumnsList;
		}
		
	
		
		  //**//根据属性 组装对象 
		 public  static  TableColumns  getTableColumnsByAttrName(String attrName,MorphDynaBean bean,TableColumns tableColumn,Object attrVal){
		
		      switch(attrName)
			   { 
			   
			     case "options":  
			    
		       		 Options options=new Options();
		       		 options=(Options)getEndNodeBean(options,(MorphDynaBean)attrVal,attrName);
		       		 tableColumn.setOptions(options);
					   break;	
			     case "event":  
	
		      	     Event event=new Event();
		      	     event=(Event)getEndNodeBean(event,(MorphDynaBean)attrVal,attrName);
		      	     tableColumn.setEvent(event);
					  break;	
			     case "novalid":  
				
					   break;	
			     case "rules":  
			
					   break;	
			      default:
			    	  if (ReflectionUtil.getSetMethod(tableColumn.getClass(),attrName)!=null){
		        	    ReflectionUtil.invokeSet(tableColumn, attrName, attrVal);
		        	  }
			    	  break;
			   }
		      return tableColumn;
		  }
		 
	
	public static Object getEndNodeBean(Object object, MorphDynaBean bean,String attrName) {

		Object attrVal = null;
		DynaProperty[] tabdynaProperties = bean.getDynaClass().getDynaProperties();
		for (int e = 0; e < tabdynaProperties.length; e++) {
			attrName = tabdynaProperties[e].getName();// 属性名称
			attrVal = bean.get(attrName);// 属性值
		  if (attrVal != null && !"null".equals(attrVal.toString().trim())) {
			 if (ReflectionUtil.getSetMethod(object.getClass(), attrName) != null) {
				if ("defaultValue".equals(attrName)) { // 多个地方同属性 但类型不一致
				  System.out.println("========s");
				} else {
				  ReflectionUtil.invokeSet(object, attrName, attrVal);
				}
			  }
		  }
		}
		return object;
	}
	
	
}
