
import Gamecfg from "../../common/gameCfg";
import { gameMethod } from "../../common/gameMethod";
import { KindItem, SevBack } from "../../common/Xys";
import Config from "../../Config";
import FguiMgr from "../../frameWork/fgui/FguiMgr";
import GameController from "../../GameController";
import UIHelp, { DialogParams } from "../../logic/ui/UIHelp";
import { I18n } from "../../utils/I18nUtil";
import { InGame, ItemKind } from "../const/TypeConst";
import GameDataCenter from "../GameDataCenter";
import IDataModel from "../../frameWork/model/IDataModel";


export default class WindowModel extends IDataModel {
    constructor() {
        super('window');
    }

    popItem: cc.Prefab = null
    tipsItem: cc.Prefab = null

    popItemList: KindItem[] = []
    isPoping: boolean = false //还在飘窗中

    rewardViewCloseCallbacks: { cb: Function, doNext: boolean }[] = [] // 奖励界面关闭后触发, doNext:立刻触发下一个cb
    doSevback(result: SevBack): void {

    }
    doEvent(result: SevBack): void {
        if (result.win) {
            if (result.win.items && Config.inGame >= InGame.home) {//招募界面开启时不用弹
                this.checkRewardItems(result.win.items)
            }
            if (result.win.msg) {
                // 兼容一下新旧版本
                if (typeof result.win.msg == "string") {
                    UIHelp.ShowTips(result.win.msg)
                } else if (Array.isArray(result.win.msg)) {
                    result.win.msg.forEach((element, index) => {
                        setTimeout(() => {
                            UIHelp.ShowTips(element)
                        }, index * 300);
                    });
                }
            }
            // 被提示被踢出
            if (result.win.msgOut) {
                GameController.network.stopRequest = true
                let data: DialogParams = {
                    title: I18n.getI18nText('common_dialogtitle_1'),
                    content: result.win.msgOut,
                    onlyConfirm: true,
                    cbConfirm() {
                        GameDataCenter.plat.instance.restart()
                    },
                }
                UIHelp.ShowSystemDialog(data)
            }

        }
    }

    checkRewardItems(items: KindItem[]) {
        let rewardItems: KindItem[] = []
        for (let i = 0; i < items.length; i++) {
            let item = items[i]
            rewardItems.push(item)
        }
        // //地宫自动模式屏蔽奖励弹窗
        // if (rewardItems.length > 0) {
        //     //获得新弟子单独弹出获得弟子弹框
        //     let dzList: KindItem[] = [];
        //     let zfList: KindItem[] = [];
        //     FguiMgr.Instance.openUI(RewardViewView, ViewZorder.REWARD, () => { }, {
        //         items: rewardItems, closeBack: (items: KindItem[]) => {
        //             if (zfList.length > 0) {
        //                 let zfBase = Gamecfg.zfxtZfbase.getItem(String(zfList[0][1]));
        //                 let pram = {
        //                     xls: zfBase,
        //                     type: 0
        //                 }
        //                 FguiMgr.Instance.openUI(ZhenFaShengJiTuPoSuccessView, ViewZorder.POP, null, pram);
        //             } else if (!gameMethod.isEmpty(dzList)) {
        //                 FguiMgr.Instance.openUI(HuoDeDiZiView, ViewZorder.REWARD, null, { dzid: null, rwdList: dzList });
        //             }
        //         }
        //     });
        //     rewardItems.forEach(element => {
        //         if (element[0] == ItemKind.dizi) {
        //             dzList.push(element);
        //         } else if (element[0] == ItemKind.zf) {
        //             zfList.push(element);
        //         }
        //     });

        // }

    }

    checkRewardViewCloseCallbacks() {
        if (this.rewardViewCloseCallbacks.length > 0) {
            let param = this.rewardViewCloseCallbacks.splice(0, 1)[0]
            param.cb()
            if (param.doNext) {
                this.checkRewardViewCloseCallbacks()
            }
        }
    }
}