import { _decorator, Component, Node, Tween, tween, UITransform, Vec3 } from 'cc';
import { ui_base } from './ui_base';
import { pointer_ding_wei_data } from '../../../data/data';
import { tools } from '../../tools';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('ui_pointer_ding_wei')
export class ui_pointer_ding_wei extends ui_base {
    @property(Node) pointer_bar:Node = null;
    @property(Node) pointer_bar_point:Node = null;
    @property(Node) img_text:Node = null;
    @property(Node) img_eye_1:Node = null;
    @property(Node) img_eye_2:Node = null;
    @property(Node) btn_pointer_ding_wei_sure:Node = null;
    private mPointDingWeiData:pointer_ding_wei_data = null;
    private duration:number = 1;
    private moveDirIsRight:boolean = true; 
    private rightLength:number = 200;
    protected init(): void {
        this.mPointDingWeiData = this.mTopData._pointer_ding_wei_data;
        if(this.mPointDingWeiData===null){
            return tools.showToast("指针定位配置错误~")
        }
        this.rightLength = this.mPointDingWeiData.rightLength;
        this.initPointerDingWei()
    }
    initPointerDingWei(){
        this.loadBg(this.mPointDingWeiData.bg)
        gameManager.initUiBaseAtt(this.pointer_bar,this.mPointDingWeiData.pointer_bar)
        gameManager.initUiBaseAtt(this.pointer_bar_point,this.mPointDingWeiData.pointer_bar_point)
        gameManager.initUiBaseAtt(this.img_text,this.mPointDingWeiData.img_text)
        gameManager.initUiBaseAtt(this.img_eye_1,this.mPointDingWeiData.img_eye_1)
        gameManager.initUiBaseAtt(this.img_eye_2,this.mPointDingWeiData.img_eye_2)
        gameManager.initUiBaseAtt(this.btn_pointer_ding_wei_sure,this.mPointDingWeiData.btn_pointer_ding_wei_sure)
        this.btn_pointer_ding_wei_sure.on(Node.EventType.TOUCH_START,this.stopRunPoint.bind(this))
        this.startRunPoint()
    }



    stopRunPoint(){
        Tween.stopAllByTarget(this.pointer_bar_point)
        let r= this.rightLength*0.5;
        if(this.pointer_bar_point.position.x>(-r)&&this.pointer_bar_point.position.x<r){
            this.onFinishEvent()
            console.log("过关成功")
        }else{
            this.onFialEvent()
            console.log("过关失败")
        }

    }

    startRunPoint(){
        let len = this.pointer_bar.getComponent(UITransform).contentSize.width*0.5;
        let duration = this.pointer_bar_point.position.x===0?this.duration*0.5:this.duration;
        if(this.moveDirIsRight){
            tween(this.pointer_bar_point).to(duration,{position:new Vec3(len,0,0)}).call(()=>{
                this.moveDirIsRight=!this.moveDirIsRight;
                this.startRunPoint()
            }).start()
        }else{
            tween(this.pointer_bar_point).to(duration,{position:new Vec3(-len,0,0)}).call(()=>{
                this.moveDirIsRight=!this.moveDirIsRight;
                this.startRunPoint()
            }).start()
        }
        
    }
}


