package com.common.lib_base.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.lib_base.R;
import com.common.lib_base.utils.StringUtils;
import com.common.lib_base.utils.ui.ToastUtils;


/**
 * Date: 2022/5/24 0024 14:21
 * Description: 按钮是圆角的
 */
public class MessageRoundEditBtnDialog extends Dialog implements View.OnClickListener {

    /*MessageRoundEditBtnDialog.Builder builder = new MessageRoundEditBtnDialog.Builder(mContext);
                builder.setHaveTitle(false)   //是否要标题
                        .setIsOneBtn(true)   //是否只有一个按钮  只有一个按钮的情况下面只有设置左边的按钮
                        .setIsCancle(true)   //外部点击是否可以关闭
                        .setTitle("提示框")    //标题栏的文字
                        .setTitleColorAndroidSize(0xFF3F51B5, 18)   //标题栏的文字大小  文字颜色
                        .setMessage("欢迎来到洒家卖蘑菇博客")                      //中间信息部分
                        .setMessageColorAndroidSize(0xFF009688, 16)   //中间信息部分的文字  大小  颜色
                        .setLeftButton("取消")                                   //左边按钮的文字
                        .setLeftButtonColorAndSize(0xFFFF5722, 16)      //左边文字的  颜色  大小
                        .setRightButton("确定")                                   //右边文字
                        .setRightButtonColorAndSize(0xFF47DDDB, 16)     //右边文字的颜色大小
                        .setBackGroundRes(R.drawable.chat_room_all_layout)        // dialog 的背景文件
                        .setLineColor(0xffececec)                                  //线的颜色
                        .setOnClickLisener(new MessageRoundEditBtnDialog.onClickListener() {    //监听事件
        @Override
        public void onLeftClick(Dialog dialog) {
            dialog.dismiss();
        }

        @Override
        public void onRightClick(Dialog dialog) {
            dialog.dismiss();
        }
    }).build().show();*/

    private String titleText;
    private int titleSzie;
    private int titleColor;
    private String message;
    private String leftBtnText;
    private String rightBtnText;
    private boolean isCancel;
    private int messageColor;
    private int messageSize;
    private onClickListener listener;
    private boolean isHaveTitle = false;
    private LinearLayout backgroundLayout;

    EditText et_content;

    private boolean isOneBtn = false;
    private int LeftButtonContentSize;
    private int LeftButtonContentColor;
    private int RightButtonContentSize;
    private int RightButtonContentColor;

    private int BackGroundRes;
    private int lineColor;


    //控件
    private LinearLayout title_layout;
    private TextView title, content, left_btn, right_btn;
    private View divider1, divider2, divider3;

    @Override
    protected void onStart() {
        super.onStart();
        // 设置背景色,0f为透明白色
//        this.getWindow().setDimAmount(0f);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

    }


