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

@ccclass('ui_wei_chi_pointer')
export class ui_wei_chi_pointer extends ui_base {
    @property(Node) img_hand:Node = null; //无效果一张图
    @property(Node) img_bar:Node = null;  //无效果一张图
    @property(Node) img_pointer:Node = null;
    @property(Node) img_text_bing_zhu_hu_xi:Node = null;  //无效果一张图
    @property(Node) btn_bingxi:Node = null; 
    @property(Node) lab_time_count:Node = null; //倒计时
    private rightLength:number = 200;
    private moveLen:number = 50;
    private curTime:number = 0;
    private time:number = 3;
    private mWeiChiPointer:wei_chi_pointer_data = null;
    protected init(): void {
        this.mWeiChiPointer = this.mTopData._wei_chi_pointer_data;
        if(this.mWeiChiPointer===null){
            return tools.showToast("指针定位界面编辑错误！")
        }
        this.rightLength = this.mWeiChiPointer.rightLength;
        this.time = this.mWeiChiPointer.time;
        this.initWeiChiPointer()
    }

    initWeiChiPointer(){
        this.loadBg(this.mWeiChiPointer.bg)
        gameManager.initUiBaseAtt(this.img_hand,this.mWeiChiPointer.img_hand)
        gameManager.initUiBaseAtt(this.img_bar,this.mWeiChiPointer.img_bar)
        gameManager.initUiBaseAtt(this.img_pointer,this.mWeiChiPointer.img_pointer)
        gameManager.initUiBaseAtt(this.img_text_bing_zhu_hu_xi,this.mWeiChiPointer.img_text_bing_zhu_hu_xi)
        gameManager.initUiBaseAtt(this.btn_bingxi,this.mWeiChiPointer.btn_bingxi)
        this.btn_bingxi.on(Node.EventType.TOUCH_START,this.onClickMove.bind(this))
    }

    protected onEnable(): void {
        this.startRunPoint()
    }

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

    }
    onClickMove(){
        if(this.img_pointer.position.x<(this.img_bar.getComponent(UITransform).contentSize.width*0.5)){
            this.img_pointer.position= new Vec3(this.img_pointer.position.x+this.moveLen,this.img_pointer.position.y)
        }     
    }

    startRunPoint(){
        this.lab_time_count.getComponent(Label).string = `倒计时 ${this.time-this.curTime} 秒`
        this.schedule(this.timeCount,1)
        this.schedule(()=>{
            if(this.img_pointer.position.x>-(this.img_bar.getComponent(UITransform).contentSize.width*0.5)){
                this.img_pointer.position= new Vec3(this.img_pointer.position.x-this.moveLen,this.img_pointer.position.y)
            }
        },0.5)
    }

    timeCount(){
        this.curTime++;
        this.lab_time_count.getComponent(Label).string = `倒计时 ${this.time-this.curTime} 秒`
        if(this.curTime>=this.time){
            this.curTime = 0;
            this.unscheduleAllCallbacks()
            this.stopRunPoint()
            return
        }
    }
}


