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 android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

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

@@ -1024,16 +1024,47 @@
 mWriter;
+%0Aimport java.lang.reflect.Type;
 %0A%0Aimport
@@ -1171,25 +1171,19 @@
 son.
-reflect.TypeToken
+GsonBuilder
 ;%0A%0A/
@@ -1314,16 +1314,36 @@
 xt ctx;%0A
+%09private Gson gson;%0A
 %09%0A%09publi
@@ -1391,16 +1391,93 @@
  = ctx;%0A
+%09%09gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();;%0A
 %09%7D%0A%09%0A%09pu
@@ -1507,16 +1507,27 @@
 filename
+, Type type
 ) throws
@@ -1555,34 +1555,8 @@
 n %7B%0A
-%09%09Gson gson = new Gson();%0A
 %09%09T 
@@ -1719,41 +1719,12 @@
 er, 
-(new TypeToken%3CT%3E() %7B%7D).getT
+t
 ype
-()
 );%0A%09
@@ -1955,20 +1955,16 @@
 %0A%09public
- %3CT%3E
  void sa
@@ -1966,17 +1966,22 @@
 id save(
-T
+Object
  toSave,
@@ -2000,37 +2000,22 @@
 name
-) %7B%0A%09%09Gson gson = new Gson();
+, Type type) %7B
 %0A%09%09t
@@ -2184,41 +2184,12 @@
 ve, 
-(new TypeToken%3CT%3E() %7B%7D).getT
+t
 ype
-()
 , wr

revert to previously deleted type-as-parameter load and save methods.