import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import CommonDefine from "../../Platform/th/CommonDefine";
import { GameStoryConfigData } from "../FirstSelectScene/GameStoryConfig";
import GameBaseScript from "../GameRoomRes/GameBaseScript";
import JuQingManager from "../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import JuQingQiPao from "../JuQingGuanQia/JuQingQiPao";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import Game from "../WenZiRes/ui/Game";


const { ccclass, property } = cc._decorator;

let ChadianConfig = {
    chadianLog: [
        {
            str: "奇怪,老婆来酒店干嘛?",
            qiPaoPos: 0,
            delay: 2.5,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/奇怪,老婆来酒店干嘛?"
        },
        {
            str: "陈老板,你答应的50万...",
            qiPaoPos: 1,
            delay: 2.5,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/陈老板,你答应的50万..."
        },

        {
            str: "林齐!你...你怎么来了!",
            qiPaoPos: 2,
            delay: 4.3,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/林齐!你...你怎么来了!"
        },
        {
            str: "你们!怎么会在酒店!",
            qiPaoPos: 3,
            delay: 2.8,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/你们!怎么会在酒店!"
        },
        {
            str: "你个窝囊废你瞎嚷嚷什么啊!",
            qiPaoPos: 4,
            delay: 2.8,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/你个窝囊废你瞎嚷嚷什么啊!"
        },
        {
            str: "你个臭卖包子的,也敢推老子?",
            qiPaoPos: 5,
            delay: 3.5,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/你个臭卖包子的,也敢推老子?"
        },
        {
            str: "你**敢对老子动手?你找死是吧!",
            qiPaoPos: 6,
            delay: 3.7,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/你**敢对老子动手?你找死是吧!"
        },
        {
            str: "陈天霸你给我等着,一年内我让你西餐厅破产!",
            qiPaoPos: 3,
            delay: 4.5,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/陈天霸你给我等着,一年内我让你西餐厅破产!"
        },
        {
            str: "林齐!我也是为了女儿,你以后会明白我的苦衷。",
            qiPaoPos: 2,
            delay: 5.5,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/林齐!我也是为了女儿,你以后会明白我的苦衷。"
        },
        {
            str: "今天这事没完,我看你有什么本事让我西餐厅破产。",
            qiPaoPos: 6,
            delay: 5.5,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1Gai/今天这事没完,我看你有什么本事让我西餐厅破产。"
        },
    ]
}

@ccclass
export default class KaiChangGuanQia1 extends cc.Component {
    mainNode: cc.Node = null
    scene1: cc.Node = null
    nanZhu1: cc.Node = null
    dianJiKaiMen: cc.Node = null
    kaiMenTouchArea: cc.Node = null
    kaiMenSpine: cc.Node = null
    touTingZiSpine: cc.Node = null

    scene2: cc.Node = null
    laoPo: cc.Node = null
    chenTianBa: cc.Node = null
    nanZhu2: cc.Node = null
    nanZhu3: cc.Node = null
    dianJiShuoHua: cc.Node = null
    djshTouchArea1: cc.Node = null
    dianJiTuiKai: cc.Node = null
    djtkTouchArea1: cc.Node = null
    dianJiFuChou: cc.Node = null
    djfcTouchArea1: cc.Node = null
    shanBaZhangSpine: cc.Node = null
    tag_wo: cc.Node = null
    tag_laoPo: cc.Node = null
    tag_chenTianBa: cc.Node = null

    // LIFE-CYCLE CALLBACKS:
    taiCiIndex: number = 0
    maskGuideNode: cc.Node = null

