import { _decorator, Component, EditBox, instantiate, Node, Prefab, Vec3 } from 'cc';
import { config } from '../../config';
import { edit_event_view_select_btn } from './edit_event_view_select_btn';
import { Attributes } from '../Attributes';
import { event_item } from '../../../data/data';
import { event_item_view } from './event_item_view';
import { control } from '../control';
import { main } from '../../main';
import { ClientEvent } from '../../clientEvent';
import { tools } from '../../tools';
const { ccclass, property } = _decorator;

@ccclass('edit_event_view')
export class edit_event_view extends Component {
    @property(Prefab) event_item_prefab:Prefab = null;
    @property(Node) btn_close:Node = null;
    @property(Node) content:Node = null;
    @property(Node) type_content:Node = null;
    @property(Node) type_btn:Node = null;
    @property(Node) search_node:Node = null;
    @property(Node) search_id_editbox:Node = null;
    @property(Node) search_id_btn:Node = null;
    @property(Node) search_content_editbox:Node = null;
    @property(Node) search_content_btn:Node = null;
    private m_cur_select_type:number = -1;
    private m_search_item_list:event_item[] = []
    protected start(): void {
        this.search_id_btn.on(Node.EventType.TOUCH_END, ()=> {
            let id_string = tools.trimLeftAndRightblank(this.search_id_editbox.getComponent(EditBox).string)
            this.search_id_editbox.getComponent(EditBox).string = id_string
            if(id_string.length<=0) {
                return tools.showToast('请输入id')
            }
            this.searchAction(id_string,true)
        },this)
        this.search_content_btn.on(Node.EventType.TOUCH_END, ()=> {
            let content_string = tools.trimLeftAndRightblank(this.search_content_editbox.getComponent(EditBox).string)
            this.search_content_editbox.getComponent(EditBox).string = content_string
            if(content_string.length<=0) {
                return tools.showToast('请输入内容')
            }
            this.searchAction(content_string,false)
        },this)
    }
    public show(){
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.node.removeFromParent()
        })
        this.type_content.removeAllChildren()

        let btn = instantiate(this.type_btn)
        btn.getComponent(edit_event_view_select_btn).initView(-1,"全部",this.onTypeItemClick.bind(this))
        btn.getComponent(edit_event_view_select_btn).selectStatus()
        btn.parent = this.type_content

        let btn_search = instantiate(this.type_btn)
        btn_search.getComponent(edit_event_view_select_btn).initView(-2,"搜索",this.onTypeItemClick.bind(this))
        btn_search.getComponent(edit_event_view_select_btn).unselectStatus()
        btn_search.parent = this.type_content

        config.event_type_map.forEach((v,k)=>{
            let item = instantiate(this.type_btn)
            item.parent = this.type_content
            item.getComponent(edit_event_view_select_btn).initView(k,v,this.onTypeItemClick.bind(this))
        })
        this.m_cur_select_type = -1;
        this.updateView()
    } 

    allBtnUnSelect(){
        for (let index = 0; index < this.type_content.children.length; index++) {
            const element = this.type_content.children[index];
            element.getComponent(edit_event_view_select_btn).unselectStatus()
        }
    }

    onTypeItemClick(item:edit_event_view_select_btn){
        this.allBtnUnSelect()
        item.selectStatus()
        if(this.m_cur_select_type!=item.getType()){
            this.m_cur_select_type = item.getType()
            this.updateView()
        }
    }

    updateView(){
        this.content.removeAllChildren()

        let list;
        if(this.m_cur_select_type == -2) {
            this.search_node.active = true
            list = this.m_search_item_list
        } else {
            this.search_node.active = false
            list = this.getEventListByType()
        }
         
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let item = instantiate(this.event_item_prefab)
            item.parent = this.content;
            item.getComponent(event_item_view).initView(element.type,element,this.onDeleteItem.bind(this))
        }
    }

    public onDeleteItem(item:event_item){
        let task_data = main.Singleton.edit_scene_view.getTaskData()
        if(task_data!=null){
            for (let index = 0; index < task_data.event_list.length; index++) {
                const element = task_data.event_list[index];
                if(element.event_id===item.event_id){
                    task_data.event_list.splice(index,1)
                    this.updateView()
                    ClientEvent.dispatchEvent(config.Event.UpdateEditScene)
                    break;
                }
            }
        }
    }
    getEventListByType(){
        let list = Attributes.Singleton.getEventList()
        if(this.m_cur_select_type==-1){
            return list;
        }else{
            let temp:event_item[] = []
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                if(element.type==this.m_cur_select_type){
                    temp.push(element)
                }
            }
            return temp;
        }
    }

    searchAction(string:string, isSearchID:boolean) {
        // console.log('string=',string)
        tools.showToast('暂未开放 暂未开放 暂未开放')
        return
        this.m_search_item_list.splice(0, this.m_search_item_list.length)
        if(isSearchID) {
            this.searchData(parseInt(string))
        } else {
            let check_yes = false 
            let list = Attributes.Singleton.getEventList()
            for(let i=0; i<list.length; i++) {
                const element = list[i]
                if(element.event_name.indexOf(string)!=-1) {
                    if(element.event_id!=0) {
                        this.searchData(element.event_id)
                        check_yes = true
                    }
                    break
                }
            }
            if(check_yes==false) {
                tools.showToast('搜索的内容不存在')
            }
        }

        this.searchCreateItem()
    }

    private searchData(id:number) {
        let list = Attributes.Singleton.getEventList()
        for(let i=0; i<list.length; i++) {
            const element = list[i]
            if(element.event_id == id) {
                // console.log('触发下个事件的id=',element.success.trigger_event_id)
                this.m_search_item_list.push(element)
                let trigger_event_id = element.success.trigger_event_id
                if(trigger_event_id != -1) {
                    this.searchData(trigger_event_id)
                }
                break
            }
        }
        if(this.m_search_item_list.length == 0) {
            tools.showToast('搜索的id不存在')
        }
    }

    private searchCreateItem() {
        this.content.removeAllChildren()
        for (let index = 0; index < this.m_search_item_list.length; index++) {
            const element = this.m_search_item_list[index];
            let item = instantiate(this.event_item_prefab)
            item.parent = this.content;
            item.getComponent(event_item_view).initView(element.type,element,this.onDeleteItem.bind(this))
        }
    }

}


