import { _decorator, Component, Label, Node, Toggle } from 'cc';
import { att_top_data, event_item, scene_item_data } from '../../../data/data';
import { tools } from '../../tools';
import { config } from '../../config';
import { att_widget_set_view } from '../uiWidget/att_widget_set_view';
import { att_res_set_view } from '../uiWidget/att_res_set_view';
import { single_ui_widget_info } from '../uiWidget/single_ui_widget_info';
import { Attributes } from '../Attributes';
const { ccclass, property } = _decorator;

@ccclass('attributes_top')
export class attributes_top extends Component {
    @property(Node) check_select_open_jump:Node = null;
    @property(Node) check_select_open_close:Node = null;
    @property(Node) btn_select_type:Node = null;
    @property(Node) lab_select_type:Node = null;
    @property(Node) btn_set_res:Node = null;
    @property(Node) btn_set_att:Node = null;
    @property(Node) set_att_view:Node = null;
    @property(Node) set_res_view:Node = null;
    @property(Node) btn_set_res_close:Node = null;
    @property(Node) btn_set_att_close:Node = null;
    @property(Node) close_att_node:Node = null;
    @property(Node) jump_att_node:Node = null;
    @property(Node) btn_add_child_scene:Node = null;
    @property(Node) lab_scene:Node = null;
    //event
    @property(Node) btn_close_event:Node = null;
    @property(Node) btn_finish_event:Node = null;
    @property(Node) btn_fail_event:Node = null;

    @property(Node) lab_close_event:Node = null;
    @property(Node) lab_finish_event:Node = null;
    @property(Node) lab_fail_event:Node = null;

    @property(Node) clear_close_event:Node = null;
    @property(Node) clear_finish_event:Node = null;
    @property(Node) clear_fail_event:Node = null;

    private call_back = null;
    private m_data:att_top_data = null;
    public initView(call){
        this.call_back = call;
        this.check_select_open_jump.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.is_open_jump =!this.m_data.is_open_jump;
            this.change()
        })
        this.check_select_open_close.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.is_open_close =!this.m_data.is_open_close;
            this.change()
        })

        this.close_att_node.getComponent(single_ui_widget_info).initView((data)=>{
            this.m_data.close_info = data;
            this.change()
        })
        this.jump_att_node.getComponent(single_ui_widget_info).initView((data)=>{
            this.m_data.jump_info = data;
            this.change()
        })

        this.btn_close_event.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.close_event_id = data.event_id;
                this.change()
                this.update_att(this.m_data)
            })
        })

        this.btn_finish_event.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.finish_event_id = data.event_id;
                this.change()
                this.update_att(this.m_data)
            })
        })

        this.btn_fail_event.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.fail_event_id = data.event_id;
                this.change()
                this.update_att(this.m_data)
            })
        })

        this.clear_close_event.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.close_event_id = -1;
            this.change()
            this.update_att(this.m_data)
        })

        this.clear_finish_event.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.finish_event_id = -1;
            this.change()
            this.update_att(this.m_data)
        })

        this.clear_fail_event.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.fail_event_id = -1;
            this.change()
            this.update_att(this.m_data)
        })


        this.btn_select_type.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_data.top_ui_type===config.top_view_type.__null){
                tools.show_select_ui_top_type((type)=>{
                    if( this.m_data.top_ui_type != type){
                        // this.m_data.clearAll()
                    }
                    this.m_data.top_ui_type = type;
                    this.change()
                    this.update_att(this.m_data)
                })
            }
        })

        this.btn_set_res.on(Node.EventType.TOUCH_END,()=>{
            this.set_att_view.active = false;
            this.set_res_view.active = true;
        })

        this.btn_set_att.on(Node.EventType.TOUCH_END,()=>{
            this.set_att_view.active = true;
            this.set_res_view.active = false;
        })

        this.btn_set_res_close.on(Node.EventType.TOUCH_END,()=>{
            this.set_res_view.active = false;
        })

        this.btn_set_att_close.on(Node.EventType.TOUCH_END,()=>{
            this.set_att_view.active = false;
        })

        this.btn_add_child_scene.on(Node.EventType.TOUCH_END,()=>{
            Attributes.Singleton.ShowALlChildScene((scene:scene_item_data)=>{
                this.m_data._interact_scene_data.scene = scene;
                this.change()
                this.update_att(this.m_data)
            })
        })

        
    }
    
    change(){
        if(this.call_back!=null){
            this.call_back(this.m_data)
        }
    }

    public update_att(data:att_top_data){
        this.m_data = data;

        this.close_att_node.getComponent(single_ui_widget_info).updateInfo(data.close_info)
        this.jump_att_node.getComponent(single_ui_widget_info).updateInfo(data.jump_info)
        this.lab_select_type.getComponent(Label).string = config.top_view_type_map.get(this.m_data.top_ui_type)
        
        this.check_select_open_jump.getComponent(Toggle).isChecked = this.m_data.is_open_jump;
        this.check_select_open_close.getComponent(Toggle).isChecked = this.m_data.is_open_close;
        if(this.m_data.top_ui_type===config.top_view_type.__null){
            this.btn_set_res.active = false;
            this.btn_set_att.active = false;
            this.btn_select_type.active = true;
        }else{
            this.btn_set_res.active = true;
            this.btn_set_att.active = true;
            this.btn_select_type.active = false;
            this.set_att_view.getComponent(att_widget_set_view).updateStatus(this.m_data)
            this.set_res_view.getComponent(att_res_set_view).updateStatus(this.m_data)
        }
        if(this.m_data.top_ui_type==config.top_view_type.interact_scene){
            this.btn_add_child_scene.active = true
            this.btn_set_res.active = false;
            this.btn_set_att.active = false;
            this.lab_scene.getComponent(Label).string = this.m_data._interact_scene_data.scene ?`当前的子场景名字：${this.m_data._interact_scene_data.scene.scene_diy_name}`:"添加子场景"
        }else{
            this.btn_add_child_scene.active = false
        }
        this.lab_close_event.getComponent(Label).string = this.m_data.close_event_id!=-1?`绑定的id${this.m_data.close_event_id}`:"无"
        this.lab_finish_event.getComponent(Label).string = this.m_data.finish_event_id!=-1?`绑定的id${this.m_data.finish_event_id}`:"无"
        this.lab_fail_event.getComponent(Label).string = this.m_data.fail_event_id!=-1?`绑定的id${this.m_data.fail_event_id}`:"无"
    }
}