    onLoad() {
        Common5.playRemoteAudioMusic('CommonSound/西餐厅外')

        GameBaseScript.preLoadRemoteAudio(ChadianConfig);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        this.mainNode = this.node.getChildByName('mainNode')
        this.scene1 = this.mainNode.getChildByName('scene1')
        this.nanZhu1 = this.scene1.getChildByName('男主1')
        this.dianJiKaiMen = this.scene1.getChildByName('点击开门')
        this.kaiMenTouchArea = this.scene1.getChildByName('kaiMenTouchArea')
        this.kaiMenSpine = this.scene1.getChildByName('开门动效')
        this.touTingZiSpine = this.scene1.getChildByName('偷听字')

        this.scene2 = this.mainNode.getChildByName('scene2')
        this.laoPo = this.scene2.getChildByName('老婆')
        this.chenTianBa = this.scene2.getChildByName('陈天霸')
        this.nanZhu2 = this.scene2.getChildByName('男主2')
        this.nanZhu3 = this.scene2.getChildByName('男主3')
        this.dianJiShuoHua = this.scene2.getChildByName('点击说话')
        this.djshTouchArea1 = this.scene2.getChildByName('djshTouchArea1')
        this.dianJiTuiKai = this.scene2.getChildByName('点击推开')
        this.djtkTouchArea1 = this.scene2.getChildByName('djtkTouchArea1')
        this.dianJiFuChou = this.scene2.getChildByName('点击复仇')
        this.djfcTouchArea1 = this.scene2.getChildByName('djfcTouchArea1')
        this.shanBaZhangSpine = this.scene2.getChildByName('扇巴掌特写')
        this.tag_wo = this.scene2.getChildByName('tag我')
        this.tag_laoPo = this.scene2.getChildByName('tag老婆')
        this.tag_chenTianBa = this.scene2.getChildByName('taqg陈天霸')

    }

