import FObject from "../object/FObject";
import FF from "../FF";
import DialogButton from "../box/DialogButton";
import i18n from "../../../i18n/i18n";
/**
 * 泉水
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FMqs extends cc.Component {

    @property({
        type:cc.Prefab,
        displayName: '事件按钮'
    })
    mDialog: cc.Prefab = null;

    @property({
        type:cc.Prefab,
        displayName: '对话框'
    })
    mDialog1: cc.Prefab = null;

    public temp:cc.Node = null;
    public ff:FF = null;

    public count:number = 0;
    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider){
        if(otherCollider.node.group == 'A'){
            let sobject = otherCollider.node.getComponent(FObject);
            if(sobject){
                let ff = sobject.ff;
                this.ff = ff;
                this.count ++;
                this.showButton(ff);
            }
        }
    }
    onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider){
        if(otherCollider.node.group == 'A'){
            let sobject = otherCollider.node.getComponent(FObject);
            if(sobject){
                let ff = sobject.ff;
                this.count --;
                if(this.count <= 0){
                    this.closeButton();
                }
            }
        }
    }

    public showButton(ff:FF){
        if(this.temp){
            return;
        }
        let node = cc.instantiate(this.mDialog);
        ff.main.viewManage.popView(node);
        this.temp = node;

        let fbutton = node.getComponent(DialogButton);
        fbutton.label.string = i18n.t('饮用')
        fbutton.setCallback(()=>{
            this.closeButton();
            this.openDialog();
        });
    }
    public closeButton(){
        this.count = 0;
        if(this.temp){
            this.temp.destroy();
            this.temp = null;
        }
    }

    public openDialog(){
        let node = cc.instantiate(this.mDialog1);
        this.ff.main.viewManage.popView(node);
    }

}
