import { _decorator, Component, instantiate, Label, Layers, Layout, Node, Prefab, ScrollView, Vec2, Vec3 } from 'cc';
import { tools } from '../../tools';
import { edit_scene } from '../edit_scene';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
import { da_guai_data, event_active_event_item, event_collect_event_item, event_item, event_item_be_event, event_item_change_one_item_status, event_item_countdown_over, event_item_delay_exe_event, event_item_delete_drag_other, event_item_fail, event_item_fail_event, event_item_hide, event_item_play_ani, event_item_play_sound, event_item_scene_ani, event_item_show_new_item, event_item_show_question_select, event_item_show_scene_title, event_item_stop_play_loop_sound, event_item_success, event_item_text_sound, event_item_top_view, event_stop_active_event_item, event_start_event_item, guo_ju_qing_data, scene_item_data, task_data, zhao_xi_jie_data, zhao_xi_jie_item_data, guo_ju_qing_not_rule_data, event_item_switch_scene_event, guo_ju_qing_dai_dao_ju, event_item_more_scene_is_show_arrow_event, event_item_more_scene_jump_assigned_page_event, event_group_event_item } from '../../../data/data';
import { event_item_view } from '../event/event_item_view';
import { task_zhao_xi_jie } from './task_zhao_xi_jie';
import { task_guo_ren_wu } from './task_guo_ren_wu';
import { task_da_boss } from './task_da_boss';
import { Search } from '../search/Search';
import { task_dai_dao_ju } from './task_dai_dao_ju';
const { ccclass, property } = _decorator;

@ccclass('scene_task')
export class scene_task extends Component {
    @property(Node) btn_add_task:Node = null;
    @property(Node) lab_task_title:Node = null;
    @property(Node) btn_add_event:Node = null;
    @property(Node) event_content:Node = null;
    @property(Prefab) event_item_prefab:Prefab = null;
    @property(Node) btn_update:Node = null;
    @property(Node) btn_edit_event:Node = null;
    @property(Node) task_zhao_xi_jie:Node = null;
    @property(Node) task_guo_ju_qing:Node = null;
    @property(Node) task_da_guai:Node = null;
    @property(Node) task_guo_ju_qing_dai_dao_ju:Node = null;
    @property(Node) search_node:Node = null;
    private m_edit_scene:edit_scene = null;
    private isUpdateStatus:boolean = false;
    protected start(): void {
        this.btn_add_task.on(Node.EventType.TOUCH_END,()=>{
            tools.show_add_task((type:number)=>{
                this.addTask(type)
            })
        })
        this.btn_add_event.on(Node.EventType.TOUCH_END,()=>{
            tools.show_add_event((type:number,name:string)=>{
                this.addEvent(type,name)
            })
        })
        this.btn_update.on(Node.EventType.TOUCH_END,()=>{
         
            if(!this.isUpdateStatus){
                this.isUpdateStatus = true;
                this.scheduleOnce(()=>{
                    this.UpdateEditScene()
                    this.isUpdateStatus = false;
                },1)
            }else{
                tools.showToast("正在刷新请稍后！")
            }
        })

        this.btn_edit_event.on(Node.EventType.TOUCH_END,()=>{
            tools.show_edit_event_view()
        })

        this.search_node.getComponent(Search).searchClick((id: string)=> {
            this.searchContentLocationList(true,id)
        }, (text: string)=> {
            this.searchContentLocationList(false,text)
        })
    }


    public initView(_edit_scene:edit_scene){
        this.m_edit_scene = _edit_scene;
        this.lab_task_title.getComponent(Label).string  = ""
        this.UpdateEditScene()
        ClientEvent.on(config.Event.UpdateEditScene,this.UpdateEditScene,this)
    }

