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.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

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


/**
 * @author: Pengxh
 * @email: 290677893@qq.com
 * @date: 2021-09-05 17:15:11
 * @description: 取消/确定对话框
 */
public class AlertControlDialog extends Dialog {

    private final Context ctx;
    private final String title;
    private final String message;
    private final String positiveBtn;
    private final String negativeBtn;
    private final OnDialogButtonClickListener listener;

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        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 setMessage(String message) {
            this.message = message;
            return this;
        }

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

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

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

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

    private AlertControlDialog(Builder builder) {
        super(builder.context, R.style.ControlDialogStyle);
        this.ctx = builder.context;
        this.title = builder.title;
        this.message = builder.message;
        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_alert);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        TextView dialogTitleView = findViewById(R.id.dialogTitleView);
        TextView dialogMessageView = findViewById(R.id.dialogMessageView);
        Button dialogCancelButton = findViewById(R.id.dialogCancelButton);
        Button dialogConfirmButton = findViewById(R.id.dialogConfirmButton);

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

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

        if (!TextUtils.isEmpty(negativeBtn)) {
            dialogCancelButton.setText(negativeBtn);
            dialogCancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.onCancelClick();
                        dismiss();
                    }
                }
            });
        }
        if (!TextUtils.isEmpty(positiveBtn)) {
            dialogConfirmButton.setText(positiveBtn);
            dialogConfirmButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.onConfirmClick();
                        dismiss();
                    }
                }
            });
        }
    }

    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 interface OnDialogButtonClickListener {
        void onConfirmClick();

        void onCancelClick();
    }
}
