package com.cooleshow.base.widgets;

import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;

import com.cooleshow.base.R;
import com.cooleshow.base.utils.PopupUtil;

/**
 * 创建日期：2022/6/1 15:31
 *
 * @author Ryan
 * 类说明：
 */
public class DialogUtil {
    public interface ShowListener {
        void onShow(ViewHolder holder, BaseDialog dialog);
    }
    public interface OnDialogButtonClickListener {
        void onCancel(View v);

        void onCommit(View v);
    }
    /*居中显示*/
    public static void showInCenter(FragmentManager fragmentmanager, int resourcesId, ShowListener showListener) {
        CommonDialog.init().setLayoutId(resourcesId)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, BaseDialog dialog) {
                        showListener.onShow(holder, dialog);
                    }
                })
                .setDimAmount(0.5f)
                .setGravity(Gravity.CENTER)
                .show(fragmentmanager);
    }

    /*居中显示*/
    public static void showInCenter(FragmentManager fragmentmanager, int resourcesId, String title, String content, OnDialogButtonClickListener onDialogButtonClickListener) {
        if (fragmentmanager == null) {
            return;
        }
        CommonDialog.init().setLayoutId(resourcesId)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, BaseDialog dialog) {
                        TextView tvTitle = holder.getView(R.id.tv_title);
                        TextView tvContent = holder.getView(R.id.tv_content);
                        TextView btncancel = holder.getView(R.id.btn_cancel);
                        TextView btnCommit = holder.getView(R.id.btn_commit);
                        tvTitle.setText(title);
                        tvContent.setText(content);
                        btncancel.setOnClickListener(view -> {
                            onDialogButtonClickListener.onCancel(view);
                            dialog.dismiss();
                        });
                        btnCommit.setOnClickListener(view -> {
                            onDialogButtonClickListener.onCommit(view);
                            dialog.dismiss();
                        });
                    }
                })
                .setDimAmount(0.2f)
                .setOutCancel(false)
                .setGravity(Gravity.CENTER)

                .show(fragmentmanager);
    }
}
