import { _decorator, BlockInputEvents, Component, Label, Node, Prefab, tween, v3 } from 'cc';
import { NodePoolManager } from '../../../Loading/scripts/manager/NodePoolManager';
import { AudioManager } from '../../../Loading/scripts/manager/AudioManager';


const { ccclass, property } = _decorator;

@ccclass('TipPanel')
export class TipPanel extends Component {

    @property(Prefab)
    tipPrefab: Prefab = null;

    private isAnimating: boolean = false;

    tipNode: Node = null;
    messageNode: Node = null;

    hasTip:boolean = false;


    /**
     * 显示提示框
     * @param title 标题
     * @param msg 消息
     * @param callback 确认回调
     * @param cancelCallback 取消回调
     * @param confirmStr 确认按钮文本
     * @param cancelStr 取消按钮文本
     * @param hasCancel 是否显示取消按钮
     */
    showTip(title: string, msg: string, callback?: Function, cancelCallback?: Function, confirmStr?: string, cancelStr?: string, hasCancel: boolean = true) {    
        // 从对象池获取提示框节点
        this.tipNode = NodePoolManager.instance.getNodeFromPoolStatic('TipMessagePanel', this.tipPrefab);           
        // 设置标题和消息
        let message = this.tipNode.getChildByName('message');
        this.messageNode = message;
        let titleLabel = message.getChildByName('title').getComponent(Label);
        let msgLabel = message.getChildByName('msg').getComponent(Label);
        titleLabel.string = title;
        msgLabel.string = msg;

        message.scale = v3(0, 0, 0);
        this.isAnimating = true;
        this.tipNode.getComponent(BlockInputEvents).enabled = true;
        // 添加到场景
        this.node.addChild(this.tipNode);
        this.hasTip = true;

        tween(message)
            .to(0.5, { scale: v3(1, 1, 1) }, { easing: 'backOut' })
            .call(() => {
                this.isAnimating = false;
                this.tipNode.getComponent(BlockInputEvents).enabled = false;
            })
            .start();



        if (hasCancel) {
            message.getChildByName('Btn_Confirm').active = true;
            message.getChildByName('confirm').active = true;
            message.getChildByName('Btn_Cancel').active = true;
            message.getChildByName('cancel').active = true;
            message.getChildByName('Btn_Confirm_1').active = false;
            message.getChildByName('confirm_1').active = false;
            // 添加确认按钮点击事件
            let confirmBtn = message.getChildByName('Btn_Confirm');
            if (confirmStr) {
                message.getChildByName('confirm').getComponent(Label).string = confirmStr;
            }
            confirmBtn.on(Node.EventType.TOUCH_END, () => {
                if (this.isAnimating) return;
                AudioManager.instance.playSFX('Common_Button_Click');
                this.isAnimating = true;
                this.tipNode.getComponent(BlockInputEvents).enabled = true;
                tween(this.messageNode)
                    .to(0.5, { scale: v3(0, 0, 0) }, { easing: 'backIn' })
                    .call(() => {
                        // 回收节点到对象池
                        this.closeTip();
                        // 执行回调
                        if (callback) {
                            callback();
                        }
                    })
                    .start();
            });
            // 添加取消按钮点击事件
            let cancelBtn = message.getChildByName('Btn_Cancel');
            if (cancelStr) {
                message.getChildByName('cancel').getComponent(Label).string = cancelStr;
            }
            cancelBtn.on(Node.EventType.TOUCH_END, () => {
                if (this.isAnimating) return;
                AudioManager.instance.playSFX('Common_Button_Click');
                this.closeTip();
                if (cancelCallback) {
                    cancelCallback();
                }
            });
        } else {
            message.getChildByName('Btn_Confirm').active = false;
            message.getChildByName('confirm').active = false;
            message.getChildByName('Btn_Cancel').active = false;
            message.getChildByName('cancel').active = false;
            message.getChildByName('Btn_Confirm_1').active = true;
            message.getChildByName('confirm_1').active = true;

            // 添加确认按钮点击事件
            let confirmBtn = message.getChildByName('Btn_Confirm_1');
            if (confirmStr) {
                message.getChildByName('confirm_1').getComponent(Label).string = confirmStr;
            }
            confirmBtn.on(Node.EventType.TOUCH_END, () => {
                if (this.isAnimating) return;
                AudioManager.instance.playSFX('Common_Button_Click');
                this.isAnimating = true;
                this.tipNode.getComponent(BlockInputEvents).enabled = true;
                tween(this.messageNode)
                    .to(0.5, { scale: v3(0, 0, 0) }, { easing: 'backIn' })
                    .call(() => {
                        this.closeTip();
                        // 执行回调
                        if (callback) {
                            callback();
                        }
                    })
                    .start();
            });
        }

        // // 添加关闭按钮点击事件
        // let closeBtn = message.getChildByName('Btn_Close_A');
        // closeBtn.on(Node.EventType.TOUCH_END, () => {
        //     if (this.isAnimating) return;
        //     AudioManager.instance.playSFX('Common_Button_Click');
        //     this.closeTip();
        // });
    }

    // 回收节点
    closeTip() {
        this.hasTip = false;
        // 取消所有按钮的监听事件
        this.messageNode.getChildByName('Btn_Confirm').off(Node.EventType.TOUCH_END);
        this.messageNode.getChildByName('Btn_Cancel').off(Node.EventType.TOUCH_END);
        this.messageNode.getChildByName('Btn_Confirm_1').off(Node.EventType.TOUCH_END);
        // this.messageNode.getChildByName('Btn_Close_A').off(Node.EventType.TOUCH_END);
        // 回收节点
        NodePoolManager.instance.putNodeToPool('TipMessagePanel', this.tipNode);
    }

    getHasTip() {
        return this.hasTip;
    }
}


