import { _decorator, Component, EventTouch, Node, UITransform, Vec2, Vec3 } from 'cc';
import { widget_base } from './widget_base';
import { att_slide_data, widget_item_data } from '../../../data/data';
import { config } from '../../config';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('widget_slide')
export class widget_slide extends widget_base {
    @property(Node) img_zhaobutong:Node = null;
    private mSlideData:att_slide_data = null;
    private mStartClickPos:Vec2 = null;
    private mCurSlideNum:number = 0;
    private mSlideFinishUp:boolean = false;
    private mSlideFinishDown:boolean = false;
    private mSlideFinishLeft:boolean = false;
    private mSlideFinishRight:boolean = false;
    protected init(){
        if(this.mData.att.slide_data!=null){
            this.mSlideData = this.mData.att.slide_data
        }
    }
    protected start(): void {
        if(this.mIsActive){
            this.registeredEvent()
        }
    }
    protected registeredEvent(): void {
        if( !this.mSlideData ){
            return
        }
        this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
            if(!this.mIsActive) return
            this.mStartClickPos = et.getUILocation()
        })

        if(this.mSlideData.slide_dir==config.slide_type.up_down||this.mSlideData.slide_dir==config.slide_type.left_right){
            this.node.on(Node.EventType.TOUCH_END,()=>{
                if(!this.mIsActive) return
                this.mSlideFinishUp = false;
                this.mSlideFinishDown = false;
                this.mSlideFinishLeft = false;
                this.mSlideFinishRight = false;
            })
            this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
                if(!this.mIsActive) return
                let isFinish = false;
                let distance = this.mSlideData.slide_distance;

                let et_ =et.getUILocation()
                let p = new Vec3(et_.x,et_.y)
                let pos = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
                switch (this.mSlideData.slide_dir) {
                    case config.slide_type.up_down:
                      let down_y =  (this.node.position.y - distance*0.5);
                      let up_y = (this.node.position.y + distance*0.5);
                      if(pos.y>=up_y){
                         this.mSlideFinishUp = true;
                      }
                      if(pos.y<=down_y){
                        this.mSlideFinishDown = true;
                      }

                      if(this.mSlideFinishUp&&this.mSlideFinishDown){
                        isFinish = true;
                        this.mSlideFinishUp = false;
                        this.mSlideFinishDown = false;
                      }
                    break;   
                    case config.slide_type.left_right:
                        let left_x =  (this.node.position.x - distance*0.5);
                        let right_x = (this.node.position.x + distance*0.5);
                        if(pos.x>=right_x){
                           this.mSlideFinishRight = true;
                        }
                        if(pos.x<=left_x){
                          this.mSlideFinishLeft = true;
                        }
  
                        if(this.mSlideFinishLeft&&this.mSlideFinishRight){
                          isFinish = true;
                          this.mSlideFinishLeft = false;
                          this.mSlideFinishRight = false;
                        }
                    break;  
                }
                if(isFinish){
                    this.mCurSlideNum++;
                }
    
                if(this.mCurSlideNum>=this.mSlideData.slide_num){
                    this.exeFinish()
                }
            })
        }else{
            this.node.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{
                if(!this.mIsActive) return
                let isFinish = false;
                let endClickPos = et.getUILocation()
                switch (this.mSlideData.slide_dir) {
                    case config.slide_type.up:
                        if((endClickPos.y-this.mStartClickPos.y)>=this.mSlideData.slide_distance){
                            isFinish = true;
                        }
                    break;
                    case config.slide_type.down:
                        if((this.mStartClickPos.y-endClickPos.y)>=this.mSlideData.slide_distance){
                            isFinish = true;
                        }
                    break;    
                    case config.slide_type.left:
                        if((this.mStartClickPos.x-endClickPos.x)>=this.mSlideData.slide_distance){
                            isFinish = true;
                        }
                    break;   
                    case config.slide_type.right:
                        if((endClickPos.x-this.mStartClickPos.x)>=this.mSlideData.slide_distance){
                            isFinish = true;
                        }
                    break;      
                }
                if(isFinish){
                    this.exeFinish()
                }
           
            })
        }

    }
    public exeFinish(){
        if(this.checkOtherListenWidgetFinish()){
            this.onFinishEvent()
        }
    }
    checkOtherListenWidgetFinish(){
        let is_finish = true;
        if(this.mSlideData.other_widget_finish_listen_list==undefined){
            return true
        }
        if(this.mSlideData.other_widget_finish_listen_list.length<=0){
            return true;
        }
        let event_id = gameManager.Singleton.checkWidgetList(this.mSlideData.other_widget_finish_listen_list)
        if(event_id==-1){
            is_finish = true;
        }else{
            is_finish = false;
            gameManager.Singleton.exeEvent(event_id)
        }
        return is_finish;
    }
    public initSlide(){
        this.mCurSlideNum = 0;
        this.mSlideFinishUp = false;
        this.mSlideFinishDown = false;
        this.mSlideFinishLeft = false;
        this.mSlideFinishRight = false;
    }

    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;
        }
    }
}


