package com.connor.erp.materialquery;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class InProcessReadXMLStr {
  public static void xmlElements(File xmlFilePath, List<String[]> tableDataList) {
    SAXBuilder sb = new SAXBuilder();
    try {
      Document doc = sb.build(xmlFilePath);
      Element root = doc.getRootElement();
//      loadElements(root.getChildren(), tableDataList);
    } catch (JDOMException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
  }
  
  public static void loadElements(Map<String, Object> inprocessInfoMap, List<String[]> tableDataList) {
    int inprocessInfoMapSize = inprocessInfoMap.size();
    if (inprocessInfoMapSize == 0)
      return; 
    for(Entry<String, Object> entry:inprocessInfoMap.entrySet()) {
		  JSONObject jsonObject = JSONObject.fromObject(entry.getValue());
		  /*  59 */       String segment1_value = entry.getKey();
//		  /*  60 */       String name_value = jsonObject.getString("itemName");
		  JSONArray jsonArray = jsonObject.getJSONArray("WIP");
        String[] tableData = new String[11];
        tableData[0] = segment1_value;
        tableData[1] = jsonObject.getString("description");
		  List<WIPInfo> wipList = (List<WIPInfo>)JSONArray.toList(jsonArray,WIPInfo.class);
		  for(WIPInfo w: wipList) {
			  tableData[2] = w.getWip_entyty_name();
			  tableData[3] = w.getCreation_date();
			  tableData[4] = w.getClass_code();
			  tableData[5] = w.getStart_quantity();
			  tableData[6] = w.getQuantity_completed();
			  tableData[7] = w.getQuantity_scrapped();
			  tableData[8] = w.getAssemblyNum();
			  tableData[9] = w.getItemcount();
			  tableData[10] = w.getItemquantity();
			  
			  tableDataList.add(tableData);
		  }
    }
    
//    for (int m = 0; m < inprocessInfoMapSize; m++) {
//      Element inprocessInfoEle = (Element)inprocessInfoMap.get(m);
//      List<?> inprocessInfoChildlist = inprocessInfoEle.getChildren();
//      int inprocessInfoChildlistSize = inprocessInfoChildlist.size();
//      String segment1_value = "";
//      String description_value = "";
//      for (int i = 0; i < inprocessInfoChildlistSize; i++) {
//        Element et = (Element)inprocessInfoChildlist.get(i);
//        String et_name = et.getName();
//        String et_value = et.getValue();
//        if (et_name.equals("segment1")) {
//          segment1_value = et_value;
//        } else if (et_name.equals("description")) {
//          description_value = et_value;
//        } else if (et_name.equals("inprocesses")) {
//          List<?> children = et.getChildren();
//          int childNum = children.size();
//          for (int j = 0; j < childNum; j++) {
//            Element childEt = (Element)children.get(j);
//            if (childEt.getName().equals("inprocess")) {
//              String[] tableData = new String[11];
//              tableData[0] = segment1_value;
//              tableData[1] = description_value;
//              List<?> children2 = childEt.getChildren();
//              int childNum2 = children2.size();
//              for (int k = 0; k < childNum2; k++) {
//                Element childEt2 = (Element)children2.get(k);
//                String childEt2_name = childEt2.getName();
//                String childEt2_value = childEt2.getValue();
//                if (childEt2_name.equals("wip_entity_name")) {
//                  tableData[2] = childEt2_value;
//                } else if (childEt2_name.equals("creation_date")) {
//                  tableData[3] = childEt2_value;
//                } else if (childEt2_name.equals("class_code")) {
//                  tableData[4] = childEt2_value;
//                } else if (childEt2_name.equals("Start_quantity")) {
//                  tableData[5] = childEt2_value;
//                } else if (childEt2_name.equals("quantity_completed")) {
//                  tableData[6] = childEt2_value;
//                } else if (childEt2_name.equals("quantity_scrapped")) {
//                  tableData[7] = childEt2_value;
//                } else if (childEt2_name.equals("assemblyNum")) {
//                  tableData[8] = childEt2_value;
//                } else if (childEt2_name.equals("itemcount")) {
//                  tableData[9] = childEt2_value;
//                } else if (childEt2_name.equals("itemquantity")) {
//                  tableData[10] = childEt2_value;
//                } 
//              } 
//              tableDataList.add(tableData);
//            } 
//          } 
//        } 
//      } 
//    } 
  }
}
