/*!
 * Copyright © 2016-2018 European Support Limited
 *
 * 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.
 */
import store from 'sdc-app/AppStore.js';
import React from 'react';
import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
import i18n from 'nfvo-utils/i18n/i18n.js';
import SubmitErrorResponse from 'nfvo-components/SubmitErrorResponse.jsx';

function showVariablesInMessage(variables, msg) {
    let regex;
    variables.forEach((value, index) => {
        value = value.replace(';', ',');
        regex = new RegExp("'%" + (index + 1) + "'");
        msg = msg.replace(regex, value);
    });
    return msg;
}

function parseCatalogExceptionObject(responseJSON) {
    let title, msg;
    if (
        responseJSON.requestError &&
        responseJSON.requestError.policyException
    ) {
        title = 'Error: ' + responseJSON.requestError.policyException.messageId;
        msg = responseJSON.requestError.policyException.text;
    } else if (
        responseJSON.requestError &&
        responseJSON.requestError.serviceException
    ) {
        title =
            'Error: ' + responseJSON.requestError.serviceException.messageId;
        msg = responseJSON.requestError.serviceException.text;
        let { variables } = responseJSON.requestError.serviceException;
        if (variables) {
            msg = showVariablesInMessage(variables, msg);
        }
    } else if (responseJSON.uploadDataErrors) {
        title = i18n('Error: Upload Data Error');
        msg = (
            <SubmitErrorResponse
                validationResponse={{
                    uploadDataErrors: responseJSON.uploadDataErrors
                }}
            />
        );
    } else {
        title = responseJSON.status;
        msg = responseJSON.message;
    }
    return { title, msg };
}

var errorResponseHandler = error => {
    let errorData;
    if (error.data) {
        errorData = parseCatalogExceptionObject(error.data);
    } else {
        errorData = {
            title: error.statusText,
            msg: error.responseText ? error.responseText : i18n('GENERIC_ERROR')
        };
    }
    store.dispatch({
        type: modalActionTypes.GLOBAL_MODAL_ERROR,
        data: {
            ...errorData
        }
    });
};

export default errorResponseHandler;
