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";
import taskState from "./taskState";
const { ccclass, property, disallowMultiple, menu } = cc._decorator;

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_37')
export default class DH_37 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_37/sound/汉升哥哥,这个老女人是谁啊?', delayTime: 2.89, str: '汉升哥哥,这个老女人是谁啊?', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/什么,她也是你女朋友?那我算什么?', delayTime: 4.04, str: '什么,她也是你女朋友?那我算什么?', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/汉升哥哥,那你怎么把人带回家里来了啊。', delayTime: 3.38, str: '汉升哥哥,那你怎么把人带回家里来了啊。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/你骗鬼呢,合伙人带着行李来家里。', delayTime: 3.44, str: '你骗鬼呢,合伙人带着行李来家里。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/瑶瑶,她是柳家的柳如燕,为了救我跟柳家断了。', delayTime: 4.64, str: '瑶瑶,她是柳家的柳如燕,为了救我跟柳家断了。', posi: '-1' },
        { effectUrl: 'DH/DH_37/sound/既然救过汉升哥哥,那我给她安排在客房住下吧。', delayTime: 6.0, str: '既然救过汉升哥哥,那我给她安排在客房住下吧。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/汉升,这个未成年小女孩是你妹妹?', delayTime: 4.3, str: '汉升,这个未成年小女孩是你妹妹?', posi: 'qipao2' },
        { effectUrl: 'DH/DH_37/sound/哦?是你女朋友啊,那你挺贪心啊,有女朋友还带我回家?', delayTime: 7.04, str: '哦?是你女朋友啊,那你挺贪心啊,有女朋友还带我回家?', posi: 'qipao2' },
        { effectUrl: 'DH/DH_37/sound/汉升哥哥,我就只是你的妹妹?那我走!', delayTime: 4.3, str: '汉升哥哥,我就只是你的妹妹?那我走!', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/汉升哥哥,我是你的合伙人?', delayTime: 2.16, str: '汉升哥哥,我是你的合伙人?', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/行了,就这样吧,都早点休息吧。', delayTime: 2.39, str: '行了,就这样吧,都早点休息吧。', posi: '-1' },
        { effectUrl: 'DH/DH_37/sound/那汉升哥哥,等下你来我房间,我先去洗澡了。', delayTime: 4.59, str: '那汉升哥哥,等下你来我房间,我先去洗澡了。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/那汉升,你等下来我房间,和我一起练瑜伽啊。', delayTime: 4.19, str: '那汉升,你等下来我房间,和我一起练瑜伽啊。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_37/sound/汉升哥哥,我就知道你会选择我的。', delayTime: 2.91, str: '汉升哥哥,我就知道你会选择我的。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_37/sound/没想到你这么无情,竟然抛弃女朋友来我这。', delayTime: 4.14, str: '没想到你这么无情,竟然抛弃女朋友来我这。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_37/sound/汉升哥哥,我忘记拿毛巾了,可以帮我拿条毛巾吗?', delayTime: 5.0, str: '汉升哥哥,我忘记拿毛巾了,可以帮我拿条毛巾吗?', posi: 'qipao4' },
        { effectUrl: 'DH/DH_37/sound/汉升,要不要跟我一起做双人瑜伽呀!', delayTime: 3.78, str: '汉升,要不要跟我一起做双人瑜伽呀!', posi: 'qipao5' },
    ]

    @property(sp.Skeleton)
    suyao: sp.Skeleton = null
    @property(sp.Skeleton)
    liuruyan: sp.Skeleton = null

    @property(sp.Skeleton)
    suyao_big: sp.Skeleton = null
    @property(sp.Skeleton)
    liuruyan_big: sp.Skeleton = null

    @property(sp.Skeleton)
    zuoyongyoubao: sp.Skeleton = null

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

    start() {
        Common5.playRemoteAudioMusic("remotesound/effect/轻快-10秒", true)
        Common5.preLoadRemoteAudioByChatConfig(this.chatConfig);
        super.start();
        this.continueStory();
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_379)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_38)
        })
        this.suyao.node.active = true
        this.liuruyan.node.active = true
        this.suyao.setAnimation(0, "苏瑶", true)
        this.liuruyan.setAnimation(0, "柳如燕2", true)
        this.suyao_big.node.active = false
        this.liuruyan_big.node.active = false
        const dialogEvents = [
            (func) => {
                this.showQiPao(this.chatConfig[0], func, () => {
                    this.suyao.setAnimation(0, "苏瑶指人说话", true)
                });
            },
            (func) => {
                this.suyao.setAnimation(0, "苏瑶", true)
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {
                    let strs = ['她是我女朋友', '这是我一个朋友', '这是我找的合伙人']
                    let actives = [true, true, true]
                    let funcs = [
                        () => {
                            this.showQiPao(this.chatConfig[1], () => {
                                PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                    node.getComponent(taskState).showFail("任务失败,重新开始", () => {
                                        this.continueStory();
                                    }, () => {
                                        this.showQiPao(this.chatConfig[2], () => {
                                            func()
                                        }, () => {
                                            this.suyao.setAnimation(0, "苏瑶说话", true)
                                        });
                                    })
                                })
                                // PrefabManage.showTextTips("任务失败,重新开始")
                                // this.continueStory()
                            }, () => {
                                this.suyao.setAnimation(0, "苏瑶失望说话", true)
                            });
                        },
                        () => {
                            this.showQiPao(this.chatConfig[2], () => {
                                func()
                            }, () => {
                                this.suyao.setAnimation(0, "苏瑶说话", true)
                            });
                        },
                        () => {
                            this.showQiPao(this.chatConfig[3], () => {
                                PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                    node.getComponent(taskState).showFail("任务失败,重新开始", () => {
                                        this.continueStory();
                                    }, () => {
                                        this.showQiPao(this.chatConfig[2], () => {
                                            func()
                                        }, () => {
                                            this.suyao.setAnimation(0, "苏瑶说话", true)
                                        });
                                    })
                                })
                                // PrefabManage.showTextTips("任务失败,重新开始")
                                // this.continueStory()
                            }, () => {
                                this.suyao.setAnimation(0, "苏瑶失望说话", true)
                            });
                        },
                    ]
                    let isvideo = [false, false, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                })
            },
            (func) => {
                this.suyao.setAnimation(0, "苏瑶", true)
                this.showClickDialog(this.chatConfig[4], func, () => {
                    // this.nanzhu.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.showQiPao(this.chatConfig[5], func, () => {
                    this.suyao.setAnimation(0, "苏瑶说话", true)
                });
            },
            (func) => {
                this.suyao.setAnimation(0, "苏瑶", true)
                this.showQiPao(this.chatConfig[6], func, () => {
                    this.liuruyan.setAnimation(0, "柳如燕说话", true)
                });
            },
            (func) => {
                this.liuruyan.setAnimation(0, "柳如燕2", true)
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {
                    let strs = ['不是的,如燕,这是我女朋友。', '是的,如燕,这是我认的妹妹。', '不是的,如燕,这是我合伙人。']
                    let actives = [true, true, true]
                    let funcs = [
                        () => {
                            this.showQiPao(this.chatConfig[7], () => {
                                func()
                            }, () => {
                                this.liuruyan.setAnimation(0, "柳如燕说话", true)
                                // this.suyao.setAnimation(0, "生气说话", true)
                            });
                        },
                        () => {
                            this.showQiPao(this.chatConfig[8], () => {
                                this.suyao.setAnimation(0, "苏瑶", true)
                                PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                    node.getComponent(taskState).showFail("任务失败,重新开始", () => {
                                        this.continueStory();
                                    }, () => {
                                        this.showQiPao(this.chatConfig[7], () => {
                                            func()
                                        }, () => {
                                            this.liuruyan.setAnimation(0, "柳如燕说话", true)
                                            // this.suyao.setAnimation(0, "生气说话", true)
                                        });
                                    })
                                })
                                // PrefabManage.showTextTips("任务失败,重新开始")
                                // this.continueStory()
                            }, () => {
                                this.suyao.setAnimation(0, "苏瑶失望说话", true)
                            });
                        },
                        () => {
                            this.showQiPao(this.chatConfig[9], () => {
                                this.suyao.setAnimation(0, "苏瑶", true)
                                PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                    node.getComponent(taskState).showFail("任务失败,重新开始", () => {
                                        this.continueStory();
                                    }, () => {
                                        this.showQiPao(this.chatConfig[7], () => {
                                            func()
                                        }, () => {
                                            this.liuruyan.setAnimation(0, "柳如燕说话", true)
                                            // this.suyao.setAnimation(0, "生气说话", true)
                                        });
                                    })
                                })
                                // PrefabManage.showTextTips("任务失败,重新开始")
                                // this.continueStory()
                            }, () => {
                                this.suyao.setAnimation(0, "苏瑶失望说话", true)
                            });
                        },
                    ]
                    let isvideo = [false, false, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                })
            },
            (func) => {
                this.liuruyan.setAnimation(0, "柳如燕2", true)
                this.showClickDialog(this.chatConfig[10], func, () => {
                    // this.nanzhu.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.runBg(0.5, 5, this.MoveNodes.getChildByName("苏瑶"), () => {
                    func()
                })
            },
            (func) => {
                this.suyao.node.active = false
                this.suyao_big.node.active = true
                this.showQiPao(this.chatConfig[11], func, () => {
                    this.suyao_big.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.runBg(0.5, 5, this.MoveNodes.getChildByName("柳如燕"), () => {
                    func()
                })
            },
            (func) => {
                this.liuruyan.node.active = false
                this.liuruyan_big.node.active = true
                this.showQiPao(this.chatConfig[12], func, () => {
                    this.liuruyan_big.setAnimation(0, "柳如燕说话", true)
                });
            },
            (func) => {
                this.liuruyan.setAnimation(0, "柳如燕2", true)
                this.suyao.node.active = true
                this.suyao_big.node.active = false
                this.liuruyan.node.active = true
                this.liuruyan_big.node.active = false
                this.runBg(1, 1, this.node, () => {
                    func()
                })
            },
            (func) => {
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {
                    let strs = ['去苏瑶房间', '去燕子房间', '小孩子才做选择,大人全都要']
                    let actives = [true, true, true]
                    let funcs = [
                        () => {
                            this.suyao.node.active = false
                            this.suyao_big.node.active = true
                            this.runBg(0, 5, this.MoveNodes.getChildByName("苏瑶"), () => {
                                this.showQiPao(this.chatConfig[13], () => {
                                    this.suyao_big.setAnimation(0, "待机", true)
                                    PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                        node.getComponent(taskState).showFail("任务失败,重新开始", () => {
                                            this.runBg(0, 1, this.node, () => {
                                                this.continueStory();
                                            })
                                        }, () => {
                                            func()
                                        })
                                    })
                                    // PrefabManage.showTextTips("任务失败,重新开始")
                                    // this.continueStory()
                                }, () => {
                                    // this.suyao.setAnimation(0, "沮丧说话", true)
                                });
                            })

                        },
                        () => {
                            this.liuruyan.node.active = false
                            this.liuruyan_big.node.active = true
                            this.runBg(0, 5, this.MoveNodes.getChildByName("柳如燕"), () => {
                                this.showQiPao(this.chatConfig[14], () => {
                                    this.liuruyan_big.setAnimation(0, "待机", true)
                                    PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                        node.getComponent(taskState).showFail("任务失败,重新开始", () => {
                                            this.runBg(0, 1, this.node, () => {
                                                this.continueStory();
                                            })
                                        }, () => {
                                            func()
                                        })
                                    })
                                    // PrefabManage.showTextTips("任务失败,重新开始")
                                    // this.continueStory()
                                }, () => {
                                    // this.suyao.setAnimation(0, "害羞说话", true)
                                });
                            })

                        },
                        () => {
                            func()
                        },

                    ]
                    let isvideo = [false, false, true]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                })
            },
            (func) => {
                this.suyao.node.active = true
                this.suyao_big.node.active = false
                this.liuruyan.node.active = true
                this.liuruyan_big.node.active = false
                this.runBg(0, 1, this.node, () => {
                    // func()
                })
                // 先是播放苏瑶洗澡的动效(苗的觉醒时刻的动效),然后进入柳如燕的房间,燕子展示几个瑜伽动作(动效)
                Common5.playRemoteAudioMusic('remotesound/effect/暧昧-10秒', false)
                this.scheduleOnce(() => {
                    Common5.playRemoteAudioEffect('remotesound/effect/心跳')
                }, 0.1)
                PrefabManage.showBlackGuoDu(() => {
                    this.m_bg.getChildByName("苏瑶房间").active = true
                    this.showQiPao(this.chatConfig[15], null, () => {
                    });
                })
                this.scheduleOnce(() => {
                    PrefabManage.showBlackGuoDu(() => {
                        this.m_bg.getChildByName("苏瑶房间").active = false
                        this.m_bg.getChildByName("柳如燕房间").active = true
                        this.showQiPao(this.chatConfig[16], null, () => {
                        });
                    }, () => {
                        let yujia = this.m_bg.getChildByName("柳如燕房间").getChildByName("瑜伽")
                        yujia.getComponent(sp.Skeleton).setAnimation(0, "动作", false)
                        yujia.getComponent(sp.Skeleton).setCompleteListener(() => {
                            yujia.getComponent(sp.Skeleton).setCompleteListener(null)
                            yujia.getComponent(sp.Skeleton).setAnimation(0, "呼吸", true)

                            this.scheduleOnce(() => {
                                // 展示亮相图片,男主站中间,两女左右抱男主手站两侧。
                                PrefabManage.showBlackGuoDu(() => {
                                    this.m_bg.getChildByName("苏瑶房间").active = false
                                    this.m_bg.getChildByName("柳如燕房间").active = false
                                    this.suyao.node.active = false
                                    this.liuruyan.node.active = false
                                    this.zuoyongyoubao.node.active = true
                                    func()
                                })
                            }, 1)
                        })
                    })
                }, 6)

            },
            (func) => {
                this.scheduleOnce(() => {
                    func()
                }, 2)
            }

        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_549)
                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_550_1)
                    PrefabManage.showBlackGuoDu(() => {
                        PrefabManage.loadPrefabByType(GameType.DH_38)
                        this.node.removeFromParent()
                        this.node.destroy()
                    })
                    // 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_379")
                    //         // 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)
            }
        })
    }

    runBg(time, scale, endnode, callFunc) {
        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()
    }
}