package com.sunzee.ui.dialog;

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

import com.sunzee.R;

/**
 * 连接服务器的对话框 dialog
 */
public class ManagementSystemDialog extends Dialog {
    //在构造方法里提前加载了样式
    private Context context;//上下文
    private DialogClickListener listener;
    private Button mBtnApply;
    private EditText mEtServerId;


    public ManagementSystemDialog(Context context) {
        super(context, R.style.DialogBgD);//加载样式
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //提前设置Dialog的一些样式
        setContentView(R.layout.dialog_connect_server);
        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果输入为空。
                String trim = mEtServerId.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    Toast.makeText(context, "输入框不能为空！", Toast.LENGTH_SHORT).show();
                } else {
                    listener.onClickListener(R.id.btn_apply, trim);
                    dismiss();
                }

            }
        });
    }

    private void initView() {
        mBtnApply = findViewById(R.id.btn_apply);
        mEtServerId = findViewById(R.id.et_server_id);
    }

    public void setListener(DialogClickListener listener) {
        this.listener = listener;
    }

    public interface DialogClickListener {
        void onClickListener(int type, String text);
    }
}
