// Path: teslaapi/src/main/java/com/eleks/tesla/api/auth/AuthCredentials.java
// public class AuthCredentials {
//     public final String secret;
//     public final String username;
// 
//     public AuthCredentials(String userName, String secret) {
//         this.username = userName;
//         this.secret = secret;
//     }
// }
// 
// Path: teslaapi/src/main/java/com/eleks/tesla/api/auth/OAuthModule.java
// public class OAuthModule extends AuthModule {
//     public OAuthModule() {
//     }
// 
//     public void authorizeRequest(HttpUriRequest httpurirequest, AuthCredentials authcredentials) {
//         if (authcredentials != null) {
//             httpurirequest.addHeader(new BasicHeader("Authorization", String.format("Bearer %s", authcredentials.secret)));
//         }
//     }
// }
// 
// Path: teslaapi/src/main/java/com/eleks/tesla/api/decoder/JsonObjectEntityDecoder.java
// public class JsonObjectEntityDecoder extends EntityDecoder {
//     public Object decodeEntity(HttpEntity httpentity) throws JSONException {
//         JSONObject jsonobject = null;
//         String jsonString;
//         if (httpentity == null) {
//             return null;
//         }
//         try {
//             jsonString = EntityUtils.toString(httpentity).trim();
//         } catch (Exception e) {
//             Log.e("JsonObjectEntityDecoder", "decodeEntity", e);
//             return null;
//         }
//         if (!jsonString.isEmpty()) {
//             jsonobject = new JSONObject(jsonString);
//         }
//         return jsonobject;
//     }
// }
// 
// Path: teslaapi/src/main/java/com/eleks/tesla/api/exception/TeslaApiException.java
// public class TeslaApiException extends Exception {
// 
//     public static TeslaApiException fromCode(int code) {
//         switch (code) {
//             case HttpStatus.AUTH_CODE:
//                 return new NonAuthrizedException();
//         }
//         return new TeslaApiException();
//     }
// 
//     public TeslaApiException() {
//     }
// 
//     public TeslaApiException(String detailMessage) {
//         super(detailMessage);
//     }
// 
//     public TeslaApiException(String detailMessage, Throwable throwable) {
//         super(detailMessage, throwable);
//     }
// 
//     public TeslaApiException(Throwable throwable) {
//         super(throwable);
//     }
// }


import android.os.Build;
import com.eleks.tesla.api.auth.AuthCredentials;
import com.eleks.tesla.api.auth.OAuthModule;
import com.eleks.tesla.api.decoder.JsonObjectEntityDecoder;
import com.eleks.tesla.api.exception.TeslaApiException;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.security.KeyStore;
import java.util.Locale;
import java.util.Map;
    }

    public static TeslaApi getInstance() {
        if (instance == null) {
            throw new RuntimeException("TeslaApi instance should be initialized. use init() method");
        }
        return instance;
    }

    //TODO JSONObject should be reworked to some king of AuthResult entity
    public JSONObject login(final String userName, final String password) throws TeslaApiException {
        apiEngine.getCookieStore().clear();
        final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("grant_type", "password");
            jsonObject.put("client_id", OAUTH_CLIENT_ID);
            jsonObject.put("client_secret", OAUTH_CLIENT_SECRET);
            jsonObject.put("email", userName);
            jsonObject.put("password", password);
            return apiEngine.dispatchJSONPostRequest(jsonHashApiSpec, "oauth/token", jsonObject, null);
        } catch (Exception e) {
            throw new TeslaApiException("Can not perform authorization", e);
        }
    }

    //TODO JSONObject should be reworked to some king of AuthResult entity
    public JSONObject getVehicles(AuthCredentials authcredentials) throws TeslaApiException {
        return apiEngine.dispatchGetRequest(jsonHashApiSpec, "api/1/vehicles", null, authcredentials);
    }

