package sikd.sikd5.sikd5bean.util;

import java.util.List;
import java.util.logging.Logger;
import java.lang.reflect.Field;

/**
 * @author  : Copas by agus :https://stackoverflow.com/questions/41510496/writing-generic-pojo-to-csv-transformer
 * @tanggal : Feb 8, 2019
 */
public class CSVUtil {

   private static final Logger LOGGER = Logger.getLogger(CSVUtil.class .getName());
   private final static char DEFAULT_SEPARATOR = ' ';

   public static String toCSV(List<?> objectList, char separator, char quote,boolean displayHeader) {

       StringBuilder result =new StringBuilder();  
       if (objectList.size() == 0) {
           return result.toString();
       }   

       if(displayHeader){
           result.append(getHeaders(objectList.get(0),separator)); 
           result.append("\n");
       }

       for (Object obj : objectList) {
           result.append(addObjectRow(obj, separator,quote)).append("\n");
       }

       return result.toString();
   }
   //https://tools.ietf.org/html/rfc4180
   private static String followCVSformat(String value) {

       String result = value;
       if (result.contains("\"")) {
           result = result.replace("\"", "\"\"");
       }
       return result;

   }
   public static String getHeaders(Object obj,char separator) {
       StringBuilder resultHeader = new StringBuilder(); 
       boolean firstField = true;
       Field fields[] = obj.getClass().getDeclaredFields();
       for (Field field : fields) {
           field.setAccessible(true);
          String value;
           try {
               value = field.getName();

               if(firstField){
                    resultHeader.append(value);
                    firstField = false;
                }
                else{
                   resultHeader.append(separator).append(value);
                }
               field.setAccessible(false);
           } catch (IllegalArgumentException  e) {
               LOGGER.severe(e.toString());
           }
       }           
     return resultHeader.toString();

   }


   public static String addObjectRow(Object obj, char separator,char quote) {

       StringBuilder csvRow =new StringBuilder();  
       Field fields[] = obj.getClass().getDeclaredFields();
       boolean firstField = true;
       for (Field field : fields) {
//          field.getType() in
           field.setAccessible(true);
           Object value;
           try {
               value = field.get(obj);
               if(value == null)
                   value = "";
               if(firstField){
                   csvRow.append(value);
                   firstField = false;
               }
               else{
                   csvRow.append(separator).append(value);
               }
               field.setAccessible(false);
           } catch (IllegalArgumentException | IllegalAccessException e) {
               LOGGER.severe(e.toString());
           }
       }
       return csvRow.toString();
   }
}
