package com.unis.sxzcl.widget.menu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.unis.sxzcl.R;
import com.xyxsbj.mylibrary.tool.ETTool;
import com.xyxsbj.mylibrary.tool.StringTool;
import com.xyxsbj.mylibrary.tool.ToastTool;


/**
 * 包名: com.unis.sxzcl.widget.menu
 * 类名: MenuEditView
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2017/10/23
 * 更新人:
 * 更新时间: 2020/11/26 15:43
 * 更新说明:
 * 版本号: 1.0
 */
public class MenuEditView implements View.OnClickListener {
    private Context mContext;
    private View view;
    private EditText editText;
    private Button btnEnter;
    private Button btnClear;

    private EditCallBack callBack;

    public MenuEditView(Context mContext) {
        this.mContext = mContext;
        init();
    }

    private void init() {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

        view = LayoutInflater.from(mContext).inflate(R.layout.menu_edit_layout, null);
        view.setLayoutParams(params);
        editText = view.findViewById(R.id.menu_edit);
        btnEnter = view.findViewById(R.id.menu_enter);
        btnClear = view.findViewById(R.id.menu_clear);
        btnEnter.setOnClickListener(this);
        btnClear.setOnClickListener(this);
    }

    public View getView() {
        return view;
    }

    public MenuEditView setOnEditListener(EditCallBack listener) {
        this.callBack = listener;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_enter:
                if (StringTool.isEmpty(ETTool.getData(editText))) {
                    ToastTool.showLong("请输入内容");
                } else if (callBack != null) {
                    ETTool.closeSoftKeybord(editText);
                    callBack.onEditEnter(ETTool.getData(editText), view);
                }
                break;
            case R.id.menu_clear:
                editText.setText("");
                if (callBack != null) {
                    ETTool.closeSoftKeybord(editText);
                    callBack.onEditClear(view);
                }
                break;
        }
    }

    public interface EditCallBack {
        void onEditEnter(String text, View view);

        void onEditClear(View view);
    }

}
