import { SpriteActionType } from "../object/FSprite";
import BaseEvent from "./base/BaseEvent";

const {ccclass, property} = cc._decorator;
/**
 * 推石头
 * 
 * tag == 1 左右推动
 * tag == 2 上下推动
 */
@ccclass
export default class FStoreTS extends BaseEvent {

    @property({
        displayName: '提示按钮图标',
        type: cc.SpriteFrame
    })
    mTipsIcon: cc.SpriteFrame = null;

    private mRigidBody:cc.RigidBody = null
    /**
     * 推动中
     */
    private isPull = false;
    /**
     * 当前移动方向
     */
    private moveVt = cc.v2()

    start(){
        this.mRigidBody = this.node.getComponent(cc.RigidBody);
    }

    onBegin(tag:number){
        this.startEvent()
        if(tag == 1){
            this.moveVt.y = 0
            let myX = this.ff.mainSprite.node.x
            if(myX < this.node.x){
                this.moveVt.x = 7
            }else{
                this.moveVt.x = -7
            }
        }else if(tag == 2){
            this.moveVt.x = 0
            let myY = this.ff.mainSprite.node.y
            if(myY < this.node.y){
                this.moveVt.y = 7
            }else{
                this.moveVt.y = -7
            }
        }
    }

    onEnd(tag:number){
        this.endEvent()
    }

    private startEvent(){
        let fControl = this.ff.control;
        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_START, this._touchRunningStartEvent, this);
        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchRunningMoveEvent, this);
        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_END, this._touchRunningEndEvent, this);
        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchRunningEndEvent, this);
        this.showOpt(this.mTipsIcon, () => {
        })
    }
    private endEvent(){
        this.closeOpt()
        
        let fControl = this.ff.control;
        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_START);
        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_MOVE);
        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_END);
        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_CANCEL);
        this.isPull = false;
    }
    private  _touchRunningMoveEvent(){
        cc.log('_touchRunningMoveEvent')
    }
    private  _touchRunningStartEvent(){
        cc.log('_touchRunningStartEvent')
        this.isPull = true;
        cc.tween(this).sequence(
            cc.delayTime(0.01),
            cc.callFunc(()=>{
                this.mRigidBody.type = cc.RigidBodyType.Dynamic
            })
        ).start()
        
        let mainSprite = this.ff.mainSprite

        // mainSprite.spine.setAnimation(0, SpriteActionType.run, true);

    }
    private _touchRunningEndEvent(){
        cc.log('_touchRunningEndEvent')
        this.isPull = false;
        cc.tween(this).sequence(
            cc.delayTime(0.01),
            cc.callFunc(()=>{
                this.mRigidBody.type = cc.RigidBodyType.Static
            })
        ).start()
        // let mainSprite = this.ff.mainSprite
        // mainSprite.spine.setAnimation(0, SpriteActionType.stand, true);
    }

    public update(dt){
        if(this.isPull){
            let mainSprite = this.ff.mainSprite
            mainSprite.node.x += this.moveVt.x
            mainSprite.node.y += this.moveVt.y
        }
    }


}
