import { _decorator, Color, Component, EditBox, Label, Node, Sprite } from 'cc';
import { scene_item_data } from '../../data/data';
import { tools } from '../tools';
const { ccclass, property } = _decorator;

@ccclass('scene_select_list_item')
export class scene_select_list_item extends Component {
    @property(Node) lab_name:Node = null;
    @property(Node) btn_up:Node = null;
    @property(Node) btn_down:Node = null;
    @property(Node) btn_copy:Node = null;
    @property(Node) btn_modify_name:Node = null;

    private m_data:scene_item_data = null;
    private m_is_select:boolean = false;
    private m_call_back = null;
    private m_dataListLength:number = 0;
    private m_index:number = 0;
    private m_move_call_back = null;
    private m_copy_call_back = null;

    protected start(): void {
        this.node.on(Node.EventType.TOUCH_END,()=>{
            if( this.m_call_back!=null){
                this.m_call_back(this)
            }
        })
        this.btn_up.on(Node.EventType.TOUCH_END, ()=> {
            if(this.m_move_call_back!=null) {
                this.m_move_call_back(this,true)
            }
        })
        this.btn_down.on(Node.EventType.TOUCH_END, ()=> {
            if(this.m_move_call_back!=null) {
                this.m_move_call_back(this,false)
            }
        })
        this.btn_copy.on(Node.EventType.TOUCH_END, ()=> {
            if(this.m_copy_call_back!=null) {
                this.m_copy_call_back(this)
            }
        })
        this.btn_modify_name.on(Node.EventType.TOUCH_END, ()=>{
            tools.show_dialog_input(this.m_data.scene_diy_name,(str)=>{
                this.m_data.scene_diy_name = str
                this.lab_name.getComponent(Label).string = this.m_data.scene_diy_name
            })
        })
    }

    public initView(dataListLength:number,data:scene_item_data,call,index){
        this.m_dataListLength = dataListLength
        this.m_data = data;
        this.m_call_back  = call;
        this.m_call_back = call;
        this.m_index = index;
        this.lab_name.getComponent(Label).string = this.m_data.scene_diy_name
        this.updatSelectStatus()
        this.updateUpDownBtnStatus()
    }


    public moveCallback(callback:Function) {
        this.m_move_call_back = callback
    }

    public copyCallback(callback:Function) {
        this.m_copy_call_back = callback
    }
    
    getIndex(){
        return this.m_index;
    }

    setIsSelect(isSelect){
        this.m_is_select = isSelect;
    }

    updatSelectStatus(){
        if(this.m_is_select){
            this.node.getComponent(Sprite).color = Color.YELLOW;
        }else{
            this.node.getComponent(Sprite).color = Color.GRAY;
        }
    }

    updateUpDownBtnStatus() {
        if(this.m_index==0) {
            this.btn_up.active = false
        } else {
            this.btn_up.active = true
        }

        if(this.m_index >= this.m_dataListLength - 1) {
            this.btn_down.active = false
        } else {
            this.btn_down.active = true
        }
    }

    public getSelect(){
        return this.m_is_select;
    }
    public getData(){
        return this.m_data;
    }
}


