import { _decorator, Component, Node, sys } from 'cc';
import { config } from './config';
import { tools } from './tools';
import { userDataManager } from './manager/userDataManager';
const { ccclass, property } = _decorator;
@ccclass('http')
export class http {
    public static static_domain =  "https://zcapi.xwrun.com"
    public static statistics_domain =  "https://zcapi.xwrun.com"
    public static post(url,opt, call_back,method:string='POST') {
        var xml = new XMLHttpRequest()
        let request_url = http.static_domain+url
        xml.open(method,  request_url)
        xml.setRequestHeader('Content-Type', 'application/json');
        if(userDataManager.user_data!=null){
            xml.setRequestHeader('token',userDataManager.user_data.token);
        }
        if(opt==null){
            xml.send();
        }else{
            xml.send(JSON.stringify(opt));
        }
 
        // xml.send(opt);
        var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
        array.forEach(function (eventName) {
            xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
                //    var str =  '\nEvent : ' + eventName;
                var lstr = ""
                if (eventName === 'timeout') {
                    lstr += '(timeout)';
                }
                else if (eventName === 'loadend') {
                    lstr += '...loadend!';

                } else if (eventName === 'onerror') {

                }
            };
        });

        // Special event
        xml.onreadystatechange = function () {
            if (xml.readyState === 4 && xml.status >= 200) {
                call_back(null, xml.responseText);
            } else if (xml.status === 404) {
                call_back('404 page not found!', null);
            }
        };
    }

    public static get(url, call_back) {
        var xml = new XMLHttpRequest()
        let request_url = http.static_domain+url
        xml.open('GET', request_url)
        xml.setRequestHeader('Content-Type', 'application/json');
        xml.send();

        var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
        array.forEach(function (eventName) {
            xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
                //    var str =  '\nEvent : ' + eventName;
                var lstr = ""
                if (eventName === 'timeout') {
                    lstr += '(timeout)';
                }
                else if (eventName === 'loadend') {
                    lstr += '...loadend!';

                } else if (eventName === 'onerror') {

                }
            };
        });

        // Special event
        xml.onreadystatechange = function () {
            if (xml.readyState === 4 && xml.status >= 200) {
                call_back(null, xml.responseText);
            } else if (xml.status === 404) {
                call_back('404 page not found!', null);
            }
        };
    }

   

   

}


