import FF from "../../FF";
import FSprite from "../../object/FSprite";
import FDialogPack26 from "./FDialogPack26";

/**
 * 摘果子任务中的果子
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FFruit26 extends cc.Component {

    @property({
        type:cc.ProgressBar,
        displayName: '进度条'
    })
    public mProgressBar:cc.ProgressBar = null;

    private ff:FF;
    public pick26:FDialogPack26;
    onLoad() {
        this.mProgressBar.node.active = false;
    }
    public setPick26(pick26:FDialogPack26){
        this.pick26 = pick26;
    }

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(other.node.group == 'A'){
            if(this.pick26 && this.pick26.state == 1){
                let obj = other.node.getComponent(FSprite);
                this.ff = obj.ff;
                if(obj == this.ff.mainSprite){
                    this.showButton();
                }
            }
        }
    }

    onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(other.node.group == 'A'){
            let obj = other.node.getComponent(FSprite);
            this.ff = obj.ff;
            if(obj == this.ff.mainSprite){
                this.closeButton();
            }
        }
    }
    public showButton(){
        this.ff.control.mEventButton.node.active = true;
        this.ff.control.mEventButton.setCallback(()=>{
            this.pick();
        });
    }
    public closeButton(){
        this.ff.control.mEventButton.node.active = false;
    }
    /**
     * 摘取果子
     */
    public pick(){
        this.mProgressBar.node.active = true;
        this.mProgressBar.progress = 0;
        this.ff.mBlockInputEvents.active = true;
        this.schedule(this.pupdate,0.1);

    }
    public pupdate(){
        let progress = this.mProgressBar.progress;
        progress += 0.1;
        this.mProgressBar.progress = progress;
        if(progress >= 1){
            this.ff.mBlockInputEvents.active = false;
            this.unschedule(this.pupdate);
            this.ff.mFFheader.addTmpGood(this.pick26.goodId,1);
            this.node.removeComponent(cc.PhysicsBoxCollider);
            
            cc.tween(this.node).sequence(
                cc.fadeOut(0.5),
                cc.destroySelf()
            ).start()
        }
    }


}
