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 DialogHisFav extends Dialog {
    public DialogHisFav(Context context) {
        super(context);
    }

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

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

    public static class Builder {
        private Context context;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private Button positiveButton;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.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(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

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

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


            ((Button) layout.findViewById(R.id.negativeButton))
                    .setText(negativeButtonText);
            if (negativeButtonClickListener != null) {
                ((Button) layout.findViewById(R.id.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;
        }
    }
}
