package com.hw.nativeapp.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hw.nativeapp.R;


public class Dialog_Update extends Dialog {

    public Dialog_Update(Context mcontext) {
        super(mcontext);
    }

    public Dialog_Update(Context context, int themeResId){
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;
        private String message;//提示消息
        private String negative_text;//消极的
        private String positive_text;//积极的
        private DialogInterface.OnClickListener negativeListener;//消极的监听
        private DialogInterface.OnClickListener positiveListener;//积极的监听

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


        public Builder setMessage(String message) {
            if (message == null) {
                this.message = "您没有填写提示信息哦";
            }
            this.message = message;
            return this;
        }

        public Builder setNegativeButton(String negative_text, DialogInterface.OnClickListener negativeListener) {
            if (negative_text == null) {
                this.negative_text = "取消";
            }
            this.negative_text = negative_text;
            this.negativeListener = negativeListener;

            return this;
        }

        public Builder setPositionButton(String positive_text, DialogInterface.OnClickListener positiveListener) {
            if (positive_text == null) {
                this.positive_text = "确定";
            }
            this.positive_text = positive_text;
            this.positiveListener = positiveListener;

            return this;
        }


        private TextView etContent;
        private TextView btnupEnter;
        private TextView btnupCancel;


        public Dialog_Update create() {
            final Dialog_Update dialog = new Dialog_Update(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_update, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
//            dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            etContent = (TextView) view.findViewById(R.id.et_content);
            btnupEnter = (TextView) view.findViewById(R.id.btnup_enter);
            btnupCancel = (TextView) view.findViewById(R.id.btnup_cancel);

            etContent.setText(message);
            btnupCancel.setText(negative_text);
            btnupEnter.setText(positive_text);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            btnupCancel.setOnClickListener(v -> negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE));
            btnupEnter.setOnClickListener(v -> positiveListener.onClick(dialog, Dialog.BUTTON_POSITIVE));
            return dialog;
        }
    }
}
