package com.hw.nativeapp.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
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;
import com.hw.nativeapp.utils.StringUtils;


public class DialogQuantity extends Dialog {

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

    public static class Builder {
        private Context context;
        private String negative_text;//消极的
        private String positive_text;//积极的
        private boolean negativeShow = false;
        private boolean positiveShow = false;
        private OnClickListener negativeListener;//消极的监听
        private OnClickListener positiveListener;//积极的监听

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

        public String getNewContent(){
            if (modify_quantity != null){
                return modify_quantity.getText().toString();
            }
            return "";
        }

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

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

        private TextView modify_quantity;
        private TextView btnupEnter;
        private TextView btnupCancel;


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

            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));
            if (negativeShow){
                btnupCancel.setVisibility(View.VISIBLE);
            }
            if (positiveShow){
                btnupEnter.setVisibility(View.VISIBLE);
            }
            return dialog;
        }
    }
}
