/*
 * Copyright 2017 Lime - HighTech Solutions s.r.o.
 *
 * 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.
 */

export function dispatchAction(dispatch, response) {
    if (response.data.next.length > 0) {
        if (response.data.result === "CONFIRMED") {
            const next = response.data.next;
            let authMethods = [];
            for (let key in next) {
                switch (next[key].authMethod) {
                    case "USER_ID_ASSIGN": {
                        // TODO - implement USER_ID_ASSIGN, for now it is ignored
                        break;
                    }
                    case "USERNAME_PASSWORD_AUTH": {
                        dispatch({
                            type: "SHOW_SCREEN_LOGIN",
                            payload: {
                                loading: false,
                                error: false,
                                message: "login.pleaseLogIn"
                            }
                        });
                        break;
                    }
                    case "POWERAUTH_TOKEN": {
                        // add powerauth token authentication method for operation review step
                        authMethods.push("POWERAUTH_TOKEN");
                        break;
                    }
                    case "SMS_KEY": {
                        // add powerauth token authentication method for operation review step
                        authMethods.push("SMS_KEY");
                        break;
                    }
                }
            }
            if (authMethods.length > 0) {
                dispatch({
                    type: "SHOW_SCREEN_OPERATION_REVIEW",
                    payload: {
                        authMethods: authMethods
                    }
                });
            }
        } else if (response.data.result === "AUTH_FAILED") {
            dispatch({
                type: "SHOW_SCREEN_ERROR",
                payload: {
                    message: response.data.message
                }
            })
        }
    } else {
        if (response.data.result === "CONFIRMED") {
            dispatch({
                type: "SHOW_SCREEN_SUCCESS",
                payload: {
                    message: response.data.message
                }
            })
        } else if (response.data.result === "AUTH_FAILED") {
            dispatch({
                type: "SHOW_SCREEN_ERROR",
                payload: {
                    message: response.data.message
                }
            })
        }
    }
}

export function dispatchError(dispatch, error) {
    let errorMessage;
    if (error.response) {
        errorMessage = error.response.data.message;
    } else if (error.request) {
        errorMessage = "message.invalidRequest"
    } else {
        errorMessage = error.message;
    }
    dispatch({
        type: "SHOW_SCREEN_ERROR",
        payload: {
            message: errorMessage
        }
    })
}
@@ -3190,144 +3190,725 @@
     
-errorMessage = error.response.data.message;%0A    %7D else if (error.request) %7B%0A        errorMessage = %22message.invalidRequest%22%0A    %7D else %7B
+if (error.response.data.responseObject.message) %7B%0A            // structured error responses which include a responseObject with a message%0A            errorMessage = error.response.data.responseObject.message;%0A        %7D else %7B%0A            // responses with message field in the data%0A            errorMessage = error.response.data.message;%0A        %7D%0A    %7D else if (error.request) %7B%0A        // the request was made but no response was received%0A        // see: https://www.npmjs.com/package/axios#handling-errors%0A        errorMessage = %22message.invalidRequest%22%0A    %7D else %7B%0A        // something happened in setting up the request that triggered an Error%0A        // see: https://www.npmjs.com/package/axios#handling-errors
 %0A   

Support for responseObject in error messages, comments for resolving error messages in the UI