import { _decorator, Component, Label, Node, Toggle } from 'cc';
import { event_item, event_item_change_one_item_status, event_item_delay_exe_event } from '../../../data/data';
import { config } from '../../config';
import { event_play_ani_view } from './event_play_ani_view';
import { tools } from '../../tools';
import { event_top_view_view } from './event_top_view_view';
import { event_text_sound } from './event_text_sound';
import { event_change_one_item_status } from './event_change_one_item_status';
import { event_show_new_item } from './event_show_new_item';
import { event_show_question_select } from './event_show_question_select';
import { event_countdown_over } from './event_countdown_over';
import { Attributes } from '../Attributes';
import { event_start_event } from './event_start_event';
import { event_active_event } from './event_active_event';
import { event_collect_event } from './event_collect_event';
import { event_be_event } from './event_be_event';
import { event_hide } from './event_hide';
import { event_delay_exe_event } from './event_delay_exe_event';
import { event_fail_event } from './event_fail_event';
import { event_play_sound } from './event_play_sound';
import { event_show_scene_title } from './event_show_scene_title';
import { event_scene_ani } from './event_scene_ani';
import { event_delete_drag_other_data_view } from './event_delete_drag_other_data_view';
import { event_stop_play_loop_sound } from './event_stop_play_loop_sound';
import { event_stop_active_event } from './event_stop_active_event';
import { event_switch_scene_event } from './event_switch_scene_event';
import { event_more_scene_jump_assigned_page_event } from './event_more_scene_jump_assigned_page_event';
import { event_more_scene_is_show_arrow_event } from './event_more_scene_is_show_arrow_event';
import { event_group } from './event_group';
const { ccclass, property } = _decorator;

@ccclass('edit_event')
export class edit_event extends Component {
    @property(Node) play_ani:Node = null;
    @property(Node) top_view:Node = null;
    @property(Node) text_sound:Node = null;
    @property(Node) change_one_item_status:Node = null;
    @property(Node) show_new_item:Node = null;
    @property(Node) btn_delete:Node = null;

    @property(Node) show_question_select:Node =null;
    @property(Node) countdown_over:Node = null;

    @property(Node) active_event:Node = null;
    @property(Node) stop_active_event:Node = null;
    @property(Node) start_event:Node =null;
    @property(Node) collect_event:Node = null;
    @property(Node) be_event:Node = null;
    @property(Node) hide:Node = null;
    @property(Node) delay_exe_event:Node = null;
    @property(Node) switch_scene_event:Node = null;
    @property(Node) more_scene_is_show_arrow_event:Node = null;
    @property(Node) more_scene_jump_assigned_page_event:Node = null;
    @property(Node) event_group:Node = null; 

    @property(Node) scene_ani:Node = null;

    @property(Node) delete_drag_other_data:Node = null;

    @property(Node) fail_event:Node = null;
    @property(Node) play_sound:Node = null;
    @property(Node) stop_play_loop_sound:Node = null;

    @property(Node) show_scene_title:Node = null;

    @property(Node) is_finish_game:Node = null;
    @property(Node) is_next_scene:Node = null;
    @property(Node) btn_finish_trigger_event_id:Node = null;
    @property(Node) lab_finish_trigger_event_id:Node = null;
    
    @property(Node) is_restart:Node = null;
    @property(Node) is_relife:Node = null;
    @property(Node) btn_fail_trigger_event_id:Node = null;
    @property(Node) lab_fail_trigger_event_id:Node = null;

    @property(Node) btn_set_next_event:Node = null;
    @property(Node) lab_set_next_event:Node = null;

    @property(Node) btn_fail:Node = null;
    @property(Node) btn_win:Node = null;
    @property(Node) lab_event_name:Node = null;

    @property(Node) btn_close:Node = null;

    @property(Node) btn_clear_event:Node = null;

