package com.lukouguoji.module_base.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.lukouguoji.module_base.R;
import com.ninetripods.sydialoglib.IDialog;
import com.ninetripods.sydialoglib.SYDialog;

/**
 * Created by mq on 2018/9/10 下午2:21
 * mqcoder90@gmail.com
 */

public class DialogUtil {

    /**
     * @param context               Context
     * @param title                 标题
     * @param content               内容
     * @param btn1Str               button文字
     * @param positiveClickListener 点击事件
     */
    public static void createDefaultDialog(Context context, String title, String content, String btn1Str, IDialog.OnClickListener positiveClickListener) {
        createDefaultDialog(context, title, content, btn1Str, positiveClickListener, "123", null);
    }

    /**
     * @param context               Context
     * @param title                 标题
     * @param content               内容
     * @param btn1Str               左边按钮
     * @param negativeClickListener 左边点击事件
     * @param btn2Str               右边按钮
     * @param positiveClickListener 右边点击事件
     */
    public static void createDefaultDialog(Context context, String title, String content, String btn1Str,
                                           IDialog.OnClickListener positiveClickListener, String btn2Str, IDialog.OnClickListener negativeClickListener) {
        SYDialog.Builder builder = new SYDialog.Builder(context);
        if (positiveClickListener != null) {
            if (TextUtils.isEmpty(btn1Str)) {
                builder.setPositiveButton(positiveClickListener);
            } else {
                builder.setPositiveButton(btn1Str, positiveClickListener);
            }
        }
        if (negativeClickListener != null) {
            if (TextUtils.isEmpty(btn2Str)) {
                builder.setNegativeButton(negativeClickListener);
            } else {
                builder.setNegativeButton(btn2Str, negativeClickListener);
            }
        }
        builder.setDialogView(R.layout.layout_dialog)//设置dialog布局
                .setAnimStyle(R.style.translate_style)//设置动画 默认没有动画
                .setScreenWidthP(0.75f) //设置屏幕宽度比例 0.0f-1.0f
                .setWidth(500)
                .setGravity(Gravity.CENTER)//设置Gravity
                .setWindowBackgroundP(0.2f)//设置背景透明度 0.0f-1.0f 1.0f完全不透明
                .setCancelable(true)//设置是否屏蔽物理返回键 true不屏蔽  false屏蔽
                .setCancelableOutSide(false);//设置dialog外点击是否可以让dialog消失
        builder.setBuildChildListener(new IDialog.OnBuildListener(){
            @Override
            public void onBuildChildView(IDialog dialog, View parent, int layoutRes) {
                TextView dialogTitle = parent.findViewById(R.id.dialogTitle);
                TextView etContent = parent.findViewById(R.id.et_content);

                if (!TextUtils.isEmpty(title)) {
                    dialogTitle.setText(title);
                }
                if (!TextUtils.isEmpty(content)) {
                    etContent.setText(content);
                }
            }
        });
        builder.show();
    }

}