    UpdateEditScene(){
        this.btn_add_task.active = false;
        this.btn_add_event.active = false;
        this.btn_update.active = false;
        this.btn_edit_event.active = false;
        this.task_zhao_xi_jie.active = false;
        this.task_guo_ju_qing.active = false;
        this.task_guo_ju_qing_dai_dao_ju.active = false;
        this.task_da_guai.active = false;
        let scene_data = this.m_edit_scene.getCurEditBaseView();
        this.event_content.removeAllChildren()
        this.lab_task_title.getComponent(Label).string = ""
        if(scene_data!=null){
            this.btn_update.active = true;
            this.btn_edit_event.active = true;
           let data:scene_item_data = scene_data.getData()
           if(!data._task_data){
                this.btn_add_task.active = true;
                this.btn_add_event.active = false;
           }else{
                this.btn_add_task.active = false;
                this.btn_add_event.active = true;
                this.lab_task_title.getComponent(Label).string = config.task_type_map.get(data._task_data.type)
                this.initSceneAllEvent()
                switch (data._task_data.type) {
                    case config.task_type.da_guai:
                        this.task_da_guai.active = true;
                        this.task_da_guai.getComponent(task_da_boss).updateView(data._task_data._da_guai)
                    break;
                    case config.task_type.guo_ju_qing:
                        this.task_guo_ju_qing.active = true;
                        this.task_guo_ju_qing.getComponent(task_guo_ren_wu).updateView(data._task_data._guo_ju_qing)
                    break;
                    case config.task_type.guo_ju_qing_not_rule:
                    break;
                    case config.task_type.zhao_xi_jie:
                        this.task_zhao_xi_jie.active = true;
                        this.task_zhao_xi_jie.getComponent(task_zhao_xi_jie).updateView(data._task_data._zhao_xi_jie_data)
                    break;
                    case config.task_type.guo_ju_qing_dai_dao_ju:
                        this.task_guo_ju_qing_dai_dao_ju.active = true;
                        this.task_guo_ju_qing_dai_dao_ju.getComponent(task_dai_dao_ju).updateView(data._task_data._guo_ju_qing_dai_dao_ju)
                    break;

                }
            }
        }


    }
    protected onEnable(): void {

    }

    initSceneAllEvent(){
        let task_data = this.getTaskData()
        for (let index = 0; index < task_data.event_list.length; index++) {
            const element = task_data.event_list[index];
            this.addEventItemView(element.type,element)
        }
    }

    public addTask(type:number){
        let scene_data = this.m_edit_scene.getCurEditBaseView();
        if(scene_data!=null){
            let data:scene_item_data = scene_data.getData()
            if(!data._task_data){
                data._task_data = new task_data
                data._task_data.type = type;
            }
            this.btn_add_task.active = false;
            this.btn_add_event.active = true;
            switch (type) {
                case config.task_type.da_guai:
                    if( data._task_data._da_guai==null){
                        data._task_data._da_guai = new da_guai_data
                    }
                    this.task_da_guai.active = true;
                    this.task_da_guai.getComponent(task_da_boss).updateView(data._task_data._da_guai)
                break;
        
                case config.task_type.guo_ju_qing:
                    if( data._task_data._guo_ju_qing==null){
                        data._task_data._guo_ju_qing = new guo_ju_qing_data
                    }
                    this.task_guo_ju_qing.active = true;
                    this.task_guo_ju_qing.getComponent(task_guo_ren_wu).updateView(data._task_data._guo_ju_qing)
                break;
                case config.task_type.guo_ju_qing_not_rule:
                    if(data._task_data._guo_ju_qing_not_rule_data==null){
                        data._task_data._guo_ju_qing_not_rule_data = new guo_ju_qing_not_rule_data
                    }
                break;
                case config.task_type.zhao_xi_jie:
                    if( data._task_data._zhao_xi_jie_data==null){
                        data._task_data._zhao_xi_jie_data = new zhao_xi_jie_data
                    }
                    this.task_zhao_xi_jie.active = true;
                    this.task_zhao_xi_jie.getComponent(task_zhao_xi_jie).updateView(data._task_data._zhao_xi_jie_data)
                break;
                case config.task_type.guo_ju_qing_dai_dao_ju:
                    if( data._task_data._guo_ju_qing_dai_dao_ju==null){
                        data._task_data._guo_ju_qing_dai_dao_ju = new guo_ju_qing_dai_dao_ju
                    }
                    this.task_guo_ju_qing_dai_dao_ju.active = true;
                    this.task_guo_ju_qing_dai_dao_ju.getComponent(task_dai_dao_ju).updateView(data._task_data._guo_ju_qing_dai_dao_ju)
                break;
            }
            this.lab_task_title.getComponent(Label).string = config.task_type_map.get(data._task_data.type)
         }
    }

    public getTaskData(){
        let scene_data = this.m_edit_scene.getCurEditBaseView();
        if(scene_data!=null){
            let data:scene_item_data = scene_data.getData()
            if(data._task_data){
                return data._task_data
            }
         }
         return null;
    }

    addEventItemView(type:number,data:event_item){
        let item = instantiate(this.event_item_prefab)
        item.parent = this.event_content
        item.getComponent(event_item_view).initView(type,data,this.onDeleteItem.bind(this))
    }

