import { _decorator, Color, Component, EventTouch, instantiate, Label, Node, Prefab, UITransform, Vec3 } from 'cc';
import { Attributes } from '../Attributes';
import { event_item, widget_item_data } from '../../../data/data';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('search_node')
export class search_node extends Component {
    @property(Node) btn_close:Node = null;
    @property(Node) content:Node = null;
    @property(Prefab) item_prefab:Prefab = null;
    @property(Node) scrollView:Node = null;
    private m_event_item: event_item;
    private m_id:number = 0;
    public show(eventItem: event_item){
        this.m_event_item = eventItem
        let search = eventItem.event_id.toString()
        this.node.active = true;
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.close()
        })
        this.m_id = parseInt(search)
        this.content.removeAllChildren()
        let search_data = this.getCurSearchType()
        let item = instantiate(this.item_prefab)
        item.parent = this.content;
        item.getComponent(Label).color = Color.MAGENTA
        if(search_data==null){
            item.getComponent(Label).string = "当前搜索的id不存在"
        }else{
            if(search_data.type==0){
                item.getComponent(Label).string = `您查找的是控件.\n 控件id：${search_data.widget.att.id}.控件名称：${search_data.widget.name}`
                this.finAboutWidgetList(search_data.widget)
            }else{
                item.getComponent(Label).string = `您查找的是事件.\n 事件id：${search_data.event.event_id}.事件名称：${search_data.event.event_name}`
                this.finAboutEventList(search_data.event)
            }
        }
        this.scrollView.off(Node.EventType.TOUCH_MOVE)
        // this.scrollView.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
        //     let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
        //     let n_p = this.scrollView.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
        //     this.scrollView.position = n_p;
        // })
        if(search_data!=null) {
            this.showBinding(search_data)
        }
        this.showNextSuccessTrigger()
    }

    private showBinding(search_data) {
        let c_event_item:event_item = search_data.event
        if(c_event_item==null){
            return
        }
        let c_title = ""
        let c_text = ""
        switch (c_event_item.type) {
            case config.event_type.play_ani: //0播放动画
            c_title = '播放动画'
            c_text = `${c_event_item.event_item_play_ani_data.binding_widget_id}`
            break;
            case config.event_type.top_view: //1弹窗
            c_title = '弹窗'
            c_text = `${c_event_item.event_item_top_view_data.binding_ui_id}`
            break;
            case config.event_type.text_sound: //2出现文字及语音
            c_title = '出现文字及语音'
            c_text = `${c_event_item.event_item_text_sound_data.binding_widget_id}`
            break;
            case config.event_type.change_one_item_status: //3改变指定道具资源
            c_title = '改变指定道具资源'
            c_text = `${c_event_item.event_item_change_one_item_status_data.binding_widget_id}`
            break;
            case config.event_type.show_new_item://4出现新道具
            c_title = '出现新道具'
            c_text = `${c_event_item.event_item_show_new_item_data.binding_widget_id}`
            break;
            case config.event_type.show_question_select://5出现问题选择
            c_title = '出现问题选择'
            c_text = `${c_event_item.event_item_show_question_select_data.binding_widget_id}`
            break;
            case config.event_type.start_event://6开始事件
            c_title = '开始事件'
            c_text = `${c_event_item.event_item_start_event_data.binding_event_id}`
            break;
            case config.event_type.collect_event://7收集事件 多组件完成触发
            c_title = ''
            c_text = `收集事件 多组件完成触发`
            break;
            case config.event_type.active_event://8激活
            c_title = '激活'
            c_text = `${c_event_item.event_item_active_event_data.binding_widget_id}`
            break;
            case config.event_type.stop_active_event://9停用交互
            c_title = '停用交互'
            c_text = `${c_event_item.event_item_stop_active_event_data.binding_widget_id}`
            break;
            case config.event_type.countdown_over://10倒计时结束
            c_title = '倒计时结束'
            c_text = `${c_event_item.event_item_countdown_over_data.binding_widget_id}`
            break;
            case config.event_type.be_event: //11被动触发事件
            c_title = '被动触发事件'
            c_text = `${c_event_item.event_item_be_event_data.binding_widget_id}`
            break;
            case config.event_type.hide://12触发让控件隐藏
            c_title = '触发让控件隐藏'
            c_text = `${c_event_item.event_item_hide_data.binding_widget_id}`
            break;
            case config.event_type.delay_exe_event://13延迟多久执行一个事件
            c_title = '延迟多久执行一个事件'
            c_text = `${c_event_item.event_item_delay_exe_data.binding_event_id}`
            break;
            case config.event_type.fail_event://14失败
            c_title = ''
            c_text = `失败`
            break;
            case config.event_type.play_sound://15播放一个音效
            c_title = ''
            c_text = `播放一个音效`
            break;
            case config.event_type.show_scene_title://16显示场景的标签
            c_title = '标签内容'
            c_text = `${c_event_item.event_item_show_scene_title_data.title}`
            break;
            case config.event_type.scene_ani://17场景动画
            c_title = '场景动画'
            c_text = `${c_event_item.event_item_scene_ani.ani_id}`
            break;
            case config.event_type.delete_drag_other_data://18删除其他拖拽目标的属性
            c_title = '删除其他拖拽目标的属性'
            c_text = `${c_event_item.event_item_delete_drag_other_data.widget_id}`
            break;
            case config.event_type.stop_play_loop_sound://19停止一个音效
            c_title = '停止一个音效'
            c_text = `${c_event_item.event_item_stop_play_loop_sound_data.res}`
            break;
            case config.event_type.switch_scene_event://20切换场景
            c_title = '多场景-切换场景按钮'
            let direction = c_event_item.event_item_switch_scene_event_data.direction
            let direction_str = '按钮-无'
            if(direction == config.switch_scene_page_direction.up) {
                direction_str = '按钮-上'
            } else if(direction == config.switch_scene_page_direction.down) {
                direction_str = '按钮-下'
            } else if(direction == config.switch_scene_page_direction.left) {
                direction_str = '按钮-左'
            } else if(direction == config.switch_scene_page_direction.right) {
                direction_str = '按钮-右'
            }
            let executeOnlyOnce_string = c_event_item.event_item_switch_scene_event_data.executeOnlyOnce ? '是' : '否'
            c_text = `方向:${direction_str} 页数:${c_event_item.event_item_switch_scene_event_data.binding_page_index+1} 仅执行一次:${executeOnlyOnce_string}`
            break;
            case config.event_type.more_scene_is_show_arrow_event://21多场景-是否显示箭头
            c_title = '多场景-是否显示箭头'
            c_text = c_event_item.event_item_more_scene_is_show_arrow_event_data.is_show ? '是' : '否'
            break;
            case config.event_type.more_scene_jump_assigned_page_event://22多场景-跳转指定页数事件
            c_title = '多场景-跳转指定页数事件'
            c_text = `页数:${c_event_item.event_item_more_scene_jump_assigned_page_event_data.page_number}`
            break;
            case config.event_type.event_group://100事件组
            c_title = '事件组'
            c_text = `~~~~~~~`
            break

        }   
        if(c_text!="") {
            let item = instantiate(this.item_prefab)
            item.parent = this.content;
            item.getComponent(Label).color = Color.BLACK
            item.getComponent(Label).string = "\n当前绑定-> " + c_title + ": " + c_text
        }
    }

    private showNextSuccessTrigger() {
        let text = ''
        if(this.m_event_item.success.trigger_event_id==-1) {
            text = "\n暂未设置触发下个事件的id"
        } else {
            text = `\n触发下个事件的id${this.m_event_item.success.trigger_event_id}`
        }
        let item = instantiate(this.item_prefab)
        item.parent = this.content;
        item.getComponent(Label).color = Color.RED
        item.getComponent(Label).string = text
        // let event_list = Attributes.Singleton.getEventList()
        // for (let index = 0; index < event_list.length; index++) {
        //     const element = event_list[index];
        //     if(element.event_id == this.m_event_item.success.trigger_event_id) {
                // console.log('type=',element.type)
        //         break
        //     }
        // }
    }

    finAboutWidgetList(widget:widget_item_data){
        let widget_id = widget.att.id;
        let drag_list = Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.DRAG_TYPE)
        for (let index = 0; index < drag_list.length; index++) {
            const drag_item = drag_list[index];
            if(drag_item.att.drag_data.other_widget_finish_listen_list!=undefined){
                for (let index = 0; index < drag_item.att.drag_data.other_widget_finish_listen_list.length; index++) {
                    const element = drag_item.att.drag_data.other_widget_finish_listen_list[index];
                    if(element.widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.GREEN
                        item.getComponent(Label).string = `被拖拽控件id：${drag_item.att.id}监听`
                    }
           
                }
            }
        }

        let click_list = Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.CLICK_TYPE)
        for (let index = 0; index < click_list.length; index++) {
            const drag_item = click_list[index];
            if(drag_item.att.click_data.other_widget_finish_listen_list!=undefined){
                for (let index = 0; index < drag_item.att.click_data.other_widget_finish_listen_list.length; index++) {
                    const element = drag_item.att.click_data.other_widget_finish_listen_list[index];
                    if(element.widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.GREEN
                        item.getComponent(Label).string = `被点击控件id：${drag_item.att.id}监听`
                    }
           
                }
            }
        }

        let slide_list = Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.SLIDE_TYPE)
        for (let index = 0; index < slide_list.length; index++) {
            const drag_item = slide_list[index];
            if(drag_item.att.slide_data.other_widget_finish_listen_list!=undefined){
                for (let index = 0; index < drag_item.att.slide_data.other_widget_finish_listen_list.length; index++) {
                    const element = drag_item.att.slide_data.other_widget_finish_listen_list[index];
                    if(element.widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.GREEN
                        item.getComponent(Label).string = `被滑动控件id：${drag_item.att.id}监听`
                    }
           
                }
            }
        }

        let event_list = Attributes.Singleton.getEventList()
        for (let index = 0; index < event_list.length; index++) {
            const event = event_list[index];
            switch (event.type) {
                case config.event_type.play_ani:
                    if(event.event_item_play_ani_data.binding_widget_id==widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.top_view:
                    if(event.event_item_top_view_data.binding_ui_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;


                case config.event_type.text_sound:
                    if(event.event_item_text_sound_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.change_one_item_status:
                    if(event.event_item_change_one_item_status_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.show_new_item:
                    if(event.event_item_show_new_item_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.show_question_select:
                    if(event.event_item_show_question_select_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.active_event:
                    if(event.event_item_active_event_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.stop_active_event:
                    if(event.event_item_stop_active_event_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.countdown_over:
                    if(event.event_item_countdown_over_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.be_event:
                    if(event.event_item_be_event_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.hide:
                    if(event.event_item_hide_data.binding_widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;

                case config.event_type.delete_drag_other_data:
                    if(event.event_item_delete_drag_other_data.widget_id===widget_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`
                    }
                break;
            
            }
        }
    }

    finAboutEventList(event:event_item){
        let event_id = event.event_id;
        let event_list = Attributes.Singleton.getEventList()
        for (let index = 0; index < event_list.length; index++) {
            const event = event_list[index];
            if(event.success.trigger_event_id==event_id){
                let item = instantiate(this.item_prefab)
                item.parent = this.content;
                item.getComponent(Label).color = Color.BLUE
                item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定`   
            }
            switch (event.type) {
                case config.event_type.show_question_select:
                    let question_list = event.event_item_show_question_select_data.question_list;
                    for (let index = 0; index < question_list.length; index++) {
                        const i = question_list[index];
                        if(i.binding_event_id===event_id){
                            let item = instantiate(this.item_prefab)
                            item.parent = this.content;
                            item.getComponent(Label).color = Color.BLUE
                            item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定` 
                        }
                    }
                break;
                case config.event_type.start_event:

                    if(event_id ===event.event_item_start_event_data.binding_event_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.BLUE
                        item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定` 
                    }
                break;
                case config.event_type.collect_event:
                    let event_id_list = event.event_item_collect_event_data.event_id_list
                    for (let index = 0; index < event_id_list.length; index++) {
                        const element = event_id_list[index];
                        if(element===event_id){
                            let item = instantiate(this.item_prefab)
                            item.parent = this.content;
                            item.getComponent(Label).color = Color.BLUE
                            item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定` 
                        }
                    }
                break;
                case config.event_type.delay_exe_event:
                    if(event.event_item_delay_exe_data.binding_event_id==event_id){
                        let item = instantiate(this.item_prefab)
                            item.parent = this.content;
                            item.getComponent(Label).color = Color.BLUE
                            item.getComponent(Label).string = `${event.event_name}-id：${event.event_id}绑定` 
                    }
                break;
            }
        }


        let drag_list = Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.DRAG_TYPE)
        for (let index = 0; index < drag_list.length; index++) {
            const drag_item = drag_list[index];
            if(drag_item.att.drag_data.other_widget_finish_listen_list!=undefined){
                for (let index = 0; index < drag_item.att.drag_data.other_widget_finish_listen_list.length; index++) {
                    const element = drag_item.att.drag_data.other_widget_finish_listen_list[index];
                    if(element.event_id===event_id){
                        let item = instantiate(this.item_prefab)
                        item.parent = this.content;
                        item.getComponent(Label).color = Color.GREEN
                        item.getComponent(Label).string = `被拖拽控件id：${drag_item.att.id}调用`
                    }
           
                }
            }
        }


        let top_list = Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.UI_TOP)
        for (let index = 0; index < top_list.length; index++) {
            const top_item = top_list[index];
            if(top_item.att.top_data.close_event_id===event_id){
                let item = instantiate(this.item_prefab)
                item.parent = this.content;
                item.getComponent(Label).color = Color.GREEN
                item.getComponent(Label).string = `被弹窗件id：${top_item.att.id}调用`
            }
            if(top_item.att.top_data.finish_event_id===event_id){
                let item = instantiate(this.item_prefab)
                item.parent = this.content;
                item.getComponent(Label).color = Color.GREEN
                item.getComponent(Label).string = `被弹窗件id：${top_item.att.id}调用`
            }
            if(top_item.att.top_data.fail_event_id===event_id){
                let item = instantiate(this.item_prefab)
                item.parent = this.content;
                item.getComponent(Label).color = Color.GREEN
                item.getComponent(Label).string = `被弹窗件id：${top_item.att.id}调用`
            }
        }

    }


    public getCurSearchType(id:number=this.m_id){
        let widget_list =  Attributes.Singleton.get_cur_scene_all_widget()
        let widget:widget_item_data = null;
        for (let index = 0; index < widget_list.length; index++) {
            const element = widget_list[index];
            if(id==element.att.id){
                widget = element;
                break;
            }
        }
        let event:event_item = null;
        if(widget==null){
            let event_list = Attributes.Singleton.getEventList()
            for (let index = 0; index < event_list.length; index++) {
                const element = event_list[index];
                if(id==element.event_id){
                    event = element;
                    break;
                }
            }
            if(event!=null){
                return {"event":event,"type":1}
            }
        }else{
            return {"widget":widget,"type":0}
        }
        return null;
      
    }

    public close(){
        this.node.active = false;
    }
}