// Path: teslaapi/src/main/java/com/eleks/tesla/api/auth/AuthCredentials.java
// public class AuthCredentials {
//     public final String secret;
//     public final String username;
// 
//     public AuthCredentials(String userName, String secret) {
//         this.username = userName;
//         this.secret = secret;
//     }
// }
// 
// Path: teslaapi/src/main/java/com/eleks/tesla/api/auth/OAuthModule.java
// public class OAuthModule extends AuthModule {
//     public OAuthModule() {
//     }
// 
//     public void authorizeRequest(HttpUriRequest httpurirequest, AuthCredentials authcredentials) {
//         if (authcredentials != null) {
//             httpurirequest.addHeader(new BasicHeader("Authorization", String.format("Bearer %s", authcredentials.secret)));
//         }
//     }
// }
// 
// Path: teslaapi/src/main/java/com/eleks/tesla/api/decoder/JsonObjectEntityDecoder.java
// public class JsonObjectEntityDecoder extends EntityDecoder {
//     public Object decodeEntity(HttpEntity httpentity) throws JSONException {
//         JSONObject jsonobject = null;
//         String jsonString;
//         if (httpentity == null) {
//             return null;
//         }
//         try {
//             jsonString = EntityUtils.toString(httpentity).trim();
//         } catch (Exception e) {
//             Log.e("JsonObjectEntityDecoder", "decodeEntity", e);
//             return null;
//         }
//         if (!jsonString.isEmpty()) {
//             jsonobject = new JSONObject(jsonString);
//         }
//         return jsonobject;
//     }
// }
// 
// Path: teslaapi/src/main/java/com/eleks/tesla/api/exception/TeslaApiException.java
// public class TeslaApiException extends Exception {
// 
//     public static TeslaApiException fromCode(int code) {
//         switch (code) {
//             case HttpStatus.AUTH_CODE:
//                 return new NonAuthrizedException();
//         }
//         return new TeslaApiException();
//     }
// 
//     public TeslaApiException() {
//     }
// 
//     public TeslaApiException(String detailMessage) {
//         super(detailMessage);
//     }
// 
//     public TeslaApiException(String detailMessage, Throwable throwable) {
//         super(detailMessage, throwable);
//     }
// 
//     public TeslaApiException(Throwable throwable) {
//         super(throwable);
//     }
// }


// Path: teslaapi/src/main/java/com/eleks/tesla/api/TeslaApi.java
import android.os.Build;
import com.eleks.tesla.api.auth.AuthCredentials;
import com.eleks.tesla.api.auth.OAuthModule;
import com.eleks.tesla.api.decoder.JsonObjectEntityDecoder;
import com.eleks.tesla.api.exception.TeslaApiException;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.security.KeyStore;
import java.util.Locale;
import java.util.Map;

    }

    public static TeslaApi getInstance() {
        if (instance == null) {
            throw new RuntimeException("TeslaApi instance should be initialized. use init() method");
        }
        return instance;
    }

    //TODO JSONObject should be reworked to some king of AuthResult entity
    public JSONObject login(final String userName, final String password) throws TeslaApiException {
        apiEngine.getCookieStore().clear();
        final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("grant_type", "password");
            jsonObject.put("client_id", OAUTH_CLIENT_ID);
            jsonObject.put("client_secret", OAUTH_CLIENT_SECRET);
            jsonObject.put("email", userName);
            jsonObject.put("password", password);
            return apiEngine.dispatchJSONPostRequest(jsonHashApiSpec, "oauth/token", jsonObject, null);
        } catch (Exception e) {
            throw new TeslaApiException("Can not perform authorization", e);
        }
    }

    //TODO JSONObject should be reworked to some king of AuthResult entity
    public JSONObject getVehicles(AuthCredentials authcredentials) throws TeslaApiException {
        return apiEngine.dispatchGetRequest(jsonHashApiSpec, "api/1/vehicles", null, authcredentials);
    }

    public JSONObject getVehicleState(long vehicleId, AuthCredentials authcredentials) throws TeslaApiException {