package com.github.felixgail.gplaymusic.api.exceptions;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Will be thrown when the network responds with an error.
 * It will contain the error code and (if available) a message.
 */
public class NetworkException extends IOException implements Serializable {

    private final static Gson gson = new Gson();
    @Expose
    @SerializedName("error")
    private ErrorHelper helper = new ErrorHelper();

    private Request request;

    public NetworkException(int code, String message) {
        helper = new ErrorHelper();
        helper.setCode(code);
        helper.setMessage(message);
    }

    public static NetworkException parse(Response response) {
        try {
            return gson.fromJson(response.body().charStream(), NetworkException.class);
        } catch (JsonSyntaxException e) {
            return new NetworkException(response.code(), response.message());
        }
    }

    public int getCode() {
        return helper.getCode();
    }

    public String getMessage() {
        return helper.getMessage();
    }

    public Request getRequest() {
        return request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    private String getRequestInformation() {
        if (request != null) {
            StringBuilder sb = new StringBuilder();
            sb.append("More Request Information:\n")
                    .append("URL: ").append(request.url()).append(System.lineSeparator());
            for (Map.Entry<String, List<String>> entry :
                    request.headers().toMultimap().entrySet()){
                sb.append(entry.getKey()).append(": ");
                for (String value : entry.getValue()) {
                    sb.append("\t");
                    if (value.startsWith("GoogleLogin")) {
                        sb.append("<ommited>");
                    } else {
                        sb.append(value);
                    }
                    sb.append(System.lineSeparator());
                }
            }
            return sb.toString();
        }
        return "";
    }

    @Override
    public String toString() {
        return String.format("%d: %s\n\n%s", getCode(), getMessage(), getRequestInformation());
    }

    private class ErrorHelper implements Serializable {
        @Expose
        private int code;
        @Expose
        private String message;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}

@@ -286,16 +286,62 @@
 esponse;
+%0Aimport okio.Buffer;%0Aimport okio.BufferedSink;
 %0A%0Aimport
@@ -802,29 +802,31 @@
 ivate Re
-quest request
+sponse response
 ;%0A%0A    p
@@ -1421,32 +1421,33 @@
 ssage();%0A    %7D%0A%0A
+%0A
     public Reque
@@ -1447,24 +1447,26 @@
 c Re
-quest getRequest
+sponse getResponse
 () %7B
@@ -1483,21 +1483,22 @@
 eturn re
-quest
+sponse
 ;%0A    %7D%0A
@@ -1523,29 +1523,32 @@
 etRe
-quest(Request request
+sponse(Response response
 ) %7B%0A
@@ -1566,23 +1566,25 @@
 s.re
-quest = request
+sponse = response
 ;%0A  
@@ -1629,24 +1629,25 @@
 rmation() %7B%0A
+%0A
         if (
@@ -1648,21 +1648,22 @@
   if (re
-quest
+sponse
  != null
@@ -1682,76 +1682,118 @@
     
-StringBuilder sb = new StringBuilder();%0A            sb.append(%22More 
+Request request = response.request();%0A            Buffer buffer = new Buffer();%0A            buffer.writeUtf8(%22
 Requ
@@ -1834,22 +1834,25 @@
        .
-append
+writeUtf8
 (%22URL: %22
@@ -1853,22 +1853,25 @@
 URL: %22).
-append
+writeUtf8
 (request
@@ -1876,24 +1876,143 @@
 st.url()
-).append
+.toString()).writeUtf8(System.lineSeparator())%0A                    .writeUtf8(%22Method: %22).writeUtf8(request.method()).writeUtf8
 (System.
@@ -2166,25 +2166,32 @@
         
-sb.append
+buffer.writeUtf8
 (entry.g
@@ -2199,22 +2199,25 @@
 tKey()).
-append
+writeUtf8
 (%22: %22);%0A
@@ -2292,25 +2292,32 @@
         
-sb.append
+buffer.writeUtf8
 (%22%5Ct%22);%0A
@@ -2399,25 +2399,32 @@
         
-sb.append
+buffer.writeUtf8
 (%22%3Commit
@@ -2483,25 +2483,32 @@
         
-sb.append
+buffer.writeUtf8
 (value);
@@ -2554,17 +2554,24 @@
     
-sb.append
+buffer.writeUtf8
 (Sys
@@ -2640,26 +2640,320 @@
     
-return sb.toString
+if (request.body() != null) %7B%0A                try %7B%0A                    buffer.writeUtf8(%22%5CnRequest body:%5Cn%22);%0A                    request.body().writeTo(buffer);%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A            return buffer.readUtf8
 ();%0A
@@ -3067,22 +3067,93 @@
 at(%22
-%25d
+A NetworkException occured: %5CnError Code: %25d %5CnMessage
 : %25s
+ 
 %5Cn%5Cn%25s
-%22,
+%5Cn%22,%0A               
  get

add only response to network exception