    private void initView() {
        setContentView(R.layout.dialog_all_round_edit_btn);
        setCanceledOnTouchOutside(true);


        this.setCancelable(isCancel);

        title_layout = findViewById(R.id.title_layout);
        title = findViewById(R.id.title);
        et_content = findViewById(R.id.et_content);
        content = findViewById(R.id.content);
        left_btn = findViewById(R.id.left_btn);
        right_btn = findViewById(R.id.right_btn);
        divider1 = findViewById(R.id.divider1);
        divider2 = findViewById(R.id.divider2);
        divider3 = findViewById(R.id.divider3);
        backgroundLayout = findViewById(R.id.backgroundLayout);

        //是否有标题栏
        if (isHaveTitle) {
            //设置标题栏
            if (!TextUtils.isEmpty(titleText)) {
                title_layout.setVisibility(View.VISIBLE);
                title.setText(titleText);
            } else {
                title_layout.setVisibility(View.GONE);
            }
            //设置标题栏的颜色和字体大小
            if (titleColor != 0) {
                title.setTextColor(titleColor);
            }
            if (titleSzie != 0) {
                title.setTextSize(titleSzie);
            }
        } else {
            title_layout.setVisibility(View.GONE);
        }


        //设置信息
        if (!TextUtils.isEmpty(message)) {
            content.setText(message);
            content.setVisibility(View.VISIBLE);

            //设置信息的颜色
            if (messageColor != 0) {
                content.setTextColor(messageColor);
            }

            //设置信息的字体大小
            if (messageSize != 0) {
                content.setTextSize(messageSize);
            }

        } else {
            content.setVisibility(View.GONE);
        }


        //是否只有一个按钮，只显示右边按钮
        if (isOneBtn) {
            divider2.setVisibility(View.GONE);
            right_btn.setVisibility(View.VISIBLE);
            left_btn.setVisibility(View.GONE);
        } else {
            divider2.setVisibility(View.GONE);
            left_btn.setVisibility(View.VISIBLE);
            right_btn.setVisibility(View.VISIBLE);
        }

        //设置左边按钮上面的文字
        if (!TextUtils.isEmpty(leftBtnText)) {
            //左边文字的
            left_btn.setText(leftBtnText);
//            left_btn.setBackground(getContext().getResources().getDrawable(R.drawable.shape_dialog_left_grey));

            //左边文字大小
            if (LeftButtonContentSize != 0) {
                left_btn.setTextSize(LeftButtonContentSize);
            }

            //左边文字颜色
            if (LeftButtonContentColor != 0) {
                left_btn.setTextColor(LeftButtonContentColor);
            }

//            left_btn.setTextColor(getContext().getResources().getColor(R.color.appThemeColor));
        }

        //设置右边按钮上面的文字
        if (!TextUtils.isEmpty(rightBtnText)) {
            //右边文字
            right_btn.setText(rightBtnText);
//            right_btn.setBackground(getContext().getResources().getDrawable(R.drawable.shape_dialog_right_blue));

            //右边的文字大小
            if (RightButtonContentSize != 0) {
                right_btn.setTextSize(RightButtonContentSize);
            }

            //右边的文字颜色
            if (RightButtonContentColor != 0) {
                right_btn.setTextColor(RightButtonContentColor);
            }
//            right_btn.setTextColor(getContext().getResources().getColor(R.color.white));
        }

        //设置背景颜色
        if (BackGroundRes != 0) {
            backgroundLayout.setBackgroundResource(BackGroundRes);
        }

        //设置线的颜色
        if (lineColor != 0) {
            divider1.setBackgroundColor(lineColor);
            divider2.setBackgroundColor(lineColor);
            divider3.setBackgroundColor(lineColor);
        }


        left_btn.setOnClickListener(this);
        right_btn.setOnClickListener(this);

        Window window = this.getWindow();

//        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setBackgroundDrawableResource(android.R.color.transparent);

        DisplayMetrics d = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(d);
        WindowManager.LayoutParams p = this.getWindow().getAttributes();
        p.gravity = Gravity.CENTER;
        p.height = (int) WindowManager.LayoutParams.WRAP_CONTENT;
        p.width = (int) (d.widthPixels * 0.9);
        this.getWindow().setAttributes(p);

//        WindowManager.LayoutParams attributes = window.getAttributes();
//        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
//        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        window.setAttributes(attributes);
////        window.setWindowAnimations(R.style.DialogOutAndInStyle);
//        window.setGravity(Gravity.CENTER);

    }


