/*
 * Copyright (C) Burak Dede.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.burakdede.stormpath;

import com.stormpath.sdk.account.Account;
import com.stormpath.sdk.application.Application;
import com.stormpath.sdk.authc.AuthenticationRequest;
import com.stormpath.sdk.authc.AuthenticationResult;
import com.stormpath.sdk.authc.UsernamePasswordRequest;
import com.stormpath.sdk.resource.ResourceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by burakdede on 03/03/16.
 */
public class StormpathApi {

    private final static Logger LOGGER = LoggerFactory.getLogger(StormpathApi.class);

    private static Application application;

    public static void initStormpathApi(Application application) {
        StormpathApi.application = application;
    }

    /**
     * Authenticate user with given username and password
     *
     * @param username username string
     * @param password password string
     * @return Account instance or null
     */
    public static Account authenticate(String username, String password) {
        AuthenticationRequest request = UsernamePasswordRequest.builder()
                .setUsernameOrEmail(username)
                .setPassword(password)
                .build();

        try {
            AuthenticationResult result = application.authenticateAccount(request);
            Account account = result.getAccount();
            LOGGER.info("Authenticating user with username: {} and password: {}",
                    new Object[]{account.getUsername(), "******"});
            return account;
        } catch (ResourceException e) {
            e.printStackTrace();
            LOGGER.error("Can not authenticate user with username: {} and password: {}",
                    new Object[] { username, "******"});
            LOGGER.debug("\"{}\", status={}, code={}, moreInfo=\"{}\"",
                    new Object[] {e.getDeveloperMessage(), e.getStatus(), e.getCode(), e.getMoreInfo()});
        }

        return null;
    }

}

@@ -869,32 +869,72 @@
 asswordRequest;%0A
+import com.stormpath.sdk.client.Client;%0A
 import com.storm
@@ -1080,16 +1080,25 @@
 %0Apublic 
+abstract 
 class St
@@ -1229,29 +1229,428 @@
 ication 
-application;%0A
+stormpathApplicaiton;%0A%0A    private static Client stormpathClient;%0A%0A%0A    public static Application getStormpathApplicaiton() %7B%0A        return stormpathApplicaiton;%0A    %7D%0A%0A    public static Client getStormpathClient() %7B%0A        return stormpathClient;%0A    %7D%0A%0A    /**%0A     * This is where we inject stormpath stormpathApplicaiton instance%0A     *%0A     * @param application Application instance from stormpath%0A     */
 %0A    pub
@@ -1705,16 +1705,31 @@
 lication
+, Client client
 ) %7B%0A    
@@ -1745,25 +1745,81 @@
 pathApi.
-a
+stormpathClient = client;%0A        StormpathApi.stormpathA
 pplica
-t
 i
+t
 on = app
@@ -2351,25 +2351,34 @@
 esult = 
-a
+stormpathA
 pplica
-t
 i
+t
 on.authe

make api abstract for new methods and add client field