import { default as Gamecfg, default as gameCfg } from "../../common/gameCfg";
import { GuideDoGuide, GuideDoGuidePram, GuideSkipGuides, GuideSkipGuidesPram } from "../../common/Xyc";
import GameDataCenter from "../GameDataCenter";
import IDataModel from "../../frameWork/model/IDataModel";
import { SevBack } from "../../common/Xys";
import { gameMethod } from "../../common/gameMethod";
import EventMng from "../../manager/EventMng";
import { GuideEvent } from "../const/EventConst";
import { GuideType, OpenType } from "../const/TypeConst";

export default class GuideModel extends IDataModel {

    constructor() {
        super('guide');
        this.init()
    }

    isOpenGuide: boolean //是否开启引导
    isShowing: boolean
    guideId: string //引导Id
    guideIndex: number //引导索引
    finishEvents: { [id: string]: number } = {} //功能引导未完成重启后要从头开始引导，前端记录下事件
    isChargesListShow: boolean;//存主界面左边列表开启状态

    //初始化数据
    init() {
        this.isShowing = false
        this.guideId = ""
        this.guideIndex = 0
        this.isOpenGuide = true;
    }

    doSevback(result: SevBack): void {

    }

    doEvent(result: SevBack): void {

    }

    isFinishGuide(): boolean {
        if (!this.isOpenGuide) {
            return true
        }
        let actGuide = GameDataCenter.sevBack.actGuide
        return actGuide.a?.list?.["10027"] != null

    }

    //检测引导是否满足条件
    checkCanDrama(id: string): boolean {
        let conf = gameCfg.guideInfo.getItem(id)
        if (!gameMethod.isEmpty(conf)) {
            const element = conf.way
            switch (element) {
                case '1'://进入游戏

                    break
                case '2'://完成指定引导
                    if (!this.checkIsSave(conf.variable[0].toString())) {
                        return false
                    }
                    break
                case '3'://完成指定主线任务
                    // if (!GameDataCenter.task.TaskIdIsFinish(conf.variable[0].toString())) {
                    //     return false
                    // }
                    break
                case '4'://获得指定主线任务
                    let actTaskMain = GameDataCenter.sevBack.actTaskMain;
                    if (actTaskMain.id != conf.variable[0]) {
                        return false
                    }
                    break;
                case '5'://击杀第几章第几关的boss（需要在关闭结算界面后触发）
                    let actZhangjie = GameDataCenter.sevBack.actZhangjie?.a;
                    if (Number(actZhangjie.zjid) != conf.variable[0] && actZhangjie.bossxb != conf.variable[1]) {
                        return false
                    }
                    break
            }
        }
        return true
    }
    //检测当前任务引导的任务是否完成
    checkCurTaskGuide() {
        let conf = gameCfg.guideInfo.getItem(this.guideId)
        if (!gameMethod.isEmpty(conf)) {
            let way = conf.way
            if (way == '3' && GameDataCenter.task.TaskIdIsFinish(conf.variable[0].toString())) {
                return true
            } else {
                return false
            }
        }
        return false
    }

    //是否已保存步骤 10000 
    checkIsSave(id: string) {
        let idTemp = id
        if (idTemp.slice(0, 3) == "100") {
            //新手引导
            let actGuide = GameDataCenter.sevBack.actGuide
            return actGuide.a?.list[id] != null
        } else {
            //功能引导
            // return this.finishEvents[id] != null
            let actGuide = GameDataCenter.sevBack.actGuide
            return actGuide.a?.list[id] != null
        }
    }

    //检测当前引导类型是否全部完成
    checkGuideEventFinish(guideEvent: string) {
        let confDramaList = gameCfg.guideInfoList.getItemList(guideEvent)
        if (gameMethod.isEmpty(confDramaList)) {
            //没有该功能的引导。
            return false
        }
        for (const key in confDramaList) {
            const element = confDramaList[key];
            //新手引导如果保存了步骤要过滤
            if (!this.checkIsSave(element.id)) {
                return false
            }
        }
        return true
    }

    saveGuide(cb: Function = () => { }, canshu?: any[], failcb: Function = null) {
        this.finishEvents[this.guideId] = 1
        if (GameDataCenter.sevBack.actGuide.a?.list[this.guideId] != null) {
            cb()
            return
        }
        let param: GuideDoGuidePram = {
            id: this.guideId,
            canshu: canshu || []
        }

        this.send(GuideDoGuide.url, param, (result: SevBack) => {
            if (result.type == 1) {
                cb(result)
            } else {
                if (failcb) failcb()
            }
        }, null, 1, true)
    }

    //下一步引导 默认不传，传了就是跳转到该引导
    nextGuide(guideType: string, cb?: Function, guideId?: string, param?: any[]) {
        if (this.isShowing && this.isOpenGuide) {
            if (!gameMethod.isEmpty(guideId)) {
                this.guideId = guideId
            }
            this.saveGuide((result: SevBack) => {
                if (GameDataCenter.guide.guideId == "20906") {
                    console.log('这步引导，回调成功后，不马上进行下一步引导');
                } else {
                    EventMng.emit(GuideEvent.CLOSE_GUIDE)
                    EventMng.emit(GuideEvent.GUIDE_EVENT, guideType)
                }
                cb && cb(result);
            }, param)
        }
    }

    /**批量保存引导id 让服务端直接记录完成 用于批量跳过步骤*/
    saveGuideArray(guideType: string, guideIds?: string[], cb?: Function) {
        let param: GuideSkipGuidesPram = {
            ids: guideIds,
        }

        this.send(GuideSkipGuides.url, param, (result: SevBack) => {
            if (result.type == 1) {
                EventMng.emit(GuideEvent.CLOSE_GUIDE)
                EventMng.emit(GuideEvent.GUIDE_EVENT, guideType)
                cb && cb(result);
            }
        }, null, 1, true)
    }

    //下一步引导 默认不传，传了就是跳转到该引导
    nextGuideOpen(guideType: OpenType, cb?: Function, guideId?: string, param?: any[]) {
        this.nextGuide("evt_guide_" + guideType, cb, guideId, param)
    }

    /**判断引导是否触发 */
    checkNextGuide(type: OpenType) {
        return this.isShowing && GameDataCenter.page.checkOpen(type, false) && !this.checkGuideEventFinish("evt_guide_" + type) && this.checkIsCurrentGuide(type)
    }

    /**判断引导是否触发 */
    checkNextGuide2(event: GuideType) {
        return this.isShowing && !this.checkGuideEventFinish(event) && this.checkIsCurrentGuide(null, event);
    }

    //触发特殊引导
    checkOpenGuideSpec(guideId: string, noSave?: boolean) {
        //判断第一次关闭阵法，触发剧情引导
        let actGuide = GameDataCenter.sevBack.actGuide
        if (gameMethod.isEmpty(actGuide.a?.list[guideId])) {
            let conf = gameCfg.guideInfo.getItem(guideId)
            if (gameMethod.isEmpty(conf)) {
                console.error("找不到引导配置 id = ", guideId)
                return
            }
            this.guideId = guideId
            EventMng.emit(GuideEvent.GUIDE_EVENT, conf.event)
            if (!noSave)
                this.saveGuide()
        }
    }

    /**当前的guideId是否是该OpenType的引导*/
    checkIsCurrentGuide(openType: OpenType, guideEvent?: string): boolean {
        let guideList = gameCfg.guideInfoList.getItemList(gameMethod.isEmpty(guideEvent) ? "evt_guide_" + openType : guideEvent);
        for (let key in guideList) {
            if (guideList[key].id == this.guideId) {
                return true;
            }
        }


        return false;
    }
}
