package com.genlot.ninglottery.widget.Diaolog;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;

import com.genlot.ninglottery.R;
import com.genlot.ninglottery.toolkit.LogTool;

public class ScanDialog extends BaseDialog {

    private OnScanResultListener listener;
    private String title;
    private String content;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setUpLayoutId() {
        return R.layout.dialog_scan;
    }

    public ScanDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public ScanDialog setContent(String content) {
        this.content = content;
        return this;
    }

    @Override
    public void convertView(ViewHolder holder, final BaseDialog dialog) {
        holder.setText(R.id.dialog_title, title);
        holder.setText(R.id.dialog_content, content);
        EditText editText = holder.getView(R.id.et_scanResult);
        editText.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                String result = editText.getText().toString().trim();
                LogTool.logD("scanResult:" + result);
                if (listener != null) {
                    listener.onScanResult(result);
                }
                dialog.dismiss();
            }
            return false;
        });
        holder.setOnClickListener(R.id.dialog_btn, v -> dialog.dismiss());
    }

    public interface OnScanResultListener {
        void onScanResult(String code);
    }

    public ScanDialog setOnScanResultListener(OnScanResultListener listener) {
        this.listener = listener;
        return this;
    }
}
