import React from "react";
import notification from 'antd/lib/notification';
import Spin from 'antd/lib/spin';

export default class Service {
    static spin(loading) {
        return loading && <Spin />;
    }

    static call(settings) {
        if (!settings.url || !settings.setData || !settings.setLoading) {
            console.error("Service.call", settings);
            return;
        }
        settings.setLoading(true);
        const fail = ()=> {
            settings.setLoading(false);
            notification["error"]({
                message: settings.errorTitle ? settings.errorTitle : "Erro no Serviço",
                description: settings.errorMessage ? settings.errorMessage : settings.url,
                style: {
                    marginTop: 100,
                }
            });
        };
        netuno.service({
            url: `/services/${settings.url}`,
            method: settings.method ? settings.method.toUpperCase() : "GET",
            credentials: 'include',
            headers: {
                'Content-Type': 'application/json'
            },
            body: settings.body ? settings.body : null,
            success: (response)=> {
                if (response.json) {
                    settings.setData(response.json);
                    settings.setLoading(false);
                } else {
                    fail();
                }
            },
            fail: ()=> {
                fail();
            }
        });
    }
}