import { _decorator, Component, Node } from 'cc';
import { config } from './config';
const { ccclass, property } = _decorator;
@ccclass('http')
export class http  {
    public static domain  = "https://zcapi.xwrun.com";

    static post(url,data,call_back){
         var xml =  new XMLHttpRequest()
         xml.open('POST',http.domain+url)
         xml.setRequestHeader('Content-Type','application/json');
         xml.send(JSON.stringify(data));
        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') {
    
                }
                console.log("str==",str)
                console.log("lstr==",lstr)
            };
        });

        // Special event
        xml.onreadystatechange = function () {
            if (xml.readyState === 4 && xml.status >= 200) {
                //label.string = handler(xml.responseText);
                // console.log("xml.responseText==",xml.responseText)
                call_back(null,xml.responseText);
            } else if (xml.status === 404) {
                call_back('404 page not found!',null);
                console.log("status ==",'404 page not found!')
            } else if (xml.readyState === 3) {
                call_back('Request dealing!',null);
                console.log("status ==",'Request dealing!')
            } else if (xml.readyState === 2) {
                call_back('Request received!',null);
                console.log("status ==", 'Request received!')
            } else if (xml.readyState === 1) {
                call_back('Server connection established! Request hasn\'t been received',null);
                console.log("status ==", 'Server connection established! Request hasn\'t been received')
            } else if (xml.readyState === 0) {
                call_back( 'Request hasn\'t been initiated!',null);
                console.log("status ==", 'Request hasn\'t been initiated!')
            }
        };
    }


    public static getGameList(page:number,limit:number=6):string{
        return `/smistatic/levels/${limit}_${page}.json`
    }

    public static get_dyopen_id():string{
        return `/note/user/get_dyopen_id`
    }

    public static get_login():string{
        return `/note/user/login`
    }

    public static get_sys_config():string{
        return `/smistatic/sys_config.json`
    }

    public static get_level_info(id:number):string{
        return `/smistatic/level_info/${id}.json`
    }
    //场景 scene 0=共用 1 = 场景1 2 = 场景2，
    public static get_level_resource(id:number,scene:number):string{
        return `/smistatic/resource/${id}_${scene}.json`
    }

    public static sync_data(){
        return `/note/user/sync_data`
    }

    public static run_get(url,call_back){
        var xml =  new XMLHttpRequest()
        xml.open('GET',http.domain+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') {
   
               }
               console.log("str==",str)
               console.log("lstr==",lstr)
           };
       });

       // 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 run_post(url,data,call_back){
        var xml =  new XMLHttpRequest()
        xml.open('POST',http.domain+url)
        xml.setRequestHeader('Content-Type','application/json');
        xml.setRequestHeader('token',config.TOKEN);
        xml.send(JSON.stringify(data));
   
       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') {
   
               }
               console.log("str==",str)
               console.log("lstr==",lstr)
           };
       });

       // 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);
           } 
       };
   }
}


