package co.poynt.postman.model;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Map;

import org.springframework.http.HttpHeaders;

import com.fasterxml.jackson.databind.ObjectMapper;

public class PostmanRequest {
	public String id;
	public String headers; //String of \n separated headers with : separate name:value.
	public String url;
	public String method;
	public Object data; //Either String of escaped-JSON or [] empty array (for GET)
	public String dataMode; //not used
	public String name;
	public String description;
	public String descriptionFormat;
	public Long time;
	public Integer version;
	public Object responses;
	public String tests;
	public Boolean synced;
	
	public String getData(PostmanVariables var) {
		if (this.data instanceof String) {
			String result = (String) this.data;
			result = var.replace(result);
			return result;
		} else if (this.data instanceof ArrayList && dataMode.equals("urlencoded")) {
			ArrayList<Map<String,String>> formData = (ArrayList<Map<String,String>>) this.data;
			return urlFormEncodeData(var, formData);
		} else { //empty array
			return "";
		}
	}
	
	public String urlFormEncodeData(PostmanVariables var, ArrayList<Map<String,String>> formData) {
		String result = "";
		int i = 0;
		for (Map<String,String> m : formData) {
			result += m.get("key") + "=" + URLEncoder.encode(var.replace(m.get("value")));
			if (i < formData.size()-1) {
				result += "&";
			}
		}
		return result;
	}
	
	public String getUrl(PostmanVariables var) {
		return var.replace(this.url);
	}
	
	public HttpHeaders getHeaders(PostmanVariables var) {
		HttpHeaders result = new HttpHeaders();
		if (this.headers == null || this.headers.isEmpty()) {
			return result;
		}
		String h = var.replace(headers);
		String[] splitHeads = h.split("\n");
		for (String hp : splitHeads) {
			String[] pair = hp.split(":");
			String key = pair[0].trim();
			String val = pair[1].trim();
			result.set(key, val);
		}
		return result;
	}
}

@@ -155,61 +155,8 @@
 s;%0A%0A
-import com.fasterxml.jackson.databind.ObjectMapper;%0A%0A
 publ
@@ -226,16 +226,17 @@
 ders; //
+ 
 String o
@@ -273,16 +273,26 @@
 separate
+%0A%09%09%09%09%09%09%09//
  name:va
@@ -362,16 +362,17 @@
 data; //
+ 
 Either S
@@ -419,14 +419,51 @@
 (for
+%0A%09%09%09%09%09%09//
  GET)%0A
+%09public Object rawModeData;%0A
 %09pub
@@ -486,19 +486,8 @@
 ode;
- //not used
 %0A%09pu
@@ -682,17 +682,16 @@
 synced;%0A
-%09
 %0A%09public
@@ -732,29 +732,96 @@
 ar) %7B%0A%09%09
-if (this.data
+Object dataToUse = dataMode.equals(%22raw%22) ? rawModeData : data;%0A%0A%09%09if (dataToUse
  instanc
@@ -862,25 +862,25 @@
 String) 
-this.data
+dataToUse
 ;%0A%09%09%09res
@@ -937,25 +937,25 @@
 lse if (
-this.data
+dataToUse
  instanc
@@ -967,17 +967,21 @@
 rrayList
- 
+%0A%09%09%09%09
 && dataM
@@ -1024,32 +1024,33 @@
 List%3CMap%3CString,
+ 
 String%3E%3E formDat
@@ -1067,32 +1067,33 @@
 List%3CMap%3CString,
+ 
 String%3E%3E) this.d
@@ -1090,17 +1090,17 @@
 %3E%3E) 
-this.data
+dataToUse
 ;%0A%09%09
@@ -1154,16 +1154,17 @@
 lse %7B //
+ 
 empty ar
@@ -1184,25 +1184,24 @@
  %22%22;%0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09public Str
@@ -1243,17 +1243,20 @@
 les var,
- 
+%0A%09%09%09
 ArrayLis
@@ -1264,24 +1264,25 @@
 %3CMap%3CString,
+ 
 String%3E%3E for
@@ -1343,16 +1343,17 @@
 %3CString,
+ 
 String%3E 
@@ -1399,17 +1399,22 @@
 %22) + %22=%22
- 
+%0A%09%09%09%09%09
 + URLEnc
@@ -1481,17 +1481,19 @@
 a.size()
--
+ - 
 1) %7B%0A%09%09%09
@@ -1533,25 +1533,24 @@
  result;%0A%09%7D%0A
-%09
 %0A%09public Str
@@ -1619,17 +1619,16 @@
 rl);%0A%09%7D%0A
-%09
 %0A%09public

update to use request.rawModeData element when in raw mode.