package com.wasu.cs.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import cn.com.wasu.main.R;


/**
 * Created by jeepc on 2016/9/7.
 */

public class DialogChildrenFav extends Dialog {
    public DialogChildrenFav(Context context) {
        super(context);
    }

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

    protected DialogChildrenFav(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String message;
        private Button negativeButton;
        private Button positiveButton;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;

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

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }



        public Builder setPositiveButton(OnClickListener listener) {
            this.positiveButtonClickListener = listener;
            return this;
        }


        public Builder setNegativeButton(OnClickListener listener) {
            this.negativeButtonClickListener = listener;
            return this;
        }

        public void positiveButtonRequestFocus(){
            positiveButton.requestFocus();
        }

        public DialogChildrenFav create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final DialogChildrenFav dialog = new DialogChildrenFav(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_layout_children, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            positiveButton = ((Button) layout.findViewById(R.id.positiveButton));
            if (positiveButtonClickListener != null) {
                positiveButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        positiveButtonClickListener.onClick(dialog,
                                DialogInterface.BUTTON_POSITIVE);
                    }
                });
            }


            negativeButton = (Button) layout.findViewById(R.id.negativeButton);
            if (negativeButtonClickListener != null) {
                negativeButton.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                negativeButtonClickListener.onClick(dialog,
                                        DialogInterface.BUTTON_NEGATIVE);
                            }
                        });
            }

            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            }

            dialog.setContentView(layout);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams wlp = window.getAttributes();
            Display d = window.getWindowManager().getDefaultDisplay();
            wlp.width = (int) (d.getWidth());
            wlp.height = (int) context.getResources().getDimension(R.dimen.d_300dp);
            //宽度按屏幕大小的百分比设置，这里我设置的是全屏显示
            wlp.gravity = Gravity.BOTTOM;
            wlp.y = 0;
            //如果是底部显示，则距离底部的距离是0
            window.setAttributes(wlp);
            positiveButtonRequestFocus();
            return dialog;
        }
    }
}
