import { _decorator, Component, EditBox, instantiate, Label, Node, Prefab } from 'cc';
import { att_click_data, other_widget_finish_listen_item } from '../../../data/data';
import { config } from '../../config';
import { tools } from '../../tools';
import { other_widget_finish_item } from './other_widget_finish_item';
const { ccclass, property } = _decorator;

@ccclass('attributes_click')
export class attributes_click extends Component {
    @property(Node) btn_finish_event:Node = null;
    @property(Node) btn_click_type:Node = null;
    @property(Node) lab_click_type:Node = null;
    @property(Node) click_hold_time:Node = null;
    @property(Node) multiple_clicks_num:Node = null;

    @property(EditBox) edit_click_hold_time:EditBox = null;
    @property(EditBox) edit_multiple_clicks_num:EditBox = null;

    @property(Node) btn_add_listen_other_widget_finish_event:Node = null;
    @property(Prefab) other_widget_finish_item:Prefab = null;
    @property(Node) other_widget_finish_content:Node = null;
    private call_back = null;
    private m_data:att_click_data = null;
    public initView(call){
        this.call_back = call;
        this.btn_click_type.on(Node.EventType.TOUCH_END,()=>{
            tools.show_click_type_select((type:number)=>{
                this.m_data.click_type = type;
                this.update_att(this.m_data)
                this.change()
            })
        })
    }

    protected start(): void {
        this.edit_click_hold_time.node.on('editing-did-ended', this.change, this);
        this.edit_multiple_clicks_num.node.on('editing-did-ended', this.change, this);
        this.btn_add_listen_other_widget_finish_event.on(Node.EventType.TOUCH_END,()=>{
            this.addOtherWidgetFinishListenItem()
        })
    }
    addOtherWidgetFinishListenItem(){
        let item = instantiate(this.other_widget_finish_item)
        item.parent = this.other_widget_finish_content;
        let item_data = new other_widget_finish_listen_item;
        this.m_data.other_widget_finish_listen_list.push(item_data)
        item.getComponent(other_widget_finish_item).initView(this.m_data.other_widget_finish_listen_list[this.m_data.other_widget_finish_listen_list.length-1],this.onOtherWidgetFinishListenItemClickDelete.bind(this))
        this.change()
    }

    onOtherWidgetFinishListenItemClickDelete(data:other_widget_finish_listen_item){
        let index = this.m_data.other_widget_finish_listen_list.indexOf(data)
        this.m_data.other_widget_finish_listen_list.splice(index,1)
        this.change()
    }
    change(){
        if(this.call_back!=null){
            this.m_data.hold_time =  parseInt(this.edit_click_hold_time.string)
            this.m_data.multiple_check_num =  parseInt(this.edit_multiple_clicks_num.string)
            this.call_back(this.m_data)
        }
    }

    public update_att(data:att_click_data){
        this.m_data = data;
        this.edit_click_hold_time.string = this.m_data.hold_time .toString()
        this.edit_multiple_clicks_num.string = this.m_data.multiple_check_num .toString()
        if(this.m_data.click_type===config.clcik_type.click){
            this.click_hold_time.active = false;
            this.multiple_clicks_num.active = false;
        }else if(this.m_data.click_type===config.clcik_type.Press_and_hold){
            this.multiple_clicks_num.active = false;
            this.click_hold_time.active = true;
        }else if(this.m_data.click_type===config.clcik_type.Multiple_clicks){
            this.multiple_clicks_num.active = true;
            this.click_hold_time.active = false;
        }
        if(this.m_data.other_widget_finish_listen_list==undefined){
            this.m_data.other_widget_finish_listen_list = []
        }
        this.lab_click_type.getComponent(Label).string =config.clcik_type_map.get(this.m_data.click_type)
        this.other_widget_finish_content.removeAllChildren()
        for (let index = 0; index < this.m_data.other_widget_finish_listen_list.length; index++) {
            let item = instantiate(this.other_widget_finish_item)
            item.parent = this.other_widget_finish_content;
            let item_data = this.m_data.other_widget_finish_listen_list[index]
            item.getComponent(other_widget_finish_item).initView(item_data,this.onOtherWidgetFinishListenItemClickDelete.bind(this))
        }
    }
}


