import { _decorator, Component, EditBox, instantiate, Label, Node, Prefab } from 'cc';
import { att_slide_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_slide')
export class attributes_slide extends Component {
    @property(Node) btn_slide_dir:Node = null;
    @property(Node) lab_slide_dir:Node = null;
    @property(Node) btn_slide_distance:Node = null;
    @property(Node) btn_slide_num:Node = null;
    @property(Node) btn_finish_event:Node = null;
    @property(EditBox) slide_num_edit:EditBox = null;
    @property(EditBox) slide_distance_edit: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_slide_data = null;
    
    public initView(call){
        this.call_back = call;
        this.btn_slide_dir.on(Node.EventType.TOUCH_END,()=>{
            tools.show_slide_dir_select((dir:number)=>{
                if(this.m_data.slide_dir!=dir){
                    this.m_data.slide_dir = dir;
                    this.change()
                }
                this.update_att(this.m_data)
            })
        })
    }
    public update_att(data:att_slide_data){
        this.m_data = data;
        this.slide_num_edit.string = data.slide_num.toString()
        this.slide_distance_edit.string = data.slide_distance.toString()
        this.lab_slide_dir.getComponent(Label).string = config.slide_type_map.get(data.slide_dir)
        this.other_widget_finish_content.removeAllChildren()
        if(this.m_data.other_widget_finish_listen_list==undefined){
            this.m_data.other_widget_finish_listen_list = []
        }
        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))
        }
    }
    protected start(): void {
        this.slide_num_edit.node.on('editing-did-ended', this.change, this);
        this.slide_distance_edit.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.call_back(this.m_data.slide_dir,parseInt(this.slide_num_edit.string),parseInt(this.slide_distance_edit.string))
        }
    }
}


