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 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";


const { ccclass, property } = cc._decorator;

let ChadianConfig = {
    chadianLog: [
        {
            str: "为什么?凭什么?",
            qiPaoPos: 0,
            delay: 3,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/为什么?凭什么?"
        },
        {
            str: "凭什么?你穷,又失业,养得起我和孩子吗?",
            qiPaoPos: 1,
            delay: 4.7,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/凭什么?你穷,又失业,养得起我和孩子吗?"
        },
        {
            str: "我忍",
            qiPaoPos: 0,
            delay: 1.1,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/我忍"
        },
        {
            str: "这么漂亮,跟着你?真是暴殄天物!",
            qiPaoPos: 2,
            delay: 3.8,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/这么漂亮,跟着你?真是暴殄天物!"
        },
        {
            str: "我再忍",
            qiPaoPos: 0,
            delay: 1.7,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/我再忍"
        },
        {
            str: "滚吧,别碍着我和你老婆搞烛光晚餐",
            qiPaoPos: 2,
            delay: 3.5,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/滚吧,别碍着我和你老婆搞烛光晚餐"
        },
        {
            str: "不忍了,干个x的西餐,你俩给我等着...",
            qiPaoPos: 3,
            delay: 4.2,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/不忍了,干个x的西餐,你俩给我等着..."
        },
        {
            str: "忍个球,干个x的西餐,你俩给我等着...",
            qiPaoPos: 3,
            delay: 4.7,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/忍个球,干个x的西餐,你俩给我等着..."
        },
        {
            str: "老婆我们不幸福吗?",
            qiPaoPos: 4,
            delay: 1.6,
            effectUrl: "GameStoryRes/sound/KaiChangGuanQia1/老婆我们不幸福吗?"
        },
    ]
}

@ccclass
export default class KaiChangGuanQia1 extends cc.Component {


