package com.casic.dcms.widgets;

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.casic.dcms.R;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

/**
 * @Author: Pengxh
 * @Time: 2021/4/13 13:18
 * @Email: 290677893@qq.com
 **/
public class InputDialog extends Dialog implements View.OnClickListener {

    private static final String TAG = "InputDialog";
    private String title;
    private OnDialogClickListener listener;
    private EditText serverView;
    private EditText portView;

    private InputDialog(Builder builder) {
        super(builder.mContext);
        this.title = builder.title;
        this.listener = builder.listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_input);
        initView();
    }

    private void initView() {
        TextView dialogTitle = findViewById(R.id.dialogTitle);
        serverView = findViewById(R.id.inputServerView);
        portView = findViewById(R.id.inputPortView);
        QMUIRoundButton dialogCancelButton = findViewById(R.id.dialogCancelButton);
        dialogCancelButton.setChangeAlphaWhenPress(true);
        dialogCancelButton.setOnClickListener(this);

        QMUIRoundButton dialogConfirmButton = findViewById(R.id.dialogConfirmButton);
        dialogConfirmButton.setChangeAlphaWhenPress(true);
        dialogConfirmButton.setOnClickListener(this);

        if (!TextUtils.isEmpty(title)) {
            dialogTitle.setText(title);
        }
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.dialogCancelButton) {
            if (listener != null) {
                listener.onCancelClick();
            }
        } else if (i == R.id.dialogConfirmButton) {
            if (listener != null) {
                String server = serverView.getText().toString().trim();
                String port = portView.getText().toString().trim();
                listener.onConfirmClick(server, port);
            }
        }
        this.dismiss();
    }

    public static class Builder {
        private Context mContext;
        private String title;
        private OnDialogClickListener listener;

        public Builder setContext(Context context) {
            this.mContext = context;
            return this;
        }

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

        public Builder setOnDialogClickListener(OnDialogClickListener listener) {
            this.listener = listener;
            return this;
        }

        public InputDialog build() {
            return new InputDialog(this);
        }
    }

    public interface OnDialogClickListener {
        void onConfirmClick(String serverValue, String portValue);

        void onCancelClick();
    }
}