    public addEvent(type:number,name:string){
        let task_data = this.getTaskData()
        if(task_data===null){
            return tools.showToast("当前没有任务，可以添加事件！")
        }
        let new_event = new event_item;
        new_event.type = type;
        new_event.event_name = name;
        new_event.event_id = config.getNewId()
        switch(type){
            case config.event_type.play_ani:
                new_event.event_item_play_ani_data = new event_item_play_ani
            break;
            case config.event_type.top_view:
                new_event.event_item_top_view_data = new event_item_top_view
            break;
            case config.event_type.text_sound:
                new_event.event_item_text_sound_data = new event_item_text_sound
            break;
            case config.event_type.change_one_item_status:
                new_event.event_item_change_one_item_status_data = new event_item_change_one_item_status
            break;
            case config.event_type.show_new_item:
                new_event.event_item_show_new_item_data = new event_item_show_new_item
            break;
            case config.event_type.show_question_select:
                new_event.event_item_show_question_select_data = new event_item_show_question_select
            break;
            case config.event_type.collect_event:
                new_event.event_item_collect_event_data = new event_collect_event_item
            break;
            case config.event_type.start_event:
                new_event.event_item_start_event_data = new event_start_event_item
            break;
            case config.event_type.active_event:
                new_event.event_item_active_event_data= new event_active_event_item
            break;
            case config.event_type.stop_active_event:
                new_event.event_item_stop_active_event_data = new event_stop_active_event_item
            break;
            case config.event_type.countdown_over:
                new_event.event_item_countdown_over_data = new event_item_countdown_over
            break;
            case config.event_type.be_event:
                new_event.event_item_be_event_data = new event_item_be_event
            break;
            case config.event_type.hide:
                new_event.event_item_hide_data = new event_item_hide
            break;
            case config.event_type.delay_exe_event:
                new_event.event_item_delay_exe_data = new event_item_delay_exe_event
            break;
            case config.event_type.fail_event:
                new_event.event_item_fail_event_data = new event_item_fail_event
            break;
            case config.event_type.play_sound:
                new_event.event_item_play_sound_data = new event_item_play_sound
            break;
            case config.event_type.show_scene_title:
                new_event.event_item_show_scene_title_data = new event_item_show_scene_title
            break;
            case config.event_type.scene_ani:
                new_event.event_item_scene_ani = new event_item_scene_ani
            break;
            case config.event_type.delete_drag_other_data:
                new_event.event_item_delete_drag_other_data = new event_item_delete_drag_other
            break;
            case config.event_type.stop_play_loop_sound:
                new_event.event_item_stop_play_loop_sound_data = new event_item_stop_play_loop_sound
            break;
            case config.event_type.switch_scene_event:
                new_event.event_item_switch_scene_event_data = new event_item_switch_scene_event
            break;
            case config.event_type.more_scene_is_show_arrow_event:
                new_event.event_item_more_scene_is_show_arrow_event_data = new event_item_more_scene_is_show_arrow_event
            break;
            case config.event_type.more_scene_jump_assigned_page_event:
                new_event.event_item_more_scene_jump_assigned_page_event_data = new event_item_more_scene_jump_assigned_page_event
            break;
            case config.event_type.event_group:
                new_event.event_item_event_group_data = new event_group_event_item
            break;
        }
        this.addEventItemView(type,new_event)
        task_data.event_list.push(new_event)
    }

    public onDeleteItem(item:event_item){
        let task_data = this.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)
                    break;
                }
            }
        }
    }

    searchContentLocationList(isId: boolean, string: string) {        
        let task_data = this.getTaskData()
        let data_list = task_data.event_list
        let check_yes = false
        for (let index = 0; index < data_list.length; index++) {
            const element = data_list[index];
            if(isId) {
                if(element.event_id.toString() == string) {
                    check_yes = true
                }
            } else {
                if(element.event_name.indexOf(string) != -1) {
                    check_yes = true                    
                }
            }      
            if(check_yes) {
                let c_index = (data_list.length-1)-index
                let c_row = Math.round(c_index / 2)
                let c_spacing_y = c_row * 50
                let c_item_height = (c_row - 1) * 300
                let c_offset_y = c_spacing_y + c_item_height + 900
                this.node.getComponent(ScrollView).scrollToOffset(new Vec2(0, c_offset_y))
                this.search_node.getComponent(Search).showTip(element)
                break
            }     
        }
        if(check_yes==false) {
            let item = new event_item()
            this.search_node.getComponent(Search).showTip(item)
        }
    }
}


