package com.example.tyre.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.tyre.R;

/**
 * @author: wangh
 * @description:
 * @date: 2019-11-05 17:21
 */
public class CommonDialog extends Dialog {


    TextView dialogInfo,button;
    EditText editText;
    String message;
    private OnButtonClickListener buttonClickListener;
    private boolean type=true;
    public CommonDialog(Context context) {
        super(context, R.style.CustomDialog);
    }

    public void setType(boolean type) {
        this.type = type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);
        setCanceledOnTouchOutside(false);
        dialogInfo=findViewById(R.id.dialog_info);
        editText=findViewById(R.id.dialog_info_edit);

        if (!type) {
            editText.setVisibility(View.VISIBLE);
            dialogInfo.setVisibility(View.GONE);
        }
        button=findViewById(R.id.dialog_button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!type)  {
                    String edit_info=editText.getText().toString().trim();
                    if (edit_info.isEmpty()){
                        return;
                    }
                    buttonClickListener.getEdittext(edit_info);
                }

                dismiss();
            }
        });

        if (!TextUtils.isEmpty(message)){
            dialogInfo.setText(message);
        }

    }

    public String getMessage(){
        return editText.getText().toString().trim();
    }
    public CommonDialog  setMessage(String message) {
        this.message = message;
        return this ;
    }
    public void setOnButtonClickListener(OnButtonClickListener listener) {
        this.buttonClickListener = listener;
    }        //实现回调功能
     public interface OnButtonClickListener {
     void getEdittext(String s);
     }

    @Override
    public void dismiss() {
        editText.setText("");
        super.dismiss();
    }

    @Override
    public void show() {

        super.show();
    }
}
