package com.wepay.model;

import java.io.IOException;

import org.json.*;

import com.wepay.WePay;
import com.wepay.net.WePayResource;
import com.wepay.exception.WePayException;
import com.wepay.model.data.*;

public class App extends WePayResource {
	
	protected Long client_id;
	protected String client_secret;
	protected String status;
	protected AppData appData;

	public static App fetch(String access_token) throws JSONException, IOException, WePayException {
		JSONObject params = new JSONObject();
		params.put("client_id", WePay.client_id);
		params.put("client_secret", WePay.client_secret);
		String response = request("/app", params, access_token);
		App a = gson.fromJson(response, App.class);
		AppData ad = gson.fromJson(response, AppData.class);
		ThemeObjectData atod = gson.fromJson(response, ThemeObjectData.class);
		ad.theme_object = atod;
		a.appData = ad;
		return a;
	}
	
	public void modify(AppData newData, String access_token) throws JSONException, IOException, WePayException {
		JSONObject params = new JSONObject();
		params.put("client_id", WePay.client_id);
		params.put("client_secret", WePay.client_secret);
		if (newData.gaq_domains != null) params.put("gaq_domains", newData.gaq_domains);
		if (newData.theme_object != null) params.put("theme_object", ThemeObjectData.build_theme_object(newData.theme_object));
		JSONObject object = new JSONObject(request("/app/modify", params, access_token));
		this.client_id = WePay.client_id;
		this.status = object.getString("status");
		if (this.appData == null) this.appData = newData;
		else {
			if (newData.gaq_domains != null) this.appData.gaq_domains = newData.gaq_domains; 
			if (newData.theme_object != null) this.appData.theme_object = newData.theme_object;
		}
	}

	public Long getClient_id() {
		return client_id;
	}

	public String getStatus() {
		return status;
	}
	
	public ThemeObjectData getTheme_object() {
		return appData.theme_object;
	}
	
	public String[] getGaq_domains() {
		return appData.gaq_domains;
	}
	
}

@@ -917,20 +917,17 @@
 AppData 
-newD
+d
 ata, Str
@@ -1134,28 +1134,25 @@
 ret);%0A%09%09if (
-newD
+d
 ata.gaq_doma
@@ -1190,20 +1190,17 @@
 mains%22, 
-newD
+d
 ata.gaq_
@@ -1211,28 +1211,25 @@
 ins);%0A%09%09if (
-newD
+d
 ata.theme_ob
@@ -1304,20 +1304,17 @@
 _object(
-newD
+d
 ata.them
@@ -1532,20 +1532,17 @@
 pData = 
-newD
+d
 ata;%0A%09%09e
@@ -1550,28 +1550,25 @@
 se %7B%0A%09%09%09if (
-newD
+d
 ata.gaq_doma
@@ -1607,20 +1607,17 @@
 mains = 
-newD
+d
 ata.gaq_
@@ -1633,20 +1633,17 @@
 %0A%09%09%09if (
-newD
+d
 ata.them
@@ -1692,12 +1692,9 @@
 t = 
-newD
+d
 ata.

modify refactor