    public MessageRoundEditBtnDialog(Builder builder) {
//        super(builder.context, R.style.mydialog);
        super(builder.context, R.style.MyDialogStyleTop);
        this.titleText = builder.title;
        this.titleSzie = builder.titleSize;
        this.titleColor = builder.titleColor;
        this.message = builder.message;
        this.listener = builder.listener;
        this.isCancel = builder.isCancel;
        this.leftBtnText = builder.LeftButtonContent;
        this.rightBtnText = builder.RightButtonContent;
        this.messageColor = builder.messageColor;
        this.messageSize = builder.messageSize;
        this.isHaveTitle = builder.isHaveTitle;

        this.isOneBtn = builder.isOneBtn;
        this.LeftButtonContentSize = builder.LeftButtonContentSize;
        this.LeftButtonContentColor = builder.LeftButtonContentColor;
        this.RightButtonContentSize = builder.RightButtonContentSize;
        this.RightButtonContentColor = builder.RightButtonContentColor;

        this.BackGroundRes = builder.BackGroundRes;
        this.lineColor = builder.lineColor;

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.left_btn) {
            if (listener != null) {
                listener.onLeftClick(this);
            }
        } else if (id == R.id.right_btn) {
            if (listener != null) {

                String et_content_str = et_content.getText().toString().trim();
                if (StringUtils.isNull(et_content_str)) {
                    ToastUtils.showLongToast("请输入内容");
                    return;
                }

                listener.onRightClick(this,et_content_str );
            }
        }
    }

    public interface onClickListener {
        void onLeftClick(Dialog dialog);

        void onRightClick(Dialog dialog, String text);
    }

    public abstract static class onDialogClickListener implements onClickListener {

        @Override
        public void onLeftClick(Dialog dialog) {
            dialog.dismiss();
        }

        @Override
        public void onRightClick(Dialog dialog, String text) {
            dialog.dismiss();
        }

    }


    public static class Builder {
        private Context context;

        private String title;
        private int titleColor;
        private int titleSize;
        private boolean isHaveTitle = true;

        private String message;
        private int messageColor;
        private int messageSize;

        private boolean isCancel = true;
        private boolean isOneBtn = false;

        private String LeftButtonContent;
        private int LeftButtonContentSize;
        private int LeftButtonContentColor;


        private String RightButtonContent;
        private int RightButtonContentSize;
        private int RightButtonContentColor;

        private int BackGroundRes;
        private int lineColor;

        private onClickListener listener;


        /**
         * 建造器的构造方法：
         *
         * @param context
         */
        public Builder(Context context) {
            this.context = context;
        }


        public Builder setHaveTitle(boolean b) {
            this.isHaveTitle = b;
            return this;
        }


        /**
         * @param title 标题文字
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * @param color 标题的文字颜色
         * @param size  标题的文字大小
         * @return
         */
        public Builder setTitleColorAndroidSize(int color, int size) {
            this.titleColor = color;
            this.titleSize = size;
            return this;
        }

        /**
         * @param message 设置提示的文字
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }


        /**
         * @param color 提示文字的文字颜色
         * @param size  提示文字的文字大小
         * @return
         */
        public Builder setMessageColorAndroidSize(int color, int size) {
            this.messageColor = color;
            this.messageSize = size;
            return this;
        }


        /***
         *
         * @param isCancel  设置外部是否点击可关闭
         * @return
         */
        public Builder setIsCancle(boolean isCancel) {
            this.isCancel = isCancel;
            return this;
        }


        /**
         * @param isOneBtn 是否只有一个按钮  只有一个按钮用左边
         * @return
         */
        public Builder setIsOneBtn(boolean isOneBtn) {
            this.isOneBtn = isOneBtn;
            return this;
        }


        /**
         * @param text 左边按钮的文字
         * @return
         */
        public Builder setLeftButton(String text) {
            this.LeftButtonContent = text;
            return this;
        }

        /**
         * @param color 左边按钮的颜色
         * @param size  左边按钮的大小
         * @return
         */
        public Builder setLeftButtonColorAndSize(int color, int size) {
            this.LeftButtonContentColor = color;
            this.LeftButtonContentSize = size;
            return this;
        }

        /**
         * @param text 右边按钮的文字
         * @return
         */
        public Builder setRightButton(String text) {
            this.RightButtonContent = text;
            return this;
        }


        /**
         * @param color 右边按钮的颜色
         * @param size  右边按钮的大小
         * @return
         */
        public Builder setRightButtonColorAndSize(int color, int size) {
            this.RightButtonContentColor = color;
            this.RightButtonContentSize = size;
            return this;
        }


        /**
         * @param Res 设置背景文件
         * @return
         */
        public Builder setBackGroundRes(int Res) {
            this.BackGroundRes = Res;
            return this;
        }

        /**
         * @param color 线的颜色
         * @return
         */
        public Builder setLineColor(int color) {
            this.lineColor = color;
            return this;
        }


        /**
         * @param lisener 按钮的点击事件
         * @return
         */
        public Builder setOnClickLisener(onClickListener lisener) {
            this.listener = lisener;
            return this;
        }

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

    }
}