import { _decorator, Component, EditBox, Node } from 'cc';
import { ui_att_item } from '../../../data/data';
import { receive_res_item } from '../uiWidget/receive_res_item';
import { receive_widget_item } from '../uiWidget/receive_widget_item';
import { tools } from '../../tools';
import { config } from '../../config';
import { ClientEvent } from '../../clientEvent';
import { Attributes } from '../Attributes';
const { ccclass, property } = _decorator;

@ccclass('question_btn_info')
export class question_btn_info extends Component {
    @property(Node) res_node:Node = null;
    @property(Node) att_node:Node = null;
    @property(Node) score_node:Node = null;
    @property(EditBox) score_editBox:EditBox = null
    @property(Node) btn_original_size:Node = null
    @property(Node) btn_delete:Node = null;
    private m_data:ui_att_item = null;
    private m_index:number = 0;
    private m_type:string = config.attributes_list_type.top;
    private m_call_back = null;
    protected start(): void {
        this.score_editBox.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=>{
            if(this.score_editBox.string.length<=0) {
                this.score_editBox.string = '0'
            }
            this.m_data.score = parseInt(this.score_editBox.string)
            this.updateScoreStatus()
        })
        this.btn_original_size.on(Node.EventType.TOUCH_END, ()=>{
            tools.show_dialog('设置原尺寸?',()=>{
                tools.loadSceneImg(this.m_data.res, (r)=>{
                    this.m_data.width = r.sf.originalSize.width
                    this.m_data.height = r.sf.originalSize.height
                    this.att_node.getComponent(receive_widget_item).updateView(this.m_data,this.m_type)
                    ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),this.m_type)
                })
            })
        })
    }
    public initView(data:ui_att_item,index:number,call,type){
        this.m_data =data;
        this.m_call_back = call;
        this.m_index = index;
        this.m_type = type
        if(type===config.attributes_list_type.question_select){
            this.btn_delete.active = true;
        }else{
            this.btn_delete.active = false;
        }
        this.res_node.getComponent(receive_res_item).initView(this.m_data,type)
        this.att_node.getComponent(receive_widget_item).updateView(this.m_data,type)
        this.btn_delete.off(Node.EventType.TOUCH_END)
        this.btn_delete.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_call_back!=null){
                tools.show_dialog("确定删除此按钮？",()=>{
                    this.m_call_back(this.m_index)
                    this.node.destroy()
                    this.node.removeFromParent()
                })
            }
        })

        this.updateScoreStatus()
    }

    updateScoreStatus() {
        if(this.score_editBox) {
            if(this.m_data.score==undefined||this.m_data.score==null) {
                this.m_data.score = 0
            }
            this.score_editBox.string = this.m_data.score.toString()
        }
    }

    protected onDestroy(): void {
        this.res_node.destroy()
        this.att_node.destroy()
        this.score_node.destroy()
    }
}


