import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
const { ccclass, property } = cc._decorator;

@ccclass
export default class DHBase extends cc.Component {

    dialogIndex: number = 0;
    duihuakuang1: cc.Node;
    duihuakuang2: cc.Node;
    selectIndex: number = 0;

    start() {
        // for (let i = 0; i < this.chatConfig.length; i++) {
        //     let effectUrl = this.chatConfig[i].effectUrl
        //     Common5.loadRemoteAudioEffect(effectUrl);
        // }

        // let head = this.node.getChildByName("对话框").getChildByName("头像");
        // Common5.addUrlSprite_custom("head/" + User.getHeadImg(), head.getComponent(cc.Sprite));

        this.dialogIndex = 0;
    }

    // 展示气泡
    showQiPao(curLog, func?, aniFunc?) {
        //cc.audioEngine.stopAllEffects();
        if (!curLog) {
            return
        }
        if (curLog.effectUrl && curLog.effectUrl.length > 0) {
            Common5.playRemoteAudioEffect(curLog.effectUrl);
            // Common5.playEffectCustom("GameRes", curLog.effectUrl);
        }

        let string_ = curLog.str
        let qiPaoPos_ = curLog.posi ?? curLog.qiPaoPos
        let delayTime_ = curLog.delayTime ?? curLog.delay

        if (aniFunc) {
            aniFunc()
        }

        if (qiPaoPos_ != '-1') {
            let node = this.node.getChildByName('bg').getChildByName('qipao');
            let qiPao = node.getChildByName(qiPaoPos_);
            qiPao.stopAllActions()
            this.showDialogStr(string_, qiPao.getChildByName("str"))
            // qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            // let scale = qiPao.scale
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 / this.node.getChildByName('bg').scale })
                .delay(delayTime_)
                .call(() => {
                    qiPao.active = false;
                })
                .start();
        } else {
            let chatLog = this.node.getChildByName('chatLog')
            chatLog.active = true
            if (curLog.endStr) {
                chatLog.getChildByName("str").getComponent(cc.Label).string = curLog.endStr
            } else {
                if (this.selectIndex == 1) {
                    chatLog.getChildByName("str").getComponent(cc.Label).string = curLog.str1
                } else {
                    chatLog.getChildByName("str").getComponent(cc.Label).string = string_
                }
            }
            //this.showDialogStr(string_, chatLog.getChildByName("str"))
            chatLog.stopAllActions()
            cc.tween(chatLog)
                .delay(delayTime_)
                .call(() => {
                    chatLog.active = false;
                })
                .start();
        }

        this.scheduleOnce(() => {
            func && func();
        }, delayTime_ + 0.2);
    }

    // 逐字显示
    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.05, str.length - 1);
    }

    // 展示点击说话
    showClickDialog(curLog, func?, aniFunc?) {
        let text = curLog.str;
        if (text.length > 7) {
            text = curLog.str.slice(0, 6) + '...';
        }
        let dialog = this.node.getChildByName('对话框');
        dialog.getChildByName('lab').getComponent(cc.Label).string = text;
        dialog.active = true;
        this["_clickDialog"] = curLog;
        this["_clickDialogCallback"] = func;
        this["_clickDialogAniCallback"] = aniFunc;

        this.duihuakuang1 = dialog

        if (curLog.str1) {
            let text = curLog.str1;
            if (text.length > 7) {
                text = curLog.str1.slice(0, 6) + '...';
            }
            let node = cc.instantiate(dialog)
            node.name = "第二个选择"
            this.node.addChild(node)
            let position = dialog.position.clone()
            position.y += dialog.getContentSize().height
            node.setPosition(position)
            node.getChildByName('lab').getComponent(cc.Label).string = text;

            this.duihuakuang2 = node
        }
    }

    // 点击展示对话
    onClickShowDialog(event) {
        let target = event.target;
        target.active = false;
        if (this.duihuakuang1) {
            this.duihuakuang1.active = false
        }
        if (this.duihuakuang2) {
            this.duihuakuang2.active = false
        }

        if (target.name == "第二个选择") {
            this.selectIndex = 1
        } else {
            this.selectIndex = 0
        }

        let clickDialog = this["_clickDialog"];
        let clickDialogCallback = this["_clickDialogCallback"];
        let clickDialogAniCallback = this["_clickDialogAniCallback"];
        if (clickDialog) {
            this.showQiPao(clickDialog, () => {
                clickDialogCallback && clickDialogCallback();
            }, clickDialogAniCallback);
        }
    }

    onDestroy(): void {
        cc.audioEngine.stopMusic()
    }
}