    private widget_map:Map<number,Node> = new Map;
    private m_data:event_item = null;
    private m_type:number = 0;
    private m_delete_call = null;
    public show(type:number,data:event_item,delete_call){
        this.m_data = data;
        this.m_type = type;
        this.m_delete_call = delete_call;
        this.initMap()
        this.initEventView()
        this.lab_event_name.getComponent(Label).string = `当前事件名字${this.m_data.event_name}`
        this.m_delete_call = delete_call;
        this.btn_delete.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_delete_call !=null){
                tools.show_dialog("是否删除选中的事件？",()=>{
                    this.m_delete_call(this.m_data)
                    this.node.destroy()
                    this.node.removeFromParent()
                })
            }
        })
        this.btn_clear_event.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.success.trigger_event_id = -1;
            this.lab_finish_trigger_event_id.getComponent(Label).string = "暂未设置触发下个事件的id"
        })
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.node.removeFromParent()
        })

        // this.lab_event_id.getComponent(Label).string = `当前事件id：${this.m_data.event_id}` 
        
        this.is_finish_game.getComponent(Toggle).isChecked = this.m_data.success.is_finish_game;
        this.is_next_scene.getComponent(Toggle).isChecked = this.m_data.success.is_next_scene;
        this.lab_finish_trigger_event_id.getComponent(Label).string = this.m_data.success.trigger_event_id==-1?"暂未设置触发下个事件的id":`触发下个事件的id${this.m_data.success.trigger_event_id}`
     
        
        this.is_relife.getComponent(Toggle).isChecked = this.m_data.fail.is_relife;
        this.is_restart.getComponent(Toggle).isChecked = this.m_data.fail.is_restart;
        this.lab_fail_trigger_event_id.getComponent(Label).string = this.m_data.fail.trigger_event_id==-1?"暂未设置失败事件的id":`败事件的id":${this.m_data.fail.trigger_event_id}`
    
        this.is_finish_game.on('toggle',this.change.bind(this))
        this.is_next_scene.on('toggle',this.change.bind(this))
        this.is_relife.on('toggle',this.change.bind(this))
        this.is_restart.on('toggle',this.change.bind(this))

        this.btn_fail_trigger_event_id.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.fail.trigger_event_id = data.event_id;
                this.lab_fail_trigger_event_id.getComponent(Label).string = this.m_data.fail.trigger_event_id==-1?"暂未设置失败事件的id":`败事件的id":${this.m_data.fail.trigger_event_id}`
            })
        })
        this.btn_finish_trigger_event_id.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.success.trigger_event_id = data.event_id;
                this.lab_finish_trigger_event_id.getComponent(Label).string = this.m_data.success.trigger_event_id==-1?"暂未设置触发下个事件的id":`触发下个事件的id${this.m_data.success.trigger_event_id}`
            })
        })

        this.btn_set_next_event.on(Node.EventType.TOUCH_START,()=>{
            if(this.btn_win.active==false){
                this.btn_fail.active = true;
                this.btn_win.active = true;
                this.lab_set_next_event.getComponent(Label).string ="收起"
                if(this.m_type ==config.event_type.switch_scene_event) {
                    this.btn_fail.active = false;
                    this.btn_win.getChildByName('is_finish_game').active = false
                    this.btn_win.getChildByName('is_next_scene').active = false
                }
            }else{
                this.btn_fail.active = false;
                this.btn_win.active = false; 
                this.lab_set_next_event.getComponent(Label).string ="设置后续关联"
            }
        })
    }
    change(){
        this.m_data.success.is_finish_game = this.is_finish_game.getComponent(Toggle).isChecked;
        this.m_data.success.is_next_scene = this.is_next_scene.getComponent(Toggle).isChecked;
        this.m_data.fail.is_relife = this.is_relife.getComponent(Toggle).isChecked;
        this.m_data.fail.is_restart = this.is_restart.getComponent(Toggle).isChecked;
    }
    initMap(){
        this.widget_map.set(config.event_type.play_ani,this.play_ani)
        this.widget_map.set(config.event_type.top_view,this.top_view)
        this.widget_map.set(config.event_type.text_sound,this.text_sound)
        this.widget_map.set(config.event_type.change_one_item_status,this.change_one_item_status)
        this.widget_map.set(config.event_type.show_new_item,this.show_new_item)
        this.widget_map.set(config.event_type.show_question_select,this.show_question_select)
        this.widget_map.set(config.event_type.countdown_over,this.countdown_over)

        this.widget_map.set(config.event_type.start_event,this.start_event)
        this.widget_map.set(config.event_type.collect_event,this.collect_event)
        this.widget_map.set(config.event_type.active_event,this.active_event)
        this.widget_map.set(config.event_type.stop_active_event,this.stop_active_event)
        this.widget_map.set(config.event_type.be_event,this.be_event)
        this.widget_map.set(config.event_type.hide,this.hide)
        this.widget_map.set(config.event_type.delay_exe_event,this.delay_exe_event)
        this.widget_map.set(config.event_type.switch_scene_event,this.switch_scene_event)
        this.widget_map.set(config.event_type.more_scene_is_show_arrow_event,this.more_scene_is_show_arrow_event)
        this.widget_map.set(config.event_type.more_scene_jump_assigned_page_event,this.more_scene_jump_assigned_page_event)

        this.widget_map.set(config.event_type.fail_event,this.fail_event)
        this.widget_map.set(config.event_type.play_sound,this.play_sound)
        this.widget_map.set(config.event_type.show_scene_title,this.show_scene_title)
        this.widget_map.set(config.event_type.scene_ani,this.scene_ani)
        this.widget_map.set(config.event_type.delete_drag_other_data,this.delete_drag_other_data)
        this.widget_map.set(config.event_type.stop_play_loop_sound,this.stop_play_loop_sound)

        this.widget_map.set(config.event_type.event_group,this.event_group)
    }
    hideAllWidget(){
        this.widget_map.forEach((v,k)=>{
            v.active = false;
        })
    }


    initEventView(){
        this.hideAllWidget()
        this.widget_map.get(this.m_type).active = true;
        console.log("initEventView",this.m_type)
        switch(this.m_type){
            case config.event_type.play_ani:
                this.play_ani.getComponent(event_play_ani_view).initView(this.m_data.event_item_play_ani_data)
            break;
            case config.event_type.top_view:
                this.top_view.getComponent(event_top_view_view).initView(this.m_data.event_item_top_view_data)
            break;
            case config.event_type.text_sound:
                this.text_sound.getComponent(event_text_sound).initView(this.m_data.event_item_text_sound_data)
            break;
            case config.event_type.change_one_item_status:
                this.change_one_item_status.getComponent(event_change_one_item_status).initView(this.m_data.event_item_change_one_item_status_data)
            break;
            case config.event_type.show_new_item:
                this.show_new_item.getComponent(event_show_new_item).initView(this.m_data.event_item_show_new_item_data)
            break;
            case config.event_type.show_question_select:
                this.show_question_select.getComponent(event_show_question_select).initView(this.m_data.event_item_show_question_select_data)
            break;
            case config.event_type.countdown_over:
                this.countdown_over.getComponent(event_countdown_over).initView(this.m_data.event_item_countdown_over_data)
            break;
            case config.event_type.start_event: //开始事件
                this.start_event.getComponent(event_start_event).initView(this.m_data.event_item_start_event_data)
            break;
            case config.event_type.collect_event: //收集多个事件
                this.collect_event.getComponent(event_collect_event).initView(this.m_data.event_item_collect_event_data,this.m_data.event_id)
            break;
            case config.event_type.active_event: //激活一个控件
                this.active_event.getComponent(event_active_event).initView(this.m_data.event_item_active_event_data)
            break;
            case config.event_type.stop_active_event: //不激活一个控件,停用交互
                 this.stop_active_event.getComponent(event_stop_active_event).initView(this.m_data.event_item_stop_active_event_data)
            break;
            case config.event_type.be_event: //被动激活一个事件
                this.be_event.getComponent(event_be_event).initView(this.m_data.event_item_be_event_data)
            break;
            case config.event_type.hide: //被动激活一个事件
                this.hide.getComponent(event_hide).initView(this.m_data.event_item_hide_data)
            break;
            case config.event_type.delay_exe_event: //被动激活一个事件
                this.delay_exe_event.getComponent(event_delay_exe_event).initView(this.m_data.event_item_delay_exe_data)
            break;
            case config.event_type.fail_event: //失败事件
                this.fail_event.getComponent(event_fail_event).initView(this.m_data.event_item_fail_event_data)
            break;
            case config.event_type.play_sound: //播放一个音效
                this.play_sound.getComponent(event_play_sound).initView(this.m_data.event_item_play_sound_data)
            break;
            case config.event_type.show_scene_title: //显示一个场景标签
                this.show_scene_title.getComponent(event_show_scene_title).initView(this.m_data.event_item_show_scene_title_data)
            break;
            case config.event_type.scene_ani: //
                this.scene_ani.getComponent(event_scene_ani).initView(this.m_data.event_item_scene_ani)
            break;
            case config.event_type.delete_drag_other_data: //
                this.delete_drag_other_data.getComponent(event_delete_drag_other_data_view).initView(this.m_data.event_item_delete_drag_other_data)
            break;
            case config.event_type.stop_play_loop_sound: //
                this.stop_play_loop_sound.getComponent(event_stop_play_loop_sound).initView(this.m_data.event_item_stop_play_loop_sound_data)
            break;
            case config.event_type.switch_scene_event://多场景-切换场景事件
                this.switch_scene_event.getComponent(event_switch_scene_event).initView(this.m_data.event_item_switch_scene_event_data)
            break;
            case config.event_type.more_scene_is_show_arrow_event://多场景-是否显示箭头事件
                this.more_scene_is_show_arrow_event.getComponent(event_more_scene_is_show_arrow_event).initView(this.m_data.event_item_more_scene_is_show_arrow_event_data)
            break;
            case config.event_type.more_scene_jump_assigned_page_event://多场景-跳转指定页数事件
                this.more_scene_jump_assigned_page_event.getComponent(event_more_scene_jump_assigned_page_event).initView(this.m_data.event_item_more_scene_jump_assigned_page_event_data)
            break;
            case config.event_type.event_group://事件组
                this.event_group.getComponent(event_group).initView(this.m_data, this.m_data.event_item_event_group_data)
            break;
        }
    }

}


