package cmput301w15t07.TravelTracker.util;

/*
 *   Copyright 2015 Kirby Banman,
 *                  Stuart Bildfell,
 *                  Elliot Colp,
 *                  Christian Ellinger,
 *                  Braedy Kuzma,
 *                  Ryan Thornhill
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Facade to wrap streams and GSON ugliness.
 * 
 * @author kdbanman
 *
 */
public class GsonIOManager {
	
	private Context ctx;
	private Gson gson;
	
	public GsonIOManager(Context ctx) {
		this.ctx = ctx;
		gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();;
	}
	
	public <T> T load(String filename, Type type) throws FileNotFoundException {
		T ret;
		try {
			FileInputStream fis = ctx.openFileInput(filename);
			InputStreamReader reader = new InputStreamReader(fis);
			
			ret = gson.fromJson(reader, type);
			
			fis.close();
		} catch (FileNotFoundException e) {
			throw e;
		} catch (IOException e) {
			Log.e("GSONIOManager", "file read failed");
			Log.e("GSONIOManager", e.getMessage());
			ret = null;
		}
		return ret;
	}
	
	public void save(Object toSave, String filename, Type type) {
		try {
			FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
			OutputStreamWriter writer = new OutputStreamWriter(fos);
			gson.toJson(toSave, type, writer);
			writer.flush();
			fos.close();
		} catch (IOException e) {
			Log.e("GSONIOManager", "file read failed");
			Log.e("GSONIOManager", e.getMessage());
		}
	}
}

@@ -1055,16 +1055,39 @@
 ct.Type;
+%0Aimport java.util.Date;
 %0A%0Aimport
@@ -1202,16 +1202,313 @@
 Builder;
+%0Aimport com.google.gson.JsonDeserializationContext;%0Aimport com.google.gson.JsonDeserializer;%0Aimport com.google.gson.JsonElement;%0Aimport com.google.gson.JsonParseException;%0Aimport com.google.gson.JsonPrimitive;%0Aimport com.google.gson.JsonSerializationContext;%0Aimport com.google.gson.JsonSerializer;
 %0A%0A/**%0A *
@@ -1742,44 +1742,82 @@
 r().
-excludeFieldsWithoutExposeAnnotation
+registerTypeHierarchyAdapter(Date.class, new DateAdapter()).serializeNulls
 ().c
@@ -2710,13 +2710,791 @@
 ));%0A%09%09%7D%0A
-
 %09%7D%0A
+%09%0A%09/*%0A%09 * Custom date gson adapter mix and matched from%0A%09 * %09http://stackoverflow.com/questions/6873020/gson-date-format%0A%09 * and from %0A%09 *  http://stackoverflow.com/questions/5671373/unparseable-date-1302828677828-trying-to-deserialize-with-gson-a-millisecond%0A%09 * on 4 April 2015%0A%09 */%0A%09private class DateAdapter implements JsonDeserializer%3CDate%3E, JsonSerializer%3CDate%3E %7B%0A%09%09@Override%0A%09%09public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException %7B%0A%09%09      return json == null ? null : new Date(json.getAsJsonPrimitive().getAsLong()); %0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public JsonElement serialize(Date date, Type typeOfT, JsonSerializationContext context) %7B%0A%09%09%09return date == null ? null : new JsonPrimitive(date.getTime());%0A%09%09%7D%0A%09%7D%0A%09%09%09%0A
 %7D%0A

fix accuracy issue with custom date adapter for gson.