import {API_SERVER} from '../../config';
import HtmlClient from '../../helpers/HtmlClient';
import {
    REQUEST_ORDER_PROJECTS,
    RECEIVE_ORDER_PROJECTS,
    REQUEST_ADD_PROJECT,
    orderProjectsTexts
} from '../../constants/orderProjectsConstants';
import {updateMessages} from '../notification/notificationActions';
import {setDialogOpenFlag} from '../dialog/dialogActions';

const htmlClient = new HtmlClient();

const requestOrderProjects = () => ({
    type: REQUEST_ORDER_PROJECTS,
    isLoading: true
});
const recieveOrderProjects = (json) => ({
    type: RECEIVE_ORDER_PROJECTS,
    isLoading: false,
    orderProjects: json
});

const generateOptions = (orderProjects) => {
    orderProjects.forEach((orderProject) => {
        orderProject.value = orderProject.idProject;
        orderProject.label = orderProject.projectName;
    });

    return orderProjects;
}

export const getOrderProjects = () => {
    return dispatch => {
        dispatch(requestOrderProjects());

        dispatch(recieveOrderProjects(generateOptions([
            {
                "idProject": 1,
                "projectName": "Innovation Center",
                "isAvailable": 1
            },
            {
                "idProject": 2,
                "projectName": "Demo01",
                "isAvailable": 1
            },
            {
                "idProject": 3,
                "projectName": "Kontorsrådet",
                "isAvailable": 1
            },
            {
                "idProject": 4,
                "projectName": "PerProj01",
                "isAvailable": 1
            }
        ])));
        // return htmlClient.fetch({
        //         url: `${API_SERVER}/orderProjects/api/getOrderProjects`
        //     })
        //     .then(response => {
        //         if (response.data) {
        //             const orderProjects = generateOptions(response.data);
        //             dispatch(recieveOrderProjects(orderProjects));
        //         }
        //     })
        //     .catch(error => {
        //         htmlClient.onError(error, dispatch);
        //     });
    }
}

const requestAddProject = () => ({
    type: REQUEST_ADD_PROJECT,
    isLoading: true
});

export const addProject = (projectData) => {
    return dispatch => {
        dispatch(requestAddProject());
        return htmlClient.fetch({
                url: `${API_SERVER}/orderProjects/api/addOrderProject`,
                method: 'post',
                data: {projectData: JSON.stringify(projectData)}
            })
            .then(response => {
                if(response.data && response.data.messages){
                    dispatch(updateMessages(response.data.messages, orderProjectsTexts.messages));
                    if(response.data.messages[0].code === 'success'){
                        dispatch(getOrderProjects());
                        dispatch(setDialogOpenFlag(false));
                    }
                }
            })
            .catch(error => {
                htmlClient.onError(error, dispatch);
            });
    }
}
