import React from "react";
import Axios from "axios";

class usersService {
    constructor() {
        console.log("Er are here anyway");
    }

    logInUser(reqData) {
       // debugger;
        /*
        clean up the request data here
         */
        localStorage.setItem("session_token", ``);
        return this.postAuxEnd("/login", reqData);
    }

    getUserReminders(){
        return this.getAuxEnd("/reminders", null);
    }
    //----------------------------------------  -----
    //----------------------------------------  -----
    // Unified call below
    //----------------------------------------  -----
    //----------------------------------------  -----
    getAuxEnd(uri, reqData) {
        debugger;
        const session_token = localStorage.getItem("session_token");
        let axiosConfig = {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json;charset=UTF-8',
                'Authorization': `Basic ${session_token}`,
            }
        };
        const endPoint = process.env.REACT_APP_USERS_ENDPOINT + uri;
        return Axios.get(endPoint,axiosConfig)
            .then((response) => {
                // console.log(response);
                // res = response;
                // console.log("~~~~~~~ Toks2 GET ~~~~~~~~");
                return response;
            })
            .catch((error) => {
                if (error.response) {
                    //response status is an error code
                    console.log(error.response.status);
                } else if (error.request) {
                    //response not received though the request was sent
                    console.log(error.request);
                } else {
                    //an error occurred when setting up the request
                    console.log(error.message);
                }
            });
    }

    postAuxEnd(uri, reqData) {
        const endPoint = process.env.REACT_APP_USERS_ENDPOINT + uri;
        const session_token = localStorage.getItem("session_token");
        let axiosConfig = {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json;charset=UTF-8',
                'Authorization': `Basic ${session_token}`,
            }
        };
        //Access-Control-Allow-Origin
        var postData = {
            email: "test@test.com",
            password: "password"
        };
      //  Axios.defaults.headers.post['Content-Type'] ='application/json;charset=utf-8';
      //  Axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*'; //,axiosConfig
        return Axios.post(endPoint, reqData,axiosConfig)
            .then((response) => {
                console.log(response);
                // res = response;
                console.log("~~~~~~~ Toks2 POST ~~~~~~~~");
                return response;
            })
            .catch((error) => {
                if (error.response) {
                    //response status is an error code
                    console.log("ERROR-------------------------------------------------------");
                    console.log(error.response.status);
                    console.log("ERROR-------------------------------------------------------");
                } else if (error.request) {
                    //response not received though the request was sent
                    console.log("ERROR2-------------------------------------------------------");
                    console.log(error.request);
                    console.log("ERROR2-------------------------------------------------------");
                } else {
                    //an error occurred when setting up the request
                    console.log("ERROR3-------------------------------------------------------");
                    console.log(error.message);
                    console.log("ERROR3-------------------------------------------------------");
                }
            });
    }
}

export default usersService;
