const { ccclass, property } = cc._decorator;
@ccclass




export default class HTTPS {
    private static _instance = null;
    public static get Instance(): HTTPS {
        if (this._instance == null) {
            this._instance = new HTTPS();
        }
        return this._instance;
    }
    token = null;

    // {
    //     "id": 7,
    //     "username": "13137717769",
    //     "nickname": "131****7769",
    //     "avatar": "",
    //     "balance": "9989.00",
    //     "status_text": "正常"
    // }
    // user_info = null;
    public user_info =
        {
            "id": 7,
            "username": "13137717769",
            "nickname": "131****7769",
            "avatar": "",
            "balance": "9989.00",
            "status_text": "正常"
        }


    // private _URL = "http://120.79.5.67:7000";

    //得到现在的时间
    private GetNowTime(): string {
        const date = new Date(cc.sys.now());
        const hours = date.getHours();
        const minutes = date.getMinutes();
        const seconds = date.getSeconds();
        return `${hours}:${minutes}:${seconds}    `;
    }
    post(action: string, data: object | string): Promise<any> {
        if (CC_DEBUG) {
            cc.error(this.GetNowTime() + "发送消息    >>>>>> :" + action, data);
            // cc.error(this.GetNowTime() + "消息内容 >>>>>> :", data);
        }
        const promise: Promise<any> = new Promise((resolve, reject) => {
            var timeoutKey: any;
            var retry = 5;//失败重试次数
            const send = () => {
                const httpRequest = new XMLHttpRequest();
                const method = 'POST'
                httpRequest.onreadystatechange = (event: Event): void => {
                    const readyState = httpRequest.readyState;
                    if (readyState !== 4) return;
                    if (!timeoutKey) return;//已通知超时，之后收到数据后也不处理
                    clearTimeout(timeoutKey);
                    timeoutKey = null;
                    const requestStatus = httpRequest.status;
                    if (requestStatus >= 200 && requestStatus < 400) {
                        let resp = JSON.parse(httpRequest.responseText);
                        resolve(resp);
                        if (CC_DEBUG) {
                            cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
                            // cc.error(httpRequest.responseText);
                        }
                    }
                    else if (retry-- > 0) send();
                    else reject(new Error('全部超时'));
                }

                httpRequest.open(method, Url + action, true);
                // httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
                httpRequest.setRequestHeader("Content-Type", "application/json;charset=utf-8");

                if (this.token) {
                    httpRequest.setRequestHeader("token", this.token);
                }
                var text = typeof (data) == "string" ? data : JSON.stringify(data);
                data ? httpRequest.send(text) : httpRequest.send();
                timeoutKey = setTimeout(() => {
                    timeoutKey = null;
                    httpRequest.abort();
                    reject(new Error(`Timeout`));
                }, 12000);//超时时间(毫秒)
            }
            send();
        })
        return promise;
    }
    get(action: string): Promise<any> {

        if (CC_DEBUG) {
            cc.error(this.GetNowTime() + "发送消息    >>>>>> :" + action);
        }

        const promise: Promise<any> = new Promise((resolve, reject) => {
            var timeoutKey: any;
            var retry = 5;//失败重试次数
            const send = () => {
                const httpRequest = new XMLHttpRequest();
                const method = 'GET'
                httpRequest.onreadystatechange = (event: Event): void => {
                    const readyState = httpRequest.readyState;
                    if (readyState !== 4) return;
                    if (!timeoutKey) return;//已通知超时，之后收到数据后也不处理
                    clearTimeout(timeoutKey);
                    timeoutKey = null;
                    const requestStatus = httpRequest.status;
                    if (requestStatus >= 200 && requestStatus < 400) {
                        let resp = JSON.parse(httpRequest.responseText);
                        resolve(resp);
                        if (CC_DEBUG) {
                            cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
                        }
                    }
                    else if (retry-- > 0) send();
                    else reject(new Error('全部超时'));
                }

                httpRequest.open(method, action, true);
                if (this.token) {
                    httpRequest.setRequestHeader("token", this.token);
                }
                httpRequest.send();
                timeoutKey = setTimeout(() => {
                    timeoutKey = null;
                    httpRequest.abort();
                    reject(new Error(`Timeout`));
                }, 12000);//超时时间(毫秒)
            }
            send();
        })
        return promise;
    }
}
export var HTTPSIns = HTTPS.Instance

export var Url = "http://yx.dashenwangluo.com";


export var NetGet = {
    /**获取任务配置 */
    Task: Url + "/v1/task/config",//token: 用户token

    /**获取提现规则 */
    rule: Url + "/v1/withdraw/rule",//token: 用户token

    /**获取提现记录 */
    withdrawHistory: Url + "/v1/withdraw/log?page=1&limit=20",//token: 用户token
}




export var NetPost = {
    /**发送验证码 */
    SendIphoneCode: "/v1/user/send_code",
    /**手机号登录 */
    IphoneLogin: "/v1/user/login",


    /**完成任务 */
    complete: "/v1/task/complete",// task_type: 任务类型(ad_watch/sign_in)


    /**申请提现 */
    withdraw: "/v1/withdraw/apply",// task_type: 任务类型(ad_watch/sign_in)



    /**记录广告观看 */
    record: "/v1/ad/record",// task_type: 任务类型(ad_watch/sign_in)



}
