package com.casic.dcms.widgets;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.casic.dcms.R;
import com.casic.dcms.utils.ToastHelper;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

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

    private final Context ctx;
    private final String title;
    private final String hint;
    private final String value;
    private final String positiveBtn;
    private final String negativeBtn;
    private final OnDialogButtonClickListener listener;

    private InputDialog(Builder builder) {
        super(builder.context);
        this.ctx = builder.context;
        this.title = builder.title;
        this.hint = builder.hint;
        this.value = builder.value;
        this.positiveBtn = builder.positiveBtn;
        this.negativeBtn = builder.negativeBtn;
        this.listener = builder.listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configDialogLayout();
        setContentView(R.layout.dialog_input);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        TextView dialogTitleView = findViewById(R.id.dialogTitleView);
        EditText dialogInputView = findViewById(R.id.dialogInputView);
        Button dialogCancelButton = findViewById(R.id.dialogCancelButton);
        Button dialogConfirmButton = findViewById(R.id.dialogConfirmButton);

        dialogTitleView.setText(title);
        if (!value.equals("")) {
            dialogInputView.setText(value);
        } else {
            dialogInputView.setHint(hint);
        }

        dialogCancelButton.setText(negativeBtn);
        dialogCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                listener.onCancelClick();
            }
        });

        dialogConfirmButton.setText(positiveBtn);
        dialogConfirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputValue = dialogInputView.getText().toString().trim();
                if (inputValue.isEmpty()) {
                    ToastHelper.showToast("什么都没输入，请重新输入", ToastHelper.WARING);
                    return;
                }
                dismiss();
                listener.onConfirmClick(inputValue);
            }
        });
    }

    private void configDialogLayout() {
        Window window = getWindow();
        if (window == null) {
            return;
        }
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.getDecorView().setBackgroundColor(Color.TRANSPARENT);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (QMUIDisplayHelper.getScreenWidth(ctx) * 0.8);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String hint;
        private String value;
        private String positiveBtn;
        private String negativeBtn;
        private OnDialogButtonClickListener listener;

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

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

        public Builder setHintText(String hint) {
            this.hint = hint;
            return this;
        }

        public Builder setDefaultValue(String value) {
            this.value = value;
            return this;
        }

        public Builder setPositiveButton(String name) {
            positiveBtn = name;
            return this;
        }

        public Builder setNegativeButton(String name) {
            negativeBtn = name;
            return this;
        }

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

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

    public interface OnDialogButtonClickListener {
        void onCancelClick();

        void onConfirmClick(String input);
    }
}