package com.sharefile.api;

import javax.net.ssl.HttpsURLConnection;

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

import com.sharefile.api.constants.SFKeywords;

/*
 *   
  {
  "code": "InternalServerError",
  "message": 
  {
    "lang": "en-US",
    "value": "The process cannot access the file '\\\\sf_fileserver2\\nilesh\\todelete.docx' because it is being used by another process."
  }
  
  {
   "code":"NotFound",
   "message":
  	{
  	 "lang":"en-US",
  	 "value":"The item that you requested could not be found in the system."
  	}
  }
}
 */
public class SFV3Error 
{
	private static final String ERR_FORBIDDEN =   "Forbidden (403)";
	private static final String ERR_UNAUTHORIZD = "Unauthorized (401)";
	private static final String ERR_NOTREACHABLE = "Server Not reachable (503)";
	private static final String ERR_BADMETHOD = "Method not allowed (405)";
	
	public int httpResponseCode = 0;
	public String code = "";	
	
	public String mExtraInfo = null;	
	
	public static class ErrorMessage
	{
		public String lang = null;
		public String value = null;
	}
	
	public ErrorMessage message = new ErrorMessage();
			
	
	private String getErrorMessageFromErroCode()
	{
		switch(httpResponseCode)
		{
			case HttpsURLConnection.HTTP_FORBIDDEN: return ERR_FORBIDDEN;
			case HttpsURLConnection.HTTP_UNAUTHORIZED: return ERR_UNAUTHORIZD;
			case HttpsURLConnection.HTTP_UNAVAILABLE:return ERR_NOTREACHABLE;
			case HttpsURLConnection.HTTP_BAD_METHOD:return ERR_BADMETHOD;
			default: return "Unkown Error.("+ httpResponseCode+")";
		}
	}
	
	public SFV3Error()
	{
		
	}
		
	/**
	 *   V3Error are JSON objects. It might happen that the server returns a Non-Json object responsestring/or something we got from an http exception causing 
	 *   a JSONException in this constructor. In such case, the constructor simply returns the following:
	 *   <p>httpError = original code
	 *   <p>message.value = Exception stack
	 *   <p>mExtraInfo = original response string which we tried to parse
	 */
	public SFV3Error(int httpcode , String respSring)
	{				
		httpResponseCode = httpcode;
		mExtraInfo = null;
						
		if(respSring == null)
		{
			respSring = getErrorMessageFromErroCode();
		}
				
		try 
		{						
			JSONObject errorObject = new JSONObject(respSring);			
			code =  errorObject.optString(SFKeywords.CODE);			
			JSONObject messageObject = errorObject.getJSONObject(SFKeywords.MESSAGE);			
			message.value = messageObject.optString(SFKeywords.VALUE);
		} 
		catch (JSONException e) 
		{							
			message.value = "JSON Exception during constructing V3Error. See extraInfo for more details of server response";
			mExtraInfo = respSring;			
		}
	}
				
	/**
	 *   Call this constructor only for non-server error parsing. V3Error are JSON objects. It might happen that the server returns a Non-Json object responsestring/or something we got from an http exception causing 
	 *   a JSONException in this constructor. In such case, the constructor simply returns the following:
	 *   <p>httpError = original code
	 *   <p>message.value = Exception stack
	 *   <p>mExtraInfo = original response string which we tried to parse
	 */
	public SFV3Error(int httpcode , String respSring, String extraInfo)
	{
		httpResponseCode = httpcode;
		mExtraInfo = extraInfo;	
		
		if(respSring == null)
		{
			respSring = getErrorMessageFromErroCode();
		}
		
		message.value = respSring;				
	}
	
	public boolean isAuthError()
	{
		if(httpResponseCode == HttpsURLConnection.HTTP_UNAUTHORIZED)
		{
			return true;
		}
		
		return false;
	}
}
@@ -1128,18 +1128,16 @@
 ();%0A%09%09%09%0A
-%09%0A
 %09private
@@ -1174,16 +1174,16 @@
 oCode()%0A
+
 %09%7B%0A%09%09swi
@@ -1503,19 +1503,23 @@
  %22Un
-kown
+expected
  Error
-.
+ 
 (%22+ 
@@ -2511,24 +2511,27 @@
 %7B%09%09%09%09%09%09%09%0A%09%09%09
+// 
 message.valu
@@ -2631,16 +2631,61 @@
 ponse%22;%0A
+%09%09%09message.value = %22Error Parsing response%22;%0A
 %09%09%09mExtr
@@ -3567,16 +3567,16 @@
 %0A%09%09%7D%0A%09%09%0A
-
 %09%09return
@@ -3586,9 +3586,161 @@
 lse;%0A%09%7D%0A
+%09%0A%09public String getMessageStr() %7B%0A%09%09return ( message==null %7C%7C message.value==null %7C%7C message.value.isEmpty() ) ? %0A%09%09%09%22Unspecified%22 : message.value;%0A%09%7D%0A
 %7D

add method to get string version of the error message.