import fetch from 'isomorphic-fetch';

let domain = "";

const domainPort = CONFIG.config.domainPort;
const myHeaders = new Headers({
    // 'Access-Control-Allow-Origin': '*',
    // 'Content-Type': 'text/plain;charset=UTF-8',
    // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Content-Type': 'application/json; charset=UTF-8',
});

const myLogger = {};

myLogger.saveDomain = (ip)=>{
    domain = `http://${ip}:${domainPort}`;
};

myLogger.info = function () {
    let args = [].slice.call(arguments) || [];
    // console.log.apply(console,args);
    if(domain && args.length > 0){
        let bodyData = {};
        let time = new Date().getTime();
        bodyData["time"] = time;
        bodyData[args[0]] = args.slice(1);
        fetch(domain+'/track',{
            method : 'POST',
            mode : 'cors',
            headers : myHeaders,
            body : JSON.stringify(bodyData)
        }).then(response=>response.json())
            .then((json)=>{
            }).catch(e=>{
        })
    }
};

myLogger.error = function () {
    let args = [].slice.call(arguments) || [];
    if(domain && args.length > 0){
        let bodyData = {};
        let time = new Date().getTime();
        bodyData["time"] = time;
        bodyData[args[0]] = args.slice(1);
        fetch(domain+'/errortrack',{
            method : 'POST',
            mode : 'cors',
            headers : myHeaders,
            body : JSON.stringify(bodyData)
        }).then(response=>response.json())
            .then((json)=>{
            }).catch(e=>{
        })
    }
};

module.exports = myLogger;