import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import GameBaseScript from "../GameRoomRes/GameBaseScript";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import UserManager from "../Manager/UserManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import GetAward from "../SCommon/GetAward";
import DHBase from "./DHBase";
import selectButtons from "./selectButtons";
const { ccclass, property, disallowMultiple, menu } = cc._decorator;

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_3')
export default class DH_3 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_3/sound/我要的钱带来了没?', delayTime: 1.3, str: '我要的钱带来了没?', posi: 'qipao2' },
        { effectUrl: 'DH/DH_3/sound/2000万一分不少,把人放了。', delayTime: 2.58, str: '2000万一分不少,把人放了。', posi: '-1' },
        { effectUrl: 'DH/DH_3/sound/人就在里面,你自己进去带走吧', delayTime: 2.36, str: '人就在里面,你自己进去带走吧', posi: 'qipao2' },
        { effectUrl: 'DH/DH_3/sound/到底是谁指使你的?', delayTime: 1.8, str: '到底是谁指使你的?', posi: '-1' },
        { effectUrl: 'DH/DH_3/sound/哼,只怪你招惹了你惹不起的人!', delayTime: 2.81, str: '哼,只怪你招惹了你惹不起的人!', posi: 'qipao2' },
        { effectUrl: 'DH/DH_3/sound/萌萌,你怎么了!!', delayTime: 1.73, str: '萌萌,你怎么了!!', posi: '-1' },
    ]

    @property(sp.Skeleton)
    door: sp.Skeleton = null
    @property(sp.Skeleton)
    heishehui: sp.Skeleton = null

    @property(cc.ProgressBar)
    progress: cc.ProgressBar = null

    @property(cc.Node)
    m_bg: cc.Node = null
    @property(cc.Node)
    MoveNodes: cc.Node = null
    isTouch: any;

    start() {
        // Common5.playRemoteAudioMusic("remotesound/effect/打雷下雨声", true)
        Common5.preLoadRemoteAudioByChatConfig(this.chatConfig);
        super.start();
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if (mainId == MainTaskIdEnum.MainTask_605) {
            this.continueStory();
        } else {
            this.m_bg.getChildByName("背景1").active = true
            this.door.setAnimation(0, "有锁门", false)
            this.m_bg.getChildByName("砸").active = true
            this.heishehui.node.active = false
        }
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_39)
        // })
    }

    // 继续剧情
    continueStory() {
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_4)
        // })
        const dialogEvents = [
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.heishehui.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.heishehui.setAnimation(0, "待机", true)
                this.m_bg.getChildByName("needGoodNode_605").active = true
                // this.heishehui.setAnimation(0, "小弟待机", true)
                // this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                //     // this.nanzhu.setAnimation(0, "侧头笑说话", true)
                // });
            },
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {

                    // PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                    //     let goods = [{ goodId: 1733, goodNum: 1 }];
                    //     prefabNode.getComponent(GetAward).initView(goods, () => {
                    //         // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_530)
                    //         // User.setRoomGoodStatus("DH_39")
                    //         // UserManager.clearAndaddStructureFlash("cf_FengRenYuan")
                    //     });
                    // });
                    // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_202)
                    // TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_203)
                    // this.node.destroy()
                }
            });
        }
        dialogCallFunc(dialogEvents);
    }

    guideView(nodeArray, opacity = -1) {
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, this.node, (prefab) => {
            // prefab.zIndex = 199
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            if (opacity != -1) {
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)
            }
        })
    }

    onTouch() {
        this.m_bg.getChildByName("needGoodNode_605").active = false
        this.showQiPao(this.chatConfig[this.dialogIndex++], () => {
            this.showQiPao(this.chatConfig[this.dialogIndex++], () => {
                this.showClickDialog(this.chatConfig[this.dialogIndex++], () => {
                    this.showQiPao(this.chatConfig[this.dialogIndex++], () => {
                        cc.tween(this.heishehui.node)
                            .to(0.5, { opacity: 0 })
                            .start()
                        UserManager.subMoney(20000000)
                        this.m_bg.getChildByName("背景1").active = true
                        this.door.setAnimation(0, "有锁门", false)
                        this.m_bg.getChildByName("砸").active = true
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_605)
                        TaskManager.setCurTask(MainTaskIdEnum.MainTask_606)
                    }, () => {
                        this.heishehui.setAnimation(0, "说话", true)
                    });
                }, () => {
                    this.heishehui.setAnimation(0, "说话", true)
                });
            }, () => {
                this.heishehui.setAnimation(0, "说话", true)
            });
        }, () => {
        });
    }

    onTouchZaSuo() {
        this.progress.progress += 0.1
        this.door.setAnimation(0, "砸锁", true)
        if (this.progress.progress >= 1) {
            this.m_bg.getChildByName("砸").active = false
            this.node.getChildByName("推开").active = true
            this.door.setAnimation(0, "没锁门", false)
        }

        if (!this.isTouch) {
            this.isTouch = true
            this.schedule(() => {
                let jindu = this.progress.progress - 0.1
                if (jindu <= 0) {
                    jindu = 0
                }
                this.progress.progress = jindu
            }, 1)
        }

    }

    onTouchTuiKai() {
        this.node.getChildByName("推开").active = false
        this.door.setAnimation(0, "打开门", false)
        Common5.playRemoteAudioEffect('effect/铁门打开')
        this.door.setCompleteListener(() => {
            this.door.setCompleteListener(null)
            this.door.node.active = false
            Common5.playRemoteAudioMusic("DH/DH_3/sound/打开后女孩晕倒的场景背景音", true)
            this.runBg(1, 2, this.MoveNodes.getChildByName("女儿"), () => {
                this.showQiPao(this.chatConfig[5], () => {
                    User.setStatistics("zhangJie", 6)
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_606)
                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_701)

                    this.node.getChildByName("前往医院").active = true
                }, () => {
                    // this.heishehui.setAnimation(0, "小弟说话", true)
                });
            })
        })
    }

    onTouchGoYiYuan() {
        PrefabManage.showBlackGuoDu(() => {
            PrefabManage.loadPrefabByType(GameType.DH_4)
            this.node.removeFromParent()
            this.node.destroy()
        })
    }

    runBg(time, scale, endnode, callFunc) {
        cc.Tween.stopAllByTarget(this.m_bg)
        cc.tween(this.m_bg)
            .to(time, { scale: scale })
            .start()
        cc.tween(this.m_bg)
            .sequence(
                cc.tween().to(time, { position: cc.v2(-endnode.position.x * scale, -endnode.position.y * scale) }),
                cc.tween().call(() => {
                    callFunc()
                })
            )
            .start()
    }
}