import { _decorator, Component, EventTouch, Node, UITransform, Vec2, Vec3 } from 'cc';
import { widget_base } from './widget_base';
import { att_slide_data, other_widget_finish_listen_item, 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(){
        this.checkAddOtherWidgetFinishListenOperation()
    }

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

    // 检查添加其他控件
    private checkAddOtherWidgetFinishListenOperation() {
        let onFinishEvent_cb = (()=>{
            this.checkOtherListenWidgetFinish(false)
            this.onFinishEvent()
        })

        if(this.checkOtherWidgetFinishListenListData()==false) {
            onFinishEvent_cb()
            return
        }

        // 是-直接执行自身
        if(this.mSlideData.is_direct_execute_myself) { 
            // console.log('直接-执行自身')
            onFinishEvent_cb()
        } 
        // 否-直接执行自身
        else { 
            // 是-选择其一执行自身
            let finish_list = this.checkOtherListenWidgetFinish(true)
            // console.log('完成的控件列表=',finish_list)
            if(this.mSlideData.is_select_anyone_execute_myself) {
                let is_execute_myself = false
                for (let index = 0; index < finish_list.length; index++) {
                    const element = finish_list[index];
                    if(element.is_execute_myself) {
                        is_execute_myself = true
                        break
                    }
                }
                if(is_execute_myself) {
                    // console.log('满足其中一个条件-执行自身')
                    onFinishEvent_cb()
                }
            } 
            // 否-选择其一执行自身
            else {
                // 控件全部完成
                if(finish_list.length==this.mSlideData.other_widget_finish_listen_list.length) {
                    // console.log('控件全部完成-执行自身')
                    onFinishEvent_cb()
                    return
                }

                // 同时满足
                let execute_count = 0
                let have_count = 0
                for (let i = 0; i < this.mSlideData.other_widget_finish_listen_list.length; i++) {
                    const i_element = this.mSlideData.other_widget_finish_listen_list[i]
                    if(i_element.is_execute_myself) {
                        execute_count+=1
                        for (let j = 0; j < finish_list.length; j++) {
                            const j_element = finish_list[j];
                            if(i_element.widget_id==j_element.widget_id) {
                                have_count+=1
                            }
                        }
                    }
                }

                if(execute_count>0) {
                    if(execute_count==have_count) {
                        // console.log('同时满足条件-执行自身')
                        onFinishEvent_cb()
                        return
                    }
                }
            }

        }

        // 未完成控件,执行监控事件
        this.checkOtherListenWidgetFinish(false)
    }

    private checkOtherWidgetFinishListenListData():boolean {
        if(this.mSlideData.other_widget_finish_listen_list==undefined){
            return false
        }
        if(this.mSlideData.other_widget_finish_listen_list.length<=0){
            return false
        }
        return true
    }

    private checkOtherListenWidgetFinish(is_finish_status:boolean):other_widget_finish_listen_item[]{
        if(this.checkOtherWidgetFinishListenListData()==false) {
            return []
        }
        let list = gameManager.Singleton.checkWidgetList(this.mSlideData.other_widget_finish_listen_list,is_finish_status)
        if(is_finish_status==false&&list.length>0) {
            // for (let index = 0; index < list.length; index++) {
            //     const event = list[index];
            //     gameManager.Singleton.exeEvent(event.event_id)
            // }
            // console.log('list=',list)
            // 未完成:其中有2,只执行2; 没有2,所有1执行; 没有2&&没有1,所有0执行;
            let hight_list:other_widget_finish_listen_item[] = []
            let middle_list:other_widget_finish_listen_item[] = []
            let normal_list:other_widget_finish_listen_item[] = []
            for (let index = 0; index < list.length; index++) {
                const event = list[index];
                if(event.grade>=2) {
                    hight_list.push(event)
                    break
                } else if(event.grade==1) {
                    middle_list.push(event)
                } else {
                    normal_list.push(event)
                }
            }

            let exeEvent_cb = ((item_list:other_widget_finish_listen_item[])=>{
                for (let index = 0; index < item_list.length; index++) {
                    const event = item_list[index];
                    gameManager.Singleton.exeEvent(event.event_id)
                }
            })

            if(hight_list.length>0) {
                middle_list = []
                normal_list = []
                exeEvent_cb(hight_list)
            } else {
                if(middle_list.length>0) {
                    normal_list = []
                    exeEvent_cb(middle_list)
                } else {
                    exeEvent_cb(normal_list)
                }
            }

        }
        return list;
    }
}