    // 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);
        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);
    }

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

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

        // this.scheduleOnce(() => {
        //     this.node.getChildByName('mainNode').getChildByName('tag我').active = false
        //     this.node.getChildByName('mainNode').getChildByName('tag老婆').active = false
        // }, 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) {

        let taiCiBtn = this.node.getChildByName('mainNode').getChildByName('按钮台词')
        switch (targetNode.name) {
            case "touchAreaLaoPo1":


                break;
            case "btnRight":
                taiCiBtn.active = true
                this.taiCiIndex = 1
                taiCiBtn.getChildByName('taiCi').getComponent(cc.Label).string = `${ChadianConfig.chadianLog[2].str}`
                this.guideStep(3);
                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_);
            let effect = node.getChildByName("声效").getChildByName("声效" + qiPaoPos_);
            qiPao.stopAllActions()
            this.showDialogStr(string_, qiPao.getChildByName("str"))
            // qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            effect.active = true
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(curLog.delay)
                .call(() => {
                    qiPao.active = false;
                    effect.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)
    }
    onTaiCiBtnClick0(event, custom) {
        Common5.ReportDY("inLevel", '开场剧情点位1');

        this.node.getChildByName('mainNode').getChildByName('按钮台词0').active = false
        
        let laoPo = this.node.getChildByName('mainNode').getChildByName('老婆')
        let linQi = this.node.getChildByName('mainNode').getChildByName('林齐')
        let taiCiBtn = this.node.getChildByName('mainNode').getChildByName('按钮台词')
        
        laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆林齐待机说话', true)

        this.showQiPao(ChadianConfig.chadianLog[8], () => {
            this.node.getChildByName('mainNode').getChildByName('tag我1').active = false
            

            laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆推林齐', false)
            Common5.playEffectCustom('KaiChangGuanQia1', 'sound/摔倒')
            this.scheduleOnce(() => {
                this.node.getChildByName('mainNode').getChildByName('tag我2').active = true
                laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆有开除信待机', true)
                linQi.active = true
                taiCiBtn.active = true
                this.taiCiIndex = 0
                taiCiBtn.getChildByName('taiCi').getComponent(cc.Label).string = `${ChadianConfig.chadianLog[0].str.slice(0,4)}...`
                this.guideStep(1);
            }, 2.2)
        });
    }
    onTaiCiBtnClick() {
        

        let taiCiBtn = this.node.getChildByName('mainNode').getChildByName('按钮台词')
        taiCiBtn.active = false

        let laoPo = this.node.getChildByName('mainNode').getChildByName('老婆')
        let linQi = this.node.getChildByName('mainNode').getChildByName('林齐')
        let laoBan = this.node.getChildByName('mainNode').getChildByName('老板')
        let beiMian = this.node.getChildByName('mainNode').getChildByName('背面')
        if (this.taiCiIndex == 0) {
            Common5.ReportDY("inLevel", '开场剧情点位2');

            linQi.getComponent(sp.Skeleton).setAnimation(0, '林齐倒地愤怒1说话', true)
            this.showQiPao(ChadianConfig.chadianLog[0], () => {
                laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆丢开除信', false)
                linQi.getComponent(sp.Skeleton).setAnimation(0, '林齐倒地愤怒1', true)
                this.scheduleOnce(() => {
                    laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆无开除信说话', true)
                    linQi.getComponent(sp.Skeleton).setAnimation(0, '林齐倒地愤怒1待机', true)
                }, 1)
                this.showQiPao(ChadianConfig.chadianLog[1], () => {
                    laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆无开除信待机', true)
                    // this.node.getChildByName('mainNode').getChildByName('touchAreaZhi').active = true
                    // this.guideStep(2);
                    cc.tween(this.node.getChildByName('zhiFangDaNode'))
                        .set({ active: true, scale: 0.1 })
                        .to(0.3, { x: 0, y: 0, scale: 1 })
                        .call(() => {
                            this.node.getChildByName('zhiFangDaNode').getChildByName('maskSp').active = true
                            this.node.getChildByName('zhiFangDaNode').getChildByName('btnRight').active = true
                        })
                        .start()
                });
            });
        } else if (this.taiCiIndex == 1) {
            Common5.ReportDY("inLevel", '开场剧情点位3');
            linQi.getComponent(sp.Skeleton).setAnimation(0, '林齐倒地愤怒2待机', true)
            this.showQiPao(ChadianConfig.chadianLog[2], () => {
                this.node.getChildByName('mainNode').getChildByName('taqg陈天霸').active = true
                laoBan.active = true
                laoBan.getComponent(sp.Skeleton).setAnimation(0, '老板出现', false)
                this.scheduleOnce(() => {
                    laoBan.getComponent(sp.Skeleton).setAnimation(0, '老板说话', true)
                    laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆老板搭肩待机', true)
                    this.showQiPao(ChadianConfig.chadianLog[3], () => {
                        laoBan.getComponent(sp.Skeleton).setAnimation(0, '老板待机', true)
                        taiCiBtn.active = true
                        this.taiCiIndex = 2
                        taiCiBtn.getChildByName('taiCi').getComponent(cc.Label).string = `${ChadianConfig.chadianLog[4].str}`
                        this.guideStep(4);
                    });
                }, 1)
            });
        } else if (this.taiCiIndex == 2) {
            Common5.ReportDY("inLevel", '开场剧情点位4');
            linQi.getComponent(sp.Skeleton).setAnimation(0, '林齐倒地愤怒3待机', true)
            this.showQiPao(ChadianConfig.chadianLog[4], () => {
                laoBan.getComponent(sp.Skeleton).setAnimation(0, '老板说话', true)
                this.showQiPao(ChadianConfig.chadianLog[5], () => {
                    this.node.getChildByName('mainNode').getChildByName('taqg陈天霸').active = false
                    this.node.getChildByName('mainNode').getChildByName('tag老婆').active = false
                    this.node.getChildByName('mainNode').getChildByName('tag我2').active = false

                    laoBan.getComponent(sp.Skeleton).setAnimation(0, '老板待机', true)
                    // laoBan.getComponent(sp.Skeleton).setAnimation(0, '老板消失', false)
                    // laoPo.getComponent(sp.Skeleton).setAnimation(0, '老婆消失', false)
                    laoBan.active = false
                    laoPo.active = false
                    beiMian.active = true
                    this.node.getChildByName('mainNode').getChildByName('按钮台词2').active = true
                    this.node.getChildByName('mainNode').getChildByName('按钮台词3').active = true
                });
            });
        }
    }

    onTaiCiBtnClick2(event, custom) {
        Common5.ReportDY("inLevel", '开场剧情点位5');
        this.node.getChildByName('mainNode').getChildByName('按钮台词2').active = false
        this.node.getChildByName('mainNode').getChildByName('按钮台词3').active = false

        let beiMian = this.node.getChildByName('mainNode').getChildByName('背面')
        let linQi = this.node.getChildByName('mainNode').getChildByName('林齐')
        linQi.active = false

        let linQiBaoFa = this.node.getChildByName('mainNode').getChildByName('林齐爆发')
        linQiBaoFa.active = true
        linQiBaoFa.getComponent(sp.Skeleton).setAnimation(0, '林齐爆发', false)
        Common5.playEffectCustom('KaiChangGuanQia1', 'sound/衣服炸开')
        this.scheduleOnce(() => {
            linQiBaoFa.getComponent(sp.Skeleton).setAnimation(0, '爆发待机', true)
            if (custom == 0) {
                this.showQiPao(ChadianConfig.chadianLog[6], () => {
                    beiMian.getComponent(sp.Skeleton).setAnimation(0, '离开', false)
                    this.showZhuanChang()
                });
            } else {
                this.showQiPao(ChadianConfig.chadianLog[7], () => {
                    beiMian.getComponent(sp.Skeleton).setAnimation(0, '离开', false)
                    this.showZhuanChang()
                });
            }

        }, 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.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_101)
            JuQingManager.unLockNewJuQing('WX_101')
            User.setFirstStepIndex(2)
            PrefabManage.loadPrefabByType(Common5.selectGameInfo.roomIndex)
            this.node.removeFromParent()
            this.node.destroy()
            // Common5.releaseBundleAssets('KaiChangGuanQia1')
        }, 6)
    }
}