// import { Event_Name } from "../core/event/event_mgr";
// import EventManager from "../core/event/EventManager";

export default class MessManager {
    private static _instance: MessManager;
    private constructor() { }
    public static getInstance(): MessManager {
        if (!MessManager._instance) {
            MessManager._instance = new MessManager();
        }
        return MessManager._instance;
    }

    //--胜利局数
    private winCount = 0;
    private winCountMax = 2;
    //--单局道具使用次数
    private itemCounts = [0, 0, 0];
    private itemCountMaxs = [1, 1, 1];

    private canRewardCount: number = 0;
    public haveReward: boolean = false;

    /**
     * 本地数据初始化
     */
    public initData() {

        if (CC_DEBUG) {
            window["loginData"] = {
                userId: 113,
                satoken: "dTtCi0jk7zsK1mJj6ujrk6YT5DBZ3V6NoMHTarvyJKDDS9GoVbHIf9l4t7SJO3J3ZXK3B23Ve4O8YbwdGp08PoaLscA5S7JkQozeLi7mdYyyj1O3pU7EuRAcktN4e1Zm",
            }
        }
        // window["loginData"] = {
        //     userId: 133,
        //     satoken: "dTtCi0jk7zsK1mJj6ujrk6YT5DBZ3V6NoMHTarvyJKDDS9GoVbHIf9l4t7SJO3J3ZXK3B23Ve4O8YbwdGp08PoaLscA5S7JkQozeLi7mdYyyj1O3pU7EuRAcktN4e1Zm",
        // }
    }

    // public initOneGame() {
    //     this.addPlayCount();
    //     this.itemCounts = [0, 0, 0];
    // }
    // public getItmeScore(index: number) {
    //     if (this.itemCounts[index] == 0) {
    //         return 10;
    //     } else if (this.itemCounts[index] == 1) {
    //         return 50;
    //     } else {
    //         return 0;
    //     }
    // }
    // public getCanRewardCount(): number {
    //     return this.canRewardCount;
    // }

    // public getCanPlay(): boolean {
    //     return this.winCount < this.winCountMax;
    // }

    // public getCanUseItem(index: number): boolean {
    //     return this.itemCounts[index] < this.itemCountMaxs[index];
    // }

    // public addItemCount(index: number, callBack, callFail = null) {
    //     if (this.itemCounts[index] == 0) {
    //         this.changeScore(-10, () => {
    //             this.itemCounts[index]++;
    //             callBack && callBack();
    //         }, () => {
    //             callFail && callFail();
    //         });
    //     } else {
    //         this.changeScore(-50, () => {
    //             this.itemCounts[index]++;
    //             callBack && callBack();
    //         }, () => {
    //             callFail && callFail();
    //         });
    //     }
    // }
    // public addWinCount() {
    //     this.winCount++;
    //     this.setWinTimes(this.winCount);
    // }

    public getPlayCount(callBack: Function, callFail: Function = null) {
        fetch("https://miniapi.maox.com.cn/api/index/get_login_num", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({ user_id: window["loginData"].userId })
        }).then((response: Response) => {
            return response.text()
        }).then((value) => {
            let valueJson = JSON.parse(value);
            if (valueJson["code"] == 1) {
                this.canRewardCount = valueJson["data"];
                console.log(" this.canRewardCount", this.canRewardCount)
                this.haveReward = this.canRewardCount > 0 ? true : false;
                callBack && callBack();
            } else {
                let options = {
                    title: "hint",
                    content: "今日已经通关一局，请明日再来！"
                }
                callFail && callFail();
            }
        })
    }
    public addGameCount(callBack: Function, callFail: Function) {
        fetch("https://miniapi.maox.com.cn/api/index/add_game_num", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ user_id: window["loginData"].userId, "satoken": window["loginData"].satoken })
        }).then((response: Response) => {
            return response.text()
        }).then((value) => {
            let valueJson = JSON.parse(value);
            if (valueJson["code"] == 1) {
                this.canRewardCount--;
                callBack && callBack();
            } else {
                callFail && callFail();
            }
        })
    }

    addPlayCount() {
        fetch("https://miniapi.maox.com.cn/api/index/counter", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({ user_id: window["loginData"].userId })
        }).then((response: Response) => {
            return response.text()
        }).then((value) => {
            let valueJson = JSON.parse(value);
            if (valueJson["code"] == 1) {

            } else {

            }
        })

    }

    // kongTou() {
    //     fetch("http://game.jiuqishujie.cn/api/app-order/addUnconditionalAirdropCollectiblesCopy/" + window["loginData"].userId, {
    //         method: "POST",
    //         headers: {
    //             "Content-Type": "application/json",
    //             "satoken": window["loginData"].satoken
    //         },
    //     }).then((response: Response) => {
    //         return response.text()
    //     }).then((value) => {
    //         let valueJson = JSON.parse(value);
    //         if (valueJson["code"] == 200) {

    //         } else {
    //             let options = {
    //                 title: "hint",
    //                 content: valueJson["data"]
    //             }
    //             EventManager.Instance.emit(Event_Name.UI_SHOW, options);
    //         }
    //     })
    // }

    changeScore(score, callBack, callFail = null) {
        fetch(`http://game.jiuqishujie.cn/api/mall-credits/reduceOrIncreasePoints/${window["loginData"].userId}/${score >= 0 ? 1 : 2}/${Math.abs(score)}`, {
            method: "POST",
            headers: {

                "Content-Type": "application/json",
                "satoken": window["loginData"].satoken
            },
        }).then((response: Response) => {
            return response.text()
        }).then((value) => {
            let valueJson = JSON.parse(value);
            if (valueJson["code"] == 200) {
                callBack && callBack();
            } else {
                callFail && callFail();
            }
        })
    }

    questServer(score, callBack, callFail = null) {
        fetch('https://multiplatform.maox.com.cn/api/a/add_score', {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                score: score.toString(),
                user_id: window["loginData"].userId,
                "token": window["loginData"].satoken,
            })
        }).then((response: Response) => {
            return response.text()
        }).then((value) => {

            let valueJson = JSON.parse(value);
            console.error(valueJson);
            if (valueJson["code"] == 200) {
                callBack && callBack();
            } else {
                callFail && callFail();
            }
        })
    }


    getUserSore(callBack, callFail = null) {
        fetch('https://multiplatform.maox.com.cn/api/a/get_user_today_score', {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                user_id: window["loginData"].userId,
            })
        }).then((response: Response) => {
            return response.text()
        }).then((value) => {
            let valueJson = JSON.parse(value);
            console.error(valueJson);
            if (valueJson["code"] == 1) {
                callBack && callBack(valueJson);
            } else {
                callFail && callFail();
            }
        })
    }

}
export const messManager = MessManager.getInstance();



