import axios from 'axios';

let bearerToken = localStorage.token || undefined;
// Je moet hier waarschijnlijk de poort aanpassen.
axios.defaults.baseURL = "https://localhost:44347/api";

class RobotAPI  {

    static getAllRobots = () => {
        return axios.request({
            url: '/robots'
        });
    }

    static getRobot = (id) => {
        return axios.request({
            url: '/robots/' + id
        })
    }

    static createRobot = (robot) => {
        return axios.request({
            url: '/robots',
            method: 'post',
            data: robot,
            headers: {
                "Authorization": "Bearer " + bearerToken,
                "Content-Type": "application/json"
            }
        })
    }

    static updateRobot = (robot, id) => {
        return axios.request({
            url: '/robots/' + id,
            method: 'put',
            data: robot,
            headers: {
                "Authorization": "Bearer " + bearerToken
            }
        })
    }

    static deleteRobot = (id) => {
        return axios.request({
            url: '/robots/' + id,
            method: 'delete',
            headers: {
                "Authorization": "Bearer " + bearerToken
            }
        })
    }

    static getAllManufacturers = () => {
        return axios.request({
            url: "/manufacturers",
        })
    }

    static getManufacturer = (id) => {
        return axios.request({
            url: "/manufacturers/" + id
        })
    }

    static createManufacturer = (manufacturer) => {
        return axios.request({
            url: '/manufacturers',
            method: 'post',
            data: manufacturer,
            headers: {
                "Authorization": "Bearer " + bearerToken,
                "Content-Type": "application/json"
            }
        })
    }

    static updateManufacturer = (manufacturer, id) => {
        return axios.request({
            url: '/manufacturers/' + id,
            method: 'put',
            data: manufacturer,
            headers: {
                "Authorization": "Bearer " + bearerToken
            }
        })
    }

    static deleteManufacturer = (id) => {
        return axios.request({
            url: '/manufacturers/' + id,
            method: 'delete',
            headers: {
                "Authorization": "Bearer " + bearerToken
            }
        })
    }

    static getAllCategories = () => {
        return axios.request({
            url: "/categories"
        })
    }

    static getCategory = (id) => {
        return axios.request({
            url: "/categories/" + id
        })
    }

    static createCategory = (category) => {
        return axios.request({
            url: '/categories',
            method: 'post',
            data: category,
            headers: {
                "Authorization": "Bearer " + bearerToken,
                "Content-Type": "application/json"
            }
        })
    }

    static updateCategory = (category, id) => {
        return axios.request({
            url: '/categories/' + id,
            method: 'put',
            data: category,
            headers: {
                "Authorization": "Bearer " + bearerToken
            }
        })
    }

    static deleteCategory = (id) => {
        return axios.request({
            url: '/categories/' + Number(id),
            method: 'delete',
            headers: {
                "Authorization": "Bearer " + bearerToken
            }
        })
    }

    static login = async (username, password) => {
        try {
            const result = await axios.request({
                url: '/user/authenticate',
                method: 'post',
                data: {
                    username,
                    password
                },
                responseType: 'text'
            })

            bearerToken = result.data;

            // Dit is absoluut niet veilig.
            // De token moet in cookies bewaard worden.
            // Zie https://gkoniaris.gr/security/how-to-securely-store-jwt-tokens/ voor meer info.
            // Voor cookies zijn React Hooks nodig (https://reactjs.org/docs/hooks-intro.html), aangezien we dit niet
            // bewaren we de token in de local storage.
            localStorage.token = bearerToken;
            return true
        } catch (error) {
            return false;
        }
    }

    static isLoggedIn = () => {
        return bearerToken !== undefined;
    }
}

export default RobotAPI;

