import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import AppPlatform from "../../../FrameWork/Util/AppPlatform";
import Common5 from "../../../Platform/th/Common5";
import JuQingManager from "../../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../../JuQingChat/TaskManager";
import JuQingQiPao from "../../JuQingGuanQia/JuQingQiPao";
import BagManager from "../../Manager/BagManager";
import UserManager from "../../Manager/UserManager";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";
import GetAward from "../../SCommon/GetAward";
import GameBaseScript from "../GameBaseScript";

let gameConfig = {
    chadianLog1: [
        {
            str: "你们这是什么情况",
            qiPaoPos: -1,
            delay: 1.4,
            effectUrl: "GameRoomRes/sound/Room14/你们这是什么情况"
        },
        {
            str: "究竟是怎么一回事?",
            qiPaoPos: -1,
            delay: 1.5,
            effectUrl: "GameRoomRes/sound/Room14/究竟是怎么一回事?"
        },
        {
            str: "我不知道,他就冲进来扯我的衣服",
            qiPaoPos: 0,
            delay: 5,
            effectUrl: "GameRoomRes/sound/Room14/我不知道,他就冲进来扯我的衣服"
        },
        {
            str: "好,你先穿好衣服,我找他去",
            qiPaoPos: -1,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room14/好,你先穿好衣服,我找他去"
        },
        {
            str: "陈总,你安排的事我都完成了",
            qiPaoPos: 2,
            delay: 2.8,
            effectUrl: "GameRoomRes/sound/Room14/陈总,你安排的事我都完成了"
        },
        {
            str: "刘伟是浙菜传承人?隐藏的传承人还不少",
            qiPaoPos: 2,
            delay: 4.6,
            effectUrl: "GameRoomRes/sound/Room14/刘伟是浙菜传承人?隐藏的传承人还不少"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room14 extends cc.Component {

    // goOutTimer: number = 999;
    // installTimer: number = 999;

    onLoad() {
        GameBaseScript.preLoadRemoteAudio(gameConfig);
        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);
        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);
    }

    start() {
        this.showChatBtnStatus();
        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua, () => {
            this.showChatBtnStatus();
        }, this);
    }

    update(dt: number) {
        // if (this.goOutTimer != 999) {
        //     this.goOutTimer += dt;
        //     let progress = this.node.getChildByName('progressBar1').getComponent(cc.ProgressBar);
        //     progress.progress = this.goOutTimer / 7;
        //     if (this.goOutTimer >= 7) {
        //         this.goOutTimer = 999;
        //         progress.node.active = false;
        //         if (this.installTimer != 999) {
        //             this.installTimer = 999;
        //             this.node.getChildByName('progressBar2').active = false;
        //             this.node.getChildByName('秘书站').stopAllActions();
        //             this.node.getChildByName('温馨提示').active = true;
        //         }
        //     }
        // }
        // if (this.installTimer != 999) {
        //     this.installTimer += dt;
        //     let progress = this.node.getChildByName('progressBar2').getComponent(cc.ProgressBar);
        //     progress.progress = this.installTimer / 5;
        //     if (this.installTimer >= 5) {
        //         this.installTimer = 999;
        //         progress.node.active = false;
        //         this.success();
        //     }
        // }
    }

    showChatBtnStatus() {
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo();
        let mainId = mainTaskInfo.Id;
        if (mainId == MainTaskIdEnum.MainTask_1401) {
            this.node.getChildByName('发小').active = true;
            this.node.getChildByName('秘书').active = true;
            this.node.getChildByName('nameplate').active = true;
            this.showQiPao(gameConfig.chadianLog1[0], () => {
                this.node.getChildByName('还原事件真相').active = true;
            });
        } else if (mainId == MainTaskIdEnum.MainTask_1402) {
            this.node.getChildByName('发小').active = false;
            this.node.getChildByName('还原事件真相').active = false;
            let sec = this.node.getChildByName('秘书');
            sec.active = true;
            const dialogEvents = [
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[1], func);
                },
                (func) => {
                    sec.getComponent(sp.Skeleton).setAnimation(0, "说话", true);
                    this.showQiPao(gameConfig.chadianLog1[2], func);
                },
                (func) => {
                    sec.getComponent(sp.Skeleton).setAnimation(0, "待机", true);
                    this.showQiPao(gameConfig.chadianLog1[3], func);
                }
            ]
            const dialogCallFunc = (logEvents: any[]) => {
                logEvents.shift()(() => {
                    if (logEvents.length > 0) {
                        dialogCallFunc(logEvents);
                    } else {
                        this.node.getChildByName('秘书').active = false;
                        this.node.getChildByName('秘书站').active = true;
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1402);
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1403);
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        } else if (mainId == MainTaskIdEnum.MainTask_1405) {
            this.node.getChildByName('安装摄像头').active = true;
            //this.node.getChildByName('安装提示').active = true;
            //let sec = this.node.getChildByName('秘书站');
            // cc.tween(sec)
            //     .set({ active: true, opacity: 255 })
            //     .delay(3)
            //     .to(1, { opacity: 0 })
            //     .call(() => {
            //         this.goOutTimer = 0;
            //         this.node.getChildByName('progressBar1').active = true;
            //     })
            //     .delay(7)
            //     .to(1, { opacity: 255 })
            //     .union()
            //     .repeatForever()
            //     .start();
        } else if (mainId == MainTaskIdEnum.MainTask_1406) {
            this.node.getChildByName('摄像头').active = true;
            this.node.getChildByName('查看监控').active = true;
        } else {
            this.node.getChildByName('秘书站').active = true;
        }
    }

    success() {
        // this.node.getChildByName('秘书站').stopAllActions();
        // this.node.getChildByName('秘书站').active = false;
        // this.node.getChildByName('progressBar1').active = false;
        // this.node.getChildByName('progressBar2').active = false;
        this.node.getChildByName('摄像头').active = true;
        this.node.getChildByName('查看监控').active = true;
        // this.goOutTimer = 999;
        // this.installTimer = 999;
        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1405);
        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1406);
        EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "查看摄像头":
                this.node.getChildByName('摄像界面').active = true;
                const dialogEvents = [
                    (func) => {
                        this.showQiPao(gameConfig.chadianLog1[4], func);
                    },
                    (func) => {
                        this.showQiPao(gameConfig.chadianLog1[5], func);
                    }
                ]
                const dialogCallFunc = (logEvents: any[]) => {
                    logEvents.shift()(() => {
                        if (logEvents.length > 0) {
                            dialogCallFunc(logEvents);
                        } else {
                            this.node.getChildByName('摄像界面').active = false;
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1406);
                            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1407);
                        }
                    });
                }
                this.scheduleOnce(()=>{
                    dialogCallFunc(dialogEvents);
                },4.67)
                
                break;
            default:
                break;
        }
    }

    //展示气泡
    showQiPao(curLog, func?) {
        console.log("curLog==", curLog)
        if (!curLog) {
            console.log("xxxxxx")
            return;
        }
        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos

        if (qiPaoPos_ != -1) {
            if (curLog.effectUrl && curLog.effectUrl.length > 0) {
                Common5.playRemoteAudioEffect(curLog.effectUrl);
            }

            let node = this.node
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            this.showDialogStr(string_, qiPao.getChildByName("str"))
            // qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(curLog.delay)
                .call(() => {
                    qiPao.active = false;
                    if (func) {
                        func();
                    }
                })
                .start();
        }
        else {
            let qiPao = this.node.getChildByName("dh_qiPao")
            qiPao.getComponent(JuQingQiPao).initView(curLog, func)
        }
    }

    showDialogStr(str, dialogStr, finishFunc?) {
        let curStr = ""
        let curIndex = 0
        let callFunc = () => {
            curStr += str[curIndex++]
            dialogStr.getComponent(cc.Label).string = curStr
            if (curIndex >= str.length) {
                finishFunc && finishFunc()
            }
        }
        this.schedule(callFunc, 0.07, str.length - 1)
    }

    onStartGameBtn1(event) {
        // let target = event.target;
        // target.active = false;
        console.log('游戏文字剧情14-查询真相');
        PrefabManage.loadPrefabByType(GameType.GameStory14);
    }

    onStartGameBtn2(event) {
        let target = event.target;
        target.active = false;
        this.success();
        // this.node.getChildByName('安装提示').active = false;
        // if (this.goOutTimer == 999) {
        //     this.node.getChildByName('秘书站').stopAllActions();
        //     this.node.getChildByName('温馨提示').active = true;
        // } else {
        //     this.installTimer = 0;
        //     this.node.getChildByName('progressBar2').active = true;
        // }
    }

    onStartGameBtn3(event) {
        let target = event.target;
        target.active = false;
        this.node.getChildByName('摄像界面').active = true;
        const dialogEvents = [
            (func) => {
                this.showQiPao(gameConfig.chadianLog1[4], func);
            },
            (func) => {
                this.showQiPao(gameConfig.chadianLog1[5], func);
            }
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    this.node.getChildByName('摄像界面').active = false;
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1406);
                    TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1407);
                }
            });
        }
        this.scheduleOnce(()=>{
            dialogCallFunc(dialogEvents);
        },4.67)
    }

    // 重来
    onRestartGameBtn(event) {
        this.node.getChildByName('温馨提示').active = false;
        // this.goOutTimer = 999;
        // this.installTimer = 999;
        this.node.getChildByName('progressBar1').active = false;
        this.node.getChildByName('progressBar2').active = false;
        EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
    }

    // 直接完成
    onSkipGameBtn(event) {
        this.node.getChildByName('温馨提示').active = false;
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    Common5.ReportDY("inLevel", "第14章-AD-直接完成");
                    this.success();
                }
                else {
                    Common5.showTips_custom("广告未观看完");
                }
            },
            onFailed: () => { }
        }
        AppPlatform.playVideo_custom(tab);
    }
}