package com.urucas.parseobjectserializer;

/**
 * Created by vruno on 1/26/16.
 */
import android.util.Log;

import com.parse.ParseFile;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Created by vruno on 1/25/16.
 */
public abstract class Serializer {

    public static JSONObject Serialize(ParseObject object) {

        JSONObject json = new JSONObject();
        if(object == null) return json;

        try {
            String property = object.getObjectId();
            json.put("id", property);

            Date date = object.getCreatedAt();
            json.put("createdAt", Serializer.ParseDate((Date)date));

            date = object.getUpdatedAt();
            json.put("updatedAt", Serializer.ParseDate((Date)date));

        }catch(Exception e){}

        for(String key: object.keySet()) {
            try { Push(json, key, object.get(key)); }catch(Exception e){ continue; }
        }
        return json;
    }

    public static JSONObject Push(JSONObject json, String key, Object property) {
        try {
            if (property == null)
                return json.put(key, "");

            if (property instanceof String || property instanceof Boolean
                    || property instanceof JSONArray || property instanceof JSONObject)
                return json.put(key, property);

            if (property instanceof Number)
                return json.put(key, ((Number) property).doubleValue());

            if (property instanceof ParseFile)
                return json.put(key, Serializer.Serialize((ParseFile) property));

            if (property instanceof ParseGeoPoint)
                return json.put(key, Serializer.Serialize((ParseGeoPoint) property));

            if (property instanceof Date)
                return json.put(key, Serializer.ParseDate((Date) property));

        }catch(Exception e){}
        return json;
    }

    public static String ParseDate(Date date) throws ParseException {
        String dateAsStr;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
        dateAsStr = formatter.format(date);
        return dateAsStr;
    }

    public static JSONObject Serialize(ParseFile file) throws JSONException{
        JSONObject json = new JSONObject();
        json.put("url", "");
        if(file != null && file.getUrl() != null) json.put("url", file.getUrl());
        return json;
    }

    public static JSONObject Serialize(ParseGeoPoint point) throws JSONException {
        JSONObject json = new JSONObject();
        if(point != null) {
            json.put("lat", point.getLatitude());
            json.put("lng", point.getLongitude());
        }else{
            json.put("lat", 0);
            json.put("lng", 0);
        }
        return json;
    }
}


@@ -392,15 +392,38 @@
 til.
-Objects
+HashMap;%0Aimport java.util.List
 ;%0A%0A/
@@ -661,38 +661,23 @@
 ry %7B
-%0A            String property =
+ json.put(%22id%22,
  obj
@@ -693,16 +693,17 @@
 jectId()
+)
 ;%0A      
@@ -708,38 +708,44 @@
     
-    json.put(%22id%22, property);%0A
+%7Dcatch(Exception e)%7B%7D%0A%0A        try %7B
 %0A   
@@ -841,32 +841,33 @@
 ParseDate((Date)
+ 
 date));%0A%0A       
@@ -962,16 +962,17 @@
 e((Date)
+ 
 date));%0A
@@ -1067,73 +1067,421 @@
 ry %7B
- Push(json, key, object.get(key)); %7Dcatch(Exception e)%7B continue;
+%0A                Object property = object.get(key);%0A                if(property instanceof HashMap)%0A                    property = object.getJSONObject(key);%0A%0A                if(property instanceof List)%0A                    property = object.getJSONArray(key);%0A%0A                Push(json, key, property);%0A%0A            %7Dcatch(Exception e)%7B%0A                e.printStackTrace();%0A                continue;%0A           
  %7D%0A 
@@ -1601,28 +1601,48 @@
 ty) 
-%7B%0A        try %7B%0A    
+throws JSONException, ParseException %7B%0A%0A
     
@@ -1666,32 +1666,16 @@
 == null)
-%0A               
  return 
@@ -1690,28 +1690,24 @@
 (key, %22%22);%0A%0A
-    
         if (
@@ -1856,36 +1856,32 @@
 ct)%0A            
-    
 return json.put(
@@ -1889,36 +1889,32 @@
 ey, property);%0A%0A
-    
         if (prop
@@ -1936,32 +1936,16 @@
  Number)
-%0A               
  return 
@@ -1995,36 +1995,32 @@
 ue());%0A%0A        
-    
 if (property ins
@@ -2037,32 +2037,16 @@
 rseFile)
-%0A               
  return 
@@ -2105,36 +2105,32 @@
 rty));%0A%0A        
-    
 if (property ins
@@ -2151,32 +2151,16 @@
 eoPoint)
-%0A               
  return 
@@ -2227,28 +2227,24 @@
 );%0A%0A        
-    
 if (property
@@ -2260,32 +2260,16 @@
 of Date)
-%0A               
  return 
@@ -2327,38 +2327,8 @@
 );%0A%0A
-        %7Dcatch(Exception e)%7B%7D%0A
     

check property type, Parse manages JSONObject as HashMap and JSONArray as List