import {
    API_SERVER
} from '../../config';
import {
    REQUEST_CUSTOMER_ACCEPTANCE,
    RECEIVE_CUSTOMER_ACCEPTANCE,
    UPLOAD_CUSTOMER_ACCEPTANCE,
    SEND_CUSTOMER_ACCEPTANCE,
    orderMessages
} from '../../constants/ordersConstants';
import {
    updateMessages
} from '../notification/notificationActions';
import HtmlClient from '../../helpers/HtmlClient';

const htmlClient = new HtmlClient();

const requestCustomerAcceptance = () => ({
    type: REQUEST_CUSTOMER_ACCEPTANCE
});
const recieveCustomerAcceptance = (json) => ({
    type: RECEIVE_CUSTOMER_ACCEPTANCE,
    customerAcceptance: json
});

export const fetchCustomerAcceptance = (idOrder) => {
    return dispatch => {
        dispatch(requestCustomerAcceptance());
        return htmlClient.fetch({
                url: `${API_SERVER}/orders/api/getCustomerAcceptance`,
                method: 'post',
                data: {
                    idOrder
                }
            })
            .then(response => {
                if (response.data) {
                    dispatch(recieveCustomerAcceptance(response.data));
                }
            })
            .catch(error => {
                htmlClient.onError(error, dispatch);
            });
    }
}

const uploadAcceptanceAction = () => ({
    type: UPLOAD_CUSTOMER_ACCEPTANCE
});

export const uploadAcceptance = (idOrder, file) => {
    return dispatch => {
        dispatch(uploadAcceptanceAction());
        return htmlClient.uploadFile(file, {
            url: `${API_SERVER}/orders/api/uploadAcceptanceDocument`,
            data: {
                idOrder
            }
        }).then(response => {
            if (typeof response.data !== 'undefined' && 'messages' in response.data) {
                dispatch(updateMessages(response.data.messages, orderMessages));
                dispatch(fetchCustomerAcceptance(idOrder));
            }
        }).catch(error => {
            htmlClient.onError(error, dispatch);
        });
    }
}

export const badFile = () =>{
    return dispatch => {
        dispatch(updateMessages([{code: 'warning', message: 'INVALID_FILE_ACCEPTANCE'}], orderMessages));
    }
}

const sendCustomerAcceptance = () => ({
    type: SEND_CUSTOMER_ACCEPTANCE
});

export const acceptDeclineInstallation = (idOrder, actionType, declineReason) => {
    return dispatch => {
        dispatch(sendCustomerAcceptance());
        return htmlClient.fetch({
                url: `${API_SERVER}/orders/api/acceptDeclineInstallation`,
                method: 'post',
                data: {
                    idOrder,
                    actionType,
                    declineReason
                }
            })
            .then(response => {
                if (response.data) {
                    dispatch(updateMessages(response.data.messages, orderMessages));
                    dispatch(fetchCustomerAcceptance(idOrder));
                }
            })
            .catch(error => {
                htmlClient.onError(error, dispatch);
            });
    }
}
