import React from 'react';
import I18n from 'react-native-i18n'
import {demandeActionUrl, isDebugMode} from "./IlinkConstants"

import {readUser} from './AuthApi'

var DBEvents = require('react-native-db-models').DBEvents;
var db = require('./persistences/db.js');

export const loadDemandeCredit = async () => {
    const user = await readUser();
    const data = {"tag": "credit_demands", 'id': user.code_membre, "lang": I18n.currentLocale(), test: isDebugMode};

    const response = await fetch(demandeActionUrl, {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    });
    try {
        console.log(response)
        let responseJson = await response.json()
        console.log(responseJson)
        return responseJson;
    } catch (e) {
        console.log(response.text())
        return [];
    }

}
export const loadMyDemandeCredit = async () => {
    const user = await readUser();
    console.log("USER ID", user.agentId);
    const data = {
        "tag": "credit_demands_of_agent", id: user.agentId
        , "lang": I18n.currentLocale(),
        test: isDebugMode

    };
    console.log(data)
    const response = await fetch(demandeActionUrl, {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    });
    try {
        console.log(response, "my demande")
        let responseJson = await response.json()
        console.log(responseJson)
        return responseJson;
    } catch (e) {
        return [];
    }

}
export const updateCreditDemand = (phone, id) => {
    const data = {
        "tag": "update_ask_credit", "phone": phone, "id": id,
        "lang": I18n.currentLocale(),
        test: isDebugMode
    }
    return fetch(demandeActionUrl, {
        "method": "POST",
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    }).then((response) => {
        return response.json()
    })
        .then((responseJson) => {
            return responseJson
        })
        .catch((error) => {
            console.warn(error)
        })

}

export const sendDemande = async (credit) => {
    const muser = await readUser()
    if (muser !== undefined && muser.code_parrain !== undefined
        && muser.code_membre !== undefined &&
        muser.code_membre !== muser.code_parrain) {
        let data = {
            "tag": "ask_credit",
            "phone": muser.phone,
            "code": muser.code,
            "lang": I18n.currentLocale(),
            test: isDebugMode,
            "montant": credit
        };

        let response = await fetch(demandeActionUrl, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
        })
        let responseTxt = "";
        try {
            responseTxt = await response.text()
            let responseJson = JSON.parse(responseTxt)
            return responseJson
        } catch (e) {
            console.log(response)
            console.log("error", responseTxt)
            return []
        }

    } else
        return -1;
}
export const sendDemandeSpecificque = async (credit, phone, code_membre) => {
    let data = {
        "tag": "ask_credit",
        "phone": phone,
        test: isDebugMode,
        code: code_membre,
        "montant": credit, "lang": I18n.currentLocale()
    };

    let response = await fetch(demandeActionUrl, {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    })
    try {
        let responseJson = await response.json()
        console.warn("server response", responseJson);
        return responseJson
    } catch (e) {
        console.warn("server response", response);
        console.warn("server response", e);
        return []
    }

}