/*
 * 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.account.AccountList;
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.client.Client;
import com.stormpath.sdk.resource.ResourceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

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

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

    private static Application stormpathApplicaiton;

    private static Client stormpathClient;

    public static Application getStormpathApplicaiton() {
        return stormpathApplicaiton;
    }

    public static Client getStormpathClient() {
        return stormpathClient;
    }

    /**
     * This is where we inject stormpath stormpathApplicaiton instance
     *
     * @param application Application instance from stormpath
     */
    public static void initStormpathApi(Application application, Client client) {
        StormpathApi.stormpathClient = client;
        StormpathApi.stormpathApplicaiton = 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) throws ResourceException {

        AuthenticationRequest request = new UsernamePasswordRequest(username, password);

        AuthenticationResult result = getStormpathApplicaiton().authenticateAccount(request);
        Account authenticated = result.getAccount();
        LOGGER.info("Authenticating user with username: {} and password: {}", new Object[]{authenticated.getUsername(), "******"});

        return authenticated;
    }


    /**
     * Search for given email in account list
     *
     * @param email Email or regex pattern to search for e.g *.@stormpath
     * @return AccountList of users matching
     */
    public static AccountList searchUser(String email) {
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("email", email);
        AccountList accounts = getStormpathApplicaiton().getAccounts(queryParams);

        return accounts;
    }


    /**
     * Stormpath specific link account link
     *
     * @param href
     * @return
     */
    public static Account getAccount(String href) {
        return getStormpathClient().getResource(href, Account.class);
    }


    /**
     * Create new user account with given username and password
     *
     * @param givenName given name of user
     * @param surname last name of user
     * @param email email of user
     * @param password password of user
     * @return Account instance of new user
     * @throws ResourceException
     */
    public static Account createUser(String givenName, String surname, String email, String password) throws ResourceException {

        Account account = getStormpathClient().instantiate(Account.class);
        account.setGivenName(givenName);
        account.setSurname(surname);
        account.setEmail(email);
        account.setPassword(password);

        Account created = getStormpathApplicaiton().createAccount(account);

        return created;
    }

}

@@ -2310,33 +2310,82 @@
 ame, password);%0A
-%0A
+        Account account = null;%0A        try %7B%0A    
         Authenti
@@ -2474,30 +2474,20 @@
     
-A
+    a
 ccount 
-authenticated 
 = re
@@ -2501,24 +2501,28 @@
 tAccount();%0A
+    
         LOGG
@@ -2601,28 +2601,22 @@
 ject%5B%5D%7Ba
-uthenticated
+ccount
 .getUser
@@ -2635,25 +2635,24 @@
 ***%22%7D);%0A
-%0A
         
 return a
@@ -2647,28 +2647,142 @@
     
-return authenticated
+%7D catch (ResourceException ex) %7B%0A            LOGGER.error(ex.getStatus() + %22 : %22 + ex.getMessage());%0A        %7D%0A%0A        return account
 ;%0A  
@@ -3922,33 +3922,82 @@
 urceException %7B%0A
-%0A
+        Account created = null;%0A        try %7B%0A    
         Account 
@@ -4055,32 +4055,36 @@
 class);%0A        
+    
 account.setGiven
@@ -4100,32 +4100,36 @@
 nName);%0A        
+    
 account.setSurna
@@ -4137,24 +4137,28 @@
 e(surname);%0A
+    
         acco
@@ -4178,32 +4178,36 @@
 email);%0A        
+    
 account.setPassw
@@ -4222,39 +4222,35 @@
 word);%0A%0A        
-Account
+   
  created = getSt
@@ -4293,16 +4293,135 @@
 ccount);
+%0A        %7D catch (ResourceException ex) %7B%0A            LOGGER.error(ex.getStatus() + %22 : %22 + ex.getMessage());%0A        %7D
 %0A%0A      

handle exception cases for stormpath resource exception