import { _decorator, Component, Node } from 'cc';
import { widget_base } from './widget_base';
import { att_click_data, widget_item_data } from '../../../data/data';
import { config } from '../../config';
import { ClientEvent } from '../../clientEvent';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('widget_click')
export class widget_click extends widget_base {
    @property(Node) img_zhaobutong:Node = null;
    private mClickData:att_click_data = null;
    private mClickNum:number = 0;
    private mClickTime:number =1; //1秒内完成点击
    private mHoldTime:number = 0; //按下的事件
    protected init(): void {
        if(this.mData.att.click_data!=null){
            this.mClickData = this.mData.att.click_data
        }
    }
    protected start(): void {
        if(this.mIsActive){
            this.registeredEvent()
        }
    }
    protected registeredEvent(): void {
        this.mHoldTime = 0;
        this.mClickNum = 0;
        switch (this.mClickData.click_type) {
            case config.clcik_type.Press_and_hold:
                this.node.on(Node.EventType.TOUCH_START,()=>{
                    if(!this.mIsActive) return
                    this.mHoldTime = 0;
                    this.checkTime()
                    this.schedule(this.checkTime,1)
                })
                this.node.on(Node.EventType.TOUCH_END,()=>{
                    if(!this.mIsActive) return
                    if(!this.getIsFinish()){
                        this.mHoldTime = 0;
                        this.unschedule(this.checkTime)
                    }
                })
            break;
            case config.clcik_type.Multiple_clicks:
                this.node.on(Node.EventType.TOUCH_START,()=>{
                    if(!this.mIsActive) return
                    this.scheduleOnce(this.reSetClickNum,this.mClickTime);
                    this.mClickNum++;
                    if(this.mClickNum===this.mClickData.multiple_check_num){
                        this.mClickNum = 0;
                        this.unschedule(this.reSetClickNum)
                        this.exeFinish()
                    }
                })
            break;
            case config.clcik_type.click:
                this.node.off(Node.EventType.TOUCH_END)
                this.node.on(Node.EventType.TOUCH_END,()=>{
                    if(!this.mIsActive) return
                    this.exeFinish()
                })
            break;
            case config.clcik_type.click_not_auto_finish:
                this.node.off(Node.EventType.TOUCH_END)
                this.node.on(Node.EventType.TOUCH_END,()=>{
                    if(!this.mIsActive) return
                    ClientEvent.dispatchEvent(config.EventRun.WIDGET_FINISH,this.mData.att.id)
                })
            break;
        }
    }

    public exeFinish(){
        if(this.checkOtherListenWidgetFinish()){
            this.onFinishEvent()
        }
    }

    checkOtherListenWidgetFinish(){
        let is_finish = true;
        if(this.mClickData.other_widget_finish_listen_list==undefined){
            return true
        }
        if(this.mClickData.other_widget_finish_listen_list.length<=0){
            return true;
        }
        let event_id = gameManager.Singleton.checkWidgetList(this.mClickData.other_widget_finish_listen_list)
        if(event_id==-1){
            is_finish = true;
        }else{
            is_finish = false;
            gameManager.Singleton.exeEvent(event_id)
        }
        return is_finish;
    }
    private checkTime(){
        this.mHoldTime+=1;
        if(this.getIsFinish()){
            this.unschedule(this.checkTime)
            return
        }
        if(this.mClickData.hold_time<=this.mHoldTime){
            this.exeFinish()
            this.unschedule(this.checkTime)
            return
        }
    }

    private reSetClickNum(){
        this.mClickNum = 0;
    }
    public showZhaoButongFinishStatus(): void {
        gameManager.Singleton.sys_click_correct_detail_music()
        this.img_zhaobutong.active = true;
    }
    public hideZhaoButongFinishStatus(): void {
        if(this.img_zhaobutong != null) {
            this.img_zhaobutong.active = false;
        }
    }
}