    start() {
        let comSuccesscall = (GameData) => {
            this.scheduleOnce(() => {
                console.warn('邵阳再次load++==FirstSelectScene', GameData)
                if (GameData != null && GameData != '') {
                    cc.director.loadScene("FirstSelectScene");
                }
            }, 0.5)
        }
        CommonDefine.getServerData(comSuccesscall)


        this.scheduleOnce(() => {
            let zhangJie = User.getStatistics("zhangJie")
            PrefabManage.preloadPrefabByType(GameStoryConfigData[zhangJie].roomIndex);
        });
        // this.node.getChildByName('mainNode').getChildByName('按钮台词0').getChildByName('taiCi').getComponent(cc.Label).string = `${ChadianConfig.chadianLog[8].str.slice(0,4)}...`
        // this.guideStep(-1);
        this.showScene(0)


    }
    showScene(index) {
        if (index == 0) {
            this.scene1.active = true
            this.scene2.active = false
            this.nanZhu1.getComponent(sp.Skeleton).setAnimation(0, '偷听1', true)
            this.scheduleOnce(() => {
                this.showQiPao(ChadianConfig.chadianLog[0], () => {
                    this.showQiPao(ChadianConfig.chadianLog[1], () => {

                        this.nanZhu1.getComponent(sp.Skeleton).setAnimation(0, '震惊', false)
                        this.scheduleOnce(() => {
                            this.nanZhu1.getComponent(sp.Skeleton).setAnimation(0, '偷听2', true)
                        }, 0.67)

                        this.touTingZiSpine.active = false
                        this.dianJiKaiMen.active = true
                        this.kaiMenTouchArea.active = true
                    });
                });
            }, 1.5)
        } else if (index == 1) {
            this.scene1.active = false
            this.scene2.active = true
            this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '男主待机冒火', true)
            cc.tween(this.nanZhu2)
                .set({ x: -500 })
                .to(0.8, { x: -190 })
                .start()

            this.chenTianBa.getComponent(sp.Skeleton).setAnimation(0, '老板待机', true)
            this.laoPo.getComponent(sp.Skeleton).setAnimation(0, '待机', true)
            this.scheduleOnce(() => {
                let part1 = () => {
                    cc.tween(this.mainNode)
                        .to(0.6, { scale: 1.5, x: 130 })
                        .call(() => {
                            this.laoPo.getComponent(sp.Skeleton).setAnimation(0, '震惊说话', true)
                            this.showQiPao(ChadianConfig.chadianLog[2], () => {
                                this.laoPo.getComponent(sp.Skeleton).setAnimation(0, '待机', true)

                                this.scheduleOnce(() => {
                                    part2()
                                }, 0.2)
                            });
                        })
                        .start()
                }

                let part2 = () => {
                    // cc.tween(this.mainNode)
                    //     .to(0.6,{scale:1,x:0})
                    //     .call(()=>{
                    this.dianJiShuoHua.active = true
                    this.djshTouchArea1.active = true
                    //     })
                    // .start()
                }
                part1()
            }, 1.5)
        }
    }

    // guideStep(index) {
    //     if (index == -1) {
    //         let node = this.node.getChildByName("mainNode").getChildByName('按钮台词0')
    //         this.guideView([node])
    //     }else if (index == 0) {
    //         let node = this.node.getChildByName('mainNode').getChildByName('touchAreaLaoPo1')
    //         this.guideView([node])
    //     } else if (index == 1) {
    //         let node = this.node.getChildByName('mainNode').getChildByName('按钮台词')
    //         this.guideView([node])
    //     } else if (index == 2) {
    //         let node = this.node.getChildByName('mainNode').getChildByName('touchAreaZhi')
    //         this.guideView([node])
    //     } else if (index == 3) {
    //         let node = this.node.getChildByName('mainNode').getChildByName('按钮台词')
    //         this.guideView([node])
    //     } else if (index == 4) {
    //         let node = this.node.getChildByName('mainNode').getChildByName('按钮台词')
    //         this.guideView([node])
    //     }
    // }

    normalTouchCallback(targetNode) {

        switch (targetNode.name) {
            case "kaiMenTouchArea":

                Common5.ReportDY("inLevel", '开场剧情点位1-点击开门');
                // this.nanZhu1.active = false
                this.kaiMenSpine.getComponent(sp.Skeleton).setAnimation(0, '开门', false)
                Common5.playEffectCustom('KaiChangGuanQia1Gai', 'sound/开门')

                cc.tween(this.nanZhu1)
                    .to(1, { opacity: 0 })
                    .call(() => {

                    })
                    .start()

                this.scheduleOnce(() => {
                    this.showScene(1)
                }, 4)
                break;
            case "djshTouchArea1":
                Common5.ReportDY("inLevel", '开场剧情点位2-点击说话');
                cc.tween(this.mainNode)
                    .to(0.6, { scale: 1, x: 0 })
                    .call(() => {
                        this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '男主指人说话', true)
                        this.showQiPao(ChadianConfig.chadianLog[3], () => {
                            this.tag_wo.active = false
                            this.tag_laoPo.active = false
                            this.tag_chenTianBa.active = false
                            this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '老板走向男主点胸', false)
                            this.chenTianBa.active = false
                            cc.tween(this.mainNode)
                                .to(0.6, { scale: 1.5, x: 210, y: 200 })
                                .start()
                            this.scheduleOnce(() => {
                                this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '老板点胸男主说话', true)
                                this.showQiPao(ChadianConfig.chadianLog[4], () => {
                                    this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '老板点胸男主待机', true)
                                    // cc.tween(this.mainNode)
                                    //     .to(0.6,{scale:1,x:0,y:0})
                                    //     .call(()=>{
                                    this.dianJiTuiKai.active = true
                                    this.djtkTouchArea1.active = true
                                    //     })
                                    // .start()
                                });
                            }, 2.2)
                        });
                    })
                    .start()


                break;
            case "djtkTouchArea1":
                Common5.ReportDY("inLevel", '开场剧情点位3-点击推开');
                this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '男主推开老板', false)
                this.scheduleOnce(() => {
                    this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '男主推开老板待机', true)
                }, 0.8)

                cc.tween(this.mainNode)
                    .to(0.6, { scale: 1, x: 0, y: 0 })
                    .delay(0.3)
                    .call(() => {
                        Common5.playEffectCustom('KaiChangGuanQia1Gai', 'sound/打耳光')
                        this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '老板扇男主', false)
                        this.scheduleOnce(() => {
                            this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '老板扇男主说话', true)
                            this.nanZhu3.active = true
                            this.nanZhu3.getComponent(sp.Skeleton).setAnimation(0, '男主倒地愤怒', true)
                            this.showQiPao(ChadianConfig.chadianLog[5], () => {
                                this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '老板扇男主待机', true)
                                this.dianJiFuChou.active = true
                                this.djfcTouchArea1.active = true
                            });
                        }, 0.8)
                    })
                    .start()

                break;
            case "djfcTouchArea1":
                Common5.ReportDY("inLevel", '开场剧情点位4-点击复仇');
                this.shanBaZhangSpine.active = true
                this.shanBaZhangSpine.getComponent(sp.Skeleton).setAnimation(0, 'animation', false)
                Common5.playEffectCustom('KaiChangGuanQia1Gai', 'sound/打耳光')
                this.nanZhu2.active = false
                this.nanZhu3.active = false
                this.scheduleOnce(() => {
                    this.shanBaZhangSpine.active = false
                    this.nanZhu2.active = true
                    this.nanZhu3.active = true
                    this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '男主待机冒火', true)
                    this.nanZhu3.x = -290
                    this.nanZhu3.getComponent(sp.Skeleton).setAnimation(0, '老板倒地说话', true)
                    this.showQiPao(ChadianConfig.chadianLog[6], () => {
                        this.nanZhu3.getComponent(sp.Skeleton).setAnimation(0, '老板倒地待机', true)
                        this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '男主指人说话', true)
                        this.showQiPao(ChadianConfig.chadianLog[7], () => {
                            this.nanZhu2.getComponent(sp.Skeleton).setAnimation(0, '男主待机冒火', true)
                            let part2 = () => {
                                this.laoPo.getComponent(sp.Skeleton).setAnimation(0, '流泪待机', true)
                                cc.tween(this.mainNode)
                                    .to(0.6, { scale: 1, x: 0 })
                                    .delay(0.3)
                                    .call(() => {
                                        cc.tween(this.laoPo)
                                            .to(1, { opacity: 0 })
                                            .start()
                                        cc.tween(this.nanZhu3)
                                            .to(1, { opacity: 0 })
                                            .start()
                                    })
                                    .delay(1.1)
                                    .call(() => {
                                        this.showZhuanChang()
                                    })
                                    .start()
                            }
                            cc.tween(this.mainNode)
                                // .to(0.6, { scale: 1.5, x: 130 })
                                .call(() => {
                                    this.laoPo.getComponent(sp.Skeleton).setAnimation(0, '说话', true)
                                    this.showQiPao(ChadianConfig.chadianLog[8], () => {
                                        this.laoPo.getComponent(sp.Skeleton).setAnimation(0, '流泪', false)
                                        this.nanZhu3.getComponent(sp.Skeleton).setAnimation(0, '老板倒地说话', true)
                                        this.showQiPao(ChadianConfig.chadianLog[9], () => {
                                            this.laoPo.getComponent(sp.Skeleton).setAnimation(0, '流泪', false)

                                            this.scheduleOnce(() => {
                                                part2()
                                            }, 2)
                                        });
                                    });
                                })
                                .start()
                        });
                    });
                }, 2)
                break;
            default:
                break;
        }
    }

    guideView(nodeArray) {
        if (this.maskGuideNode == null) {
            PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab) => {
                let guideNodeArray = nodeArray
                this.maskGuideNode = prefab
                let firstNode = guideNodeArray.shift()
                prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            })
        } else {
            this.maskGuideNode.active = true
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            this.maskGuideNode.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
        }
    }

    //展示气泡
    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)
    }

    //转场
    showZhuanChang() {
        // cc.tween(this.node.getChildByName('mainNode'))
        //     .to(0.2, { scale: 2 })
        //     .delay(1.5)
        //     .to(0.3, { scale: 0.9 })
        //     .start();
        // let linQiBaoFa = this.node.getChildByName('mainNode').getChildByName('林齐爆发')
        // linQiBaoFa.getComponent(sp.Skeleton).setAnimation(0, '消失', false)
        User.setFirstStepIndex(1)
        // this.scheduleOnce(() => {
        TaskManager.setCurTask(MainTaskIdEnum.MainTask_101)
        JuQingManager.unLockNewJuQing('WX_1')
        User.setFirstStepIndex(2)
        let zhangJie = User.getStatistics("zhangJie")
        PrefabManage.loadPrefabByType(GameStoryConfigData[zhangJie].roomIndex);
        // PrefabManage.loadPrefabByType(Common5.selectGameInfo.roomIndex)
        this.node.removeFromParent()
        this.node.destroy()
        // Common5.releaseBundleAssets('KaiChangGuanQia1')
        // }, 1)
